Answer to Question #151743 in C++ for dani

Question #151743

Create a class Person having name, age and gender as its data members.Create another class Employee which has employername and dailywages as it data member. From these two classes derive another class Teacher which contains teacher grade as data member.   Write set and get functions to enter and display the data members.

                 

1
Expert's answer
2020-12-17T18:44:11-0500
#include <iostream>
#include <utility>

using namespace std;

//Person class definition
class Person {
    //instance variable declaration
private:
    string name;
    int age{};
    char gender{};
public:
    //setter methods to set name,age and gender
    void setName(string pName) {
        name = std::move(pName);
    }

    void setAge(int pAge) {
        age = pAge;
    }

    void setGender(char pGender) {
        gender = pGender;
    }

    //getter methods to return name,age and gender
    string getName() {
        return name;
    }

    [[nodiscard]] int getAge() const {
        return age;
    }

    [[nodiscard]] char getGender() const {
        return gender;
    }
};

//Employee class definition
class Employee {
    //instance variable declaration
private:
    string employerName;
    float dailyWages{};
public:
    //setter methods to set employerName and dailyWages
    void setEmployerName(string eName) {
        employerName = std::move(eName);
    }

    void setDailyWages(float dWage) {
        dailyWages = dWage;
    }

    //getter methods to return employerName and dailyWages
    string getEmployerName() {
        return employerName;
    }

    [[nodiscard]] float getDailyWages() const {
        return dailyWages;
    }
};

/*Teacher class derived from Person class and Employee class with public
visibility mode.It is multiple inheritance*/
class Teacher : public Person, public Employee {
    //instance variable declaration
private:
    string teacherGrade;
public:
    //setter methods to set teacherGrade
    void setTeacherGrade(string grade) {
        teacherGrade = std::move(grade);
    }

    //getter methods to return teacherGrade
    string getTeacherGrade() {
        return teacherGrade;
    }
};

int main() {
    //creating an instance of Teacher class t
    Teacher t;
    /*set t's name,age and gender using setter methods.(Since Teacher derived from Person,
    it consist the attributes name,age and gender*/
    t.setName("Tom Jonson");
    t.setAge(42);
    t.setGender('M');
    /*set t's employerName and dailyWage using setter methods.(Since Teacher derived from
    Employee also,it consist the attributes employerName and dailyWage.
    Teacher derived from Person and Employee.So it is multiple inheritance*/
    t.setEmployerName("Thomas Mathew");
    t.setDailyWages(100);
    //set t's teacherGrade using setter method
    t.setTeacherGrade("grade 3");
    //print Teacher t's details using getter methods
    cout << "Teacher t's information..." << endl;
    cout << "Teacher t's name: " << t.getName() << endl;
    cout << "Teacher t's age: " << t.getAge() << endl;
    cout << "Teacher t's gender: " << t.getGender() << endl;
    cout << "Teacher t's employer name: " << t.getEmployerName() << endl;
    cout << "Teacher t's daily wages: $" << t.getDailyWages() << endl;
    cout << "Teacher t's grade: " << t.getTeacherGrade() << endl;
    return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog