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

كيفية تطبيق مفاهيم البرمجة كائنية التوجه بلغة c++

بسمة امل2

السؤال

 السلام عليكم

ياريت تساعدوني بحل هذا السؤال

Create a C++ class called employee that stores the name (a string) and number (type int) of an employee. From this class derive two classes: manager, which adds a tilte (type string); and scientist, which adds a number of publications (type int). Each of the three classes should have a getdata function (method) to get its data from the user at the keyboard, and a putdata function to display the data. Add a member function of type bool called isOverweight to the manager and scientist classes. Let’s say that an employee with more than 90kg is considered overweight. You can access this function from main() and display the string “Overweight” for overweighted managers and scientists when you display their other data. If manager and scientist objects are to be accessed using pointers to them that are stored in an array of type employee, what do you need to add to the employee base class? Can you instantiate members of this base class? Create a C++ program and instances of manager and scientist classes and call their methods. Then, create pointers of manager and scientist classes and store them in an array of type employee and call the isOverweight method.

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

Recommended Posts

  • 0

تحياتي أستاذة بسمة.

الكود المطلوب:

#include<iostream>
#include<string>
using namespace std;

class Employee
{
private:
    string name;
    int weight;
public:
    bool isOverWeight(){
        return weight >90;
    }
    void getData()
    {
        cout<<"\nEnter name:  "; cin>>name;
        cout<<"Enter weight: $ "; cin>>weight;
    }

    void putData()
    { cout<<"\nname:  "<<name<<"\nweight:  "<<weight;    }
    
    
};

//\////\////\////\////\////\////\////\////\////\////\////\//
//\////\////\////\////\////\////\////\////\////\////\////\//

class Manager : public Employee
{
private:
    string title;
public:
    void getData()
    { Employee::getData(); cout<<"Enter title:  "; cin>>title; }

    void putData()
    { Employee::putData(); cout<<"\ntitle:  "<<title; }

};

//\////\////\////\////\////\////\////\////\////\////\////\//
//\////\////\////\////\////\////\////\////\////\////\////\//

class Scientist : public Employee
{
private:
    int number_of_publications;
public:
    void getData()
    { Employee::getData(); cout<<"Enter number of publications:  "; cin>>number_of_publications; }

    void putData()
    { Employee::putData(); cout<<"\nnumber of publications:  "<<number_of_publications; }

};

int main()
{
    
    
    Manager manager;
    Scientist scientist;
    
    manager.getData();
    scientist.getData();

    cout<<"\a "<<endl; // ’\a'produces the beep sound
    manager.putData();
    cout<<endl;
    scientist.putData();
    cout<<endl;
    
    Employee *staff[2] ;
    staff[0] = &scientist;
    staff[1] = &manager;
    
    for(int i = 0; i<2; i++){
        if(staff[i]->isOverWeight()){
            cout<<i<<" is overweight";
        }
        else{
            cout<<i<<" is NOT overweight";
        }
    }


    return 0;
}

بالتوفيق.

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

  • 0
بتاريخ 35 دقائق مضت قال بسمة امل2:

 السلام عليكم

مرحباً بك ..

بتاريخ 36 دقائق مضت قال بسمة امل2:

Create a C++ class called employee that stores the name (a string) and number (type int) of an employee.

لعمل الصنف موظف نستخدم الأكواد التالية 

class Employee {
protected:
    string name;
    int number;

public:
    Employee() {}

public:
    Employee(string name, int number) {
        this->name = name;
        this->number = number;
    }

    void getData() {
        cout << "Enter Employee Name " << endl;
        cin >> this->name;
        cout << "\nEnter Employee Number " << endl;
        cin >> this->number;
    }

    void putData() {
        cout << "Employee Name: " << this->name << ", Employee Number: " << this->number << endl;
    }

};

و هنا برنامج لإستخدام و تجربة هذه الأكواد : إضغط هنا

بتاريخ 57 دقائق مضت قال بسمة امل2:

From this class derive two classes: manager, which adds a tilte (type string); and scientist, which adds a number of publications (type int).

لعمل هذا الجزء الذي يُعتبر تطبيق لمفهوم الوراثة في البرمجة الكائنية بلغة c++ نستعمل هذه الأكواد 

class Manager : public Employee
{
protected:
        string title;

public:
    Manager() {}
    Manager(string name, int number, string title) : Employee(name, number) {
        this->title = title;
    }

    void getData() {
        Employee::getData();
        cout << "Enter Manager Title " << endl;
        cin >> this->title;
    }

    void putData() {
        Employee::putData();
        cout << "Manager Title: " << this->title << endl;
    }

};

class Scientist : public Employee
{
protected:
    int numberOfPublications;

public:
    Scientist() {}
    Scientist(string name, int number, int numberOfPublications) : Employee(name, number) {
        this->numberOfPublications = numberOfPublications;
    }

    void getData() {
        Employee::getData();
        cout << "Enter Scientist Number Of Publications " << endl;
        cin >> this->numberOfPublications;
    }

    void putData() {
        Employee::putData();
        cout << "Scientist Number Of Publications: " << this->numberOfPublications << endl;
    }
};

و يُمكنك تجربة مثال بسيط لهذه الأكواد من خلال هذا : الرابط

بتاريخ 1 ساعة قال بسمة امل2:

Add a member function of type bool called isOverweight to the manager and scientist classes. Let’s say that an employee with more than 90kg is considered overweight. You can access this function from main() and display the string “Overweight” for overweighted managers and scientists when you display their other data

لعمل هذا الجزء نحتاج إلى إضافة خاصية الوزن للصنف موظف ثم نضيف الإقتران 

bool isOverweight() {
	return this->weight > 90;
}

يمكنك تجربة هذا المثال بعد إضافة هذه الخاصية: إضغط هنا

بتاريخ 2 ساعات قال بسمة امل2:

If manager and scientist objects are to be accessed using pointers to them that are stored in an array of type employee, what do you need to add to the employee base class? Can you instantiate members of this base class? Create a C++ program and instances of manager and scientist classes and call their methods. Then, create pointers of manager and scientist classes and store them in an array of type employee and call the isOverweight method

في هذه الحالة إذا كانت الدالة putData pure virtual سيتم إستعمال الدالة الموجودة في الكلاس الإبن أما إن كان العكس فسيستعمل الدالة الموجودة في الكلاس الأب لذلك سنُغير الدالة putData في الكلاس Employee إلى pure virtual بهذا الشكل 

virtual void putData() {
  cout << "Employee Name: " << this->name << ", Employee Number: " << this->number << ", Employee Weight:" << this->weight << endl;
}

فإذا إستعملنا هذا المثال 

int main() {

    Manager *m = new Manager("Samir", 1, 85, "CTO");
    Scientist *s = new Scientist("Ahmed", 2, 95, 23);

    Employee *employess[2] = {m, s};

    for (int i = 0; i < 2; ++i) {
        employess[i]->putData();
    }
}

ستكون النتيجة بهذا الشكل 

cpp_pure_virtual.thumb.PNG.f327e2e593ccbff9e7889206f817fc18.PNG

أما في حالة لم تكن الدالة pure virtual ستكون النتيجة بهذا الشكل 

cpp_without_pure_virtual.thumb.PNG.857b469360ea2ea5ba8de91fa9e8cb23.PNG

يمكنك تجربة البرنامج إنطلاقاً من هذا : الرابط

بالتوفيق

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

  • 0

اشكرك بارك الله بيك وبكل من يساهم في مساعدة الاخرين وبكل من ساهم في عمل هذا الموقع الرائع

بتاريخ 7 ساعات قال مصطفى القباني:

تحياتي أستاذة بسمة.

الكود المطلوب:


#include<iostream>
#include<string>
using namespace std;

class Employee
{
private:
    string name;
    int weight;
public:
    bool isOverWeight(){
        return weight >90;
    }
    void getData()
    {
        cout<<"\nEnter name:  "; cin>>name;
        cout<<"Enter weight: $ "; cin>>weight;
    }

    void putData()
    { cout<<"\nname:  "<<name<<"\nweight:  "<<weight;    }
    
    
};

//\////\////\////\////\////\////\////\////\////\////\////\//
//\////\////\////\////\////\////\////\////\////\////\////\//

class Manager : public Employee
{
private:
    string title;
public:
    void getData()
    { Employee::getData(); cout<<"Enter title:  "; cin>>title; }

    void putData()
    { Employee::putData(); cout<<"\ntitle:  "<<title; }

};

//\////\////\////\////\////\////\////\////\////\////\////\//
//\////\////\////\////\////\////\////\////\////\////\////\//

class Scientist : public Employee
{
private:
    int number_of_publications;
public:
    void getData()
    { Employee::getData(); cout<<"Enter number of publications:  "; cin>>number_of_publications; }

    void putData()
    { Employee::putData(); cout<<"\nnumber of publications:  "<<number_of_publications; }

};

int main()
{
    
    
    Manager manager;
    Scientist scientist;
    
    manager.getData();
    scientist.getData();

    cout<<"\a "<<endl; // ’\a'produces the beep sound
    manager.putData();
    cout<<endl;
    scientist.putData();
    cout<<endl;
    
    Employee *staff[2] ;
    staff[0] = &scientist;
    staff[1] = &manager;
    
    for(int i = 0; i<2; i++){
        if(staff[i]->isOverWeight()){
            cout<<i<<" is overweight";
        }
        else{
            cout<<i<<" is NOT overweight";
        }
    }


    return 0;
}

بالتوفيق.

اشكرك استاذ مصطفى على جهدك المبذول وبارك الله بيك وجزاك خير الجزاء

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

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...