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

سؤااال بالInheritance

بدأه m6afe8 في 21 مايو 2011 · 3 رد · 648 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

هاااي قاايز..

كيف الحااال؟؟؟

عندي سؤااال بالInheritance

انا حاااولت وحليته بس المشكله ما عرفت أكمل

وهذا السؤااال..

Assume that the publisher in Exercises 1 and 3 decides to add a third way to distribute

books: on computer disk, for those who like to do their reading on their laptop. Add a

disk class that, like book and tape, is derived from publication. The disk class should

incorporate the same member functions as the other classes. The data item unique to this

class is the disk type: either CD or DVD. You can use an enum type to store this item.

The user could select the appropriate type by typing c or d.

وهذا حلللي..

class publication
   {
   private:
      string title;
      float price;
   public:
      void getdata()
         {
         cout << "\n   Enter title (one word only): ";
         cin >> title;
         cout << "   Enter price: ";
         cin >> price;
         }
      void putdata()
         {
         cout << "\n   Title: " << title;
         cout << "\n   Price: " << price;
         }
   };
class book : private publication
   {
   private:
      int pages;
   public:
      void getdata()
         {
         publication::getdata();
         cout << "   Enter number of pages: ";
            cin >> pages;
         }
      void putdata()
         {
         publication::putdata();
         cout << "\n   Pages: " << pages;
         }
   };
class tape : private publication
   {
   private:
      float time;
   public:
      void getdata()
         {
         publication::getdata();
         cout << "   Enter playing time: "; cin >> time;
         }
      void putdata()
         {
         publication::putdata();
         cout << "\n   Playing time: " << time;
         }
   };
class disk : private publication
   {
   private:
      disktype type;
   public:
      void getdata();
      void putdata()
#2

باقي تضيفي للكلاس الاخير نوع القرص diskType كمتغير من نوع enumeration

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#3

العنصرين title و price داخل الفئه publication محدد الوصول لهم لابد ان يكون protected حتي يسمح للفئات الوارثه بالتعديل على قيمهم مباشرة او الحصول على قيمهم.

الدالتين getdata و putdata داخل الفئه publication من الأفضل جعلهم virtual طالما انك تعلم بأنك ستقوم بعمل override لهم فى الفئات الوارثه، بطريقتك الحاليه تسمي shadowing.

إستخدام public اثناء الوراثة و ذلك حتى تستطيع إجراء up-casting بين الفئه الأب و الأبن.

و الله ولي التوفيق

تم تعديل هذه المشاركة بواسطة محمد علاء الدين في 21 مايو 2011 في 07:42

مدونتي: C++ Tips and Tricks

#4

شكرآ على الافاده

Celebrity-Image-Albert-Einstein--Religion--332121.jpgAlbert%20Einstein%20copy.jpg

البرت انينشتين

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