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

السؤال

نشر

Create a class called Person that stores the name (character array of size 30), gender (single character), and age (type integer). From this class derive two classes: Student, which stores a student id (type integer), faculty name (character array of size 20) and level (type integer), and Employee, which stores a company name (character array of size 20) and salary (type double). Each of these three classes should have setdata( ) method to set its data from the user, getdata( ) method to return the value of each class data member, and a display( ) method to print its data.

Write a main() program to test the person, student and employee classes by creating objects of them, asking the user to fill in data with setdata( ), and then displaying the data with display ( ) method.

Recommended Posts

  • 1
نشر

مرحباً بك.

يمكن تنفيذ المطلوب بالكود الآتي:

#include <iostream>
#include <cstring>

using namespace std;

struct PersonType {
    char name[30];
    char gender;
    int age;
};

struct StudentType {
    PersonType person;
    int student_id;
    char facutly_name[20];
    int level;
};

struct EmployeeType {
    PersonType person;
    char company_name[20];
    double salary;
};

class Person{
    public:
        char name[30];
        char gender;
        int age;

        void setdata(){
            cout << "Name: ";
            cin >> name;
            cout << "Gender: ";
            cin >> gender;
            cout << "Age: ";
            cin >> age;
        }

        PersonType getdata(){
            PersonType data;
            strcpy(data.name, name);
            data.age = age;
            data.gender = gender;
            return data;
        }

        void display(){
            cout << "Name: " << name << endl;
            cout << "Gender: " << gender << endl;
            cout << "Age: " << age << endl;
        }
};

class Student: public Person{
    public:
        int student_id;
        char facutly_name[20];
        int level;

        void setdata(){
            Person::setdata();
            cout << "Student ID: ";
            cin >> student_id;
            cout << "Faculty Name: ";
            cin >> facutly_name;
            cout << "Level: ";
            cin >> level;
        }

        StudentType getdata(){
            StudentType data;
            data.person = Person::getdata();
            data.student_id = student_id;
            strcpy(data.facutly_name, facutly_name);
            data.level = level;
            return data;
        }

        void display(){
            Person::display();
            cout << "Student ID: " << student_id << endl;
            cout << "Faculty Name: " << facutly_name << endl;
            cout << "Level: " << level << endl;
        }
};

class Employee:Person{
    public:
        char company_name[20];
        double salary;

        void setdata(){
            Person::setdata();
            cout << "Company Name: ";
            cin >> company_name;
            cout << "Salary: ";
            cin >> salary;
        }

        EmployeeType getdata(){
            EmployeeType data;
            data.person = Person::getdata();
            strcpy(data.company_name, company_name);
            data.salary = salary;
            return data;
        }

        void display(){
            Person::display();
            cout << "Company Name: " << company_name << endl;
            cout << "Salary: " << salary << endl;
        }
};

int main(){
    Person person;
    Student student;
    Employee employee;

    cout << "#Person: " << endl;
    person.setdata();
    cout << "#Student: " << endl;
    student.setdata();
    cout << "#Employee: " << endl;
    employee.setdata();

    person.display();
    student.display();
    employee.display();

    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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...