الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Dynamic Binding

بدأه hanysaad في 11 يوليو 2008 · 3 رد · 1,637 مشاهدة · في لغة C و ++C
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

فى مقارنة بين كودين عن ال 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 40

Color of a dog is Black

Speed of a Tiger is 90

Color of a Tiger is White

و ده انا مقتنع بيه تماما

بس انا بردو مقتنع ان الكود الاول لازم يطلع نفس نتيجة الكود التانى بالضبط , عشان المفروض هو ينفذ ال Function من النوع اللىبيشاور عليه الPointer بغض النظر عن نوع ال Pointer

ايه رأيكم؟؟؟؟

Hany Saad Mostafa

ITI (Information Technology Institute) l

SD Dept.-Teaching Assistant

---

اسألكم الدعاء بظهر الغيب

#2

هذه من خواص الكلاس يختار المصرف الدالة التي تتطابق مع نوع المؤشر وليس محتواه

لكن اذا وضعت virtual أمام اسم الدالة فعند الاستدعاء ينظر الى محتوى المؤشر وليس نوعه.

tvquran_6.gif

#3

مرحبا

تفضل اخي هالرابط , وان شاء الله راح يساعدك لكي تفهم ماحدث

مـــــــن هــــــــنا

llback.jpg

اشهد ان لا إله إلا الله وان محمدا ً رسول الله

#4

السلام علكيم

شكرا ليكم على الرد

انا فعلا اتلغبطت بين ال C# وال C++

...

Hany Saad Mostafa

ITI (Information Technology Institute) l

SD Dept.-Teaching Assistant

---

اسألكم الدعاء بظهر الغيب

مواضيع مشابهة