اذهب إلى المحتوى
  • 0

لغه السي بلس بلس

حمامه السلام

السؤال

1)Write a C++ class and name it Base. The Base class has two constant private fields: one 
of type string called name and one of type integer called age; and a constructor that accepts 
two inputs
The Base class also has three public methods: get_name() that returns the name field, 
get_age() that returns the age field, and get_info() that returns the following string (The 
name is name at the age of age.)
2)-Write another C++ class and name it Derived. The Derived class inherits form the base
class. The Derived class has one private field, type, of type string, a public method, 
get_type(), which returns the type, and a constructor that accepts three inputs.
The Derived class also has another public method that overrides the get_info() method,
inherited from the Base class, which returns the following string (The name is name (type)
at the age of age.)

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

مرحباً بك،
البرنامج المطلوب يتمحور حول البرمجة الكائنية التوجه او ( OOP Object-oriented programming) بلغة سي بلس بلس و يشمل على مفهوم الكلاس و الوراثة (inheritance) إضافة الى إعادة التعريف أو ما يسمى بال overriding
الكلاس الأساسي إسمه Base و يحتوي على حقلين ثابتين أحدهما من النوع string  و الآخر من نوع integer و يكون الوصول إليهما من النوع الخاص (private) إضافة إلى دالتين getters لجلب قيمة هاتين الحقلين و دالة ثالثة لطباعة القيم و أيضا دالة ال constructor
أما الجزء الثاني فينص على كتابة كلاس إسمه Derived يرث الكلاس الأول و يحتوي على حقل من نوع string و يكون الوصول إليه من النوع الخاص (private) إضافة إلى دالة getter لجلب قيمته و يقوم بإعادة تعريف الدالة get_info و أيضا يحتوي على دالة ال constructor

و هذا مثال بسيط للتطبيق
 

// Example program
#include <iostream>
using namespace std;

class Base {
    private:
      const string name;
    private:
      const int age;
   public:
      Base(string name_val, int age_val) : name(name_val), age(age_val) {}
   
   
    string get_name() 
    { 
        return this->name; 
    } 
    
    int get_age() 
    { 
        return this->age; 
    } 
    
    string get_info() 
    {
        return "The name is " + this->name + " and the age is " + std::to_string(this->age) + ".";
    }
   
};

class Derived : public Base 
{
    private:
         string type;
         
    public:
      Derived(string name, int age, string type) : Base(name, age)
        {
            this->type = type;
        }
      
    string get_type() 
    { 
        return this->type; 
    }
    
    string get_info() 
    {
        return "The name is: " + this->get_name() + "\nThe age is: " + std::to_string(this->get_age()) + "\nThe Type is: " + this->type;
    }
    
};

int main()
{
  Base b1("Samir Abboud", 24);
  cout << "Base Info: " << b1.get_info(); 
  Derived d1("Manar", 29, "manager");
  cout << "\nDerived Info: \n" << d1.get_info(); 
  
  return 0;
}

يمكنك أيضا تصفح أكواد التطبيق و تجربته أونلاين من هنا
كما يمكنك كتابة أكواد c++ و مشاركتها أونلاين من هنا
بالتوفيق

تم التعديل في بواسطة عبود سمير
رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...