السلام عليكم
فى مقارنة بين كودين عن ال Dynamic Binding , انا مش مقتنع بال Output بتاعه خالص
الكود الاول :
#include<iostream.h>
#include<string.h>
class animal
{
protected:
int speed;
char color[20];
public:
void init_speed()
{
cout<<"Inside Animal Speed()"<<endl;
}
void init_color()
{
cout<<"Inside Animal Color()"<<endl;
}
};
class dog:public animal
{
public:
void init_speed()
{
speed=40;
cout<<"Speed of a dog is "<<speed<<endl;
}
void init_color()
{
strcpy(color,"Black");
cout<<"Color of a dog is "<<color<<endl;
}
};
class tiger:public animal
{
public:
void init_speed()
{
speed=90;
cout<<"Speed of a Tiger is "<<speed<<endl;
}
void init_color()
{
strcpy(color,"White");
cout<<"Color of a Tiger is "<<color<<endl;
}
};
main()
{
animal *p;
dog d1;
tiger t1;
p=&d1;
p->init_speed();
p->init_color();
p=&t1;
p->init_speed();
p->init_color();
}الكود التانى :
#include<iostream.h>
#include<string.h>
class animal
{
protected:
int speed;
char color[20];
public:
virtual void init_speed()
{
cout<<"Inside Animal Speed()"<<endl;
}
virtual void init_color()
{
cout<<"Inside Animal Color()"<<endl;
}
};
class dog:public animal
{
public:
void init_speed()
{
speed=40;
cout<<"Speed of a dog is "<<speed<<endl;
}
void init_color()
{
strcpy(color,"Black");
cout<<"Color of a dog is "<<color<<endl;
}
};
class tiger:public animal
{
public:
void init_speed()
{
speed=90;
cout<<"Speed of a Tiger is "<<speed<<endl;
}
void init_color()
{
strcpy(color,"White");
cout<<"Color of a Tiger is "<<color<<endl;
}
};
main()
{
animal *p;
dog d1;
tiger t1;
p=&d1;
p->init_speed();
p->init_color();
p=&t1;
p->init_speed();
p->init_color();
}ال OutPutبتاع الكود الاول :
اقتباسInside Animal Speed()Inside Animal Color()
Inside Animal Speed()
Inside Animal Color()
و ده اللى مش مقتنع بيه خالص
ال OutPutبتاع الكود التانى:
اقتباسSpeed of a dog is 40Color of a dog is Black
Speed of a Tiger is 90
Color of a Tiger is White
و ده انا مقتنع بيه تماما
بس انا بردو مقتنع ان الكود الاول لازم يطلع نفس نتيجة الكود التانى بالضبط , عشان المفروض هو ينفذ ال Function من النوع اللىبيشاور عليه الPointer بغض النظر عن نوع ال Pointer
ايه رأيكم؟؟؟؟

