141k views
5 votes
In C++ Write a program that implements these 3 classes mentioned in Person Student Employee • Use name as only data member of Person class. • Use name and GPA as the data members of Student class. • Use name and salary as the data members of Employee class. You are required to create the mutators and accessors in the classes to assign and retrieve the value(s) stored in the attributes.

User Timesha
by
8.4k points

1 Answer

3 votes

I have written the C++ code cleanly following the instructions you provided. You can examine it in the photograph. Since the variable "name" is common in all 3 classes, we derived the other classes from 'Person' class and saved ourselves from writing separate getter() and setter() methods for each class. In this way, we significantly reduced the compilation time and the number of lines in the program. If you have any questions about the parts you don't understand, you can ask me in the comments section. I wish you success.

#include <iostream>

typedef std::string str;

//Person class

class Person{

public:

//Get and set methods.

void setPersonName(str name) {

this->name = name;

}

str getPersonName() {

return name;

}

private:

//Our data must be private.

str name;

};

//Student class derived from Person class. (For the common getter() and setter() functions for name.)

class Student : public Person{

public:

//Get and set methods.

void setStudentGPA(float GPA) {

this->GPA = GPA;

}

float getStudentGPA() {

return GPA;

}

private:

float GPA;

};

//Employee class derived from Person class. (For the common getter() and setter() functions for name.)

class Employee : public Person{

public:

//Get and set methods.

void setEmployeeSalary(float salary) {

this->salary = salary;

}

int getEmployeeSalary() {

return salary;

}

private:

int salary;

};

int main(int argc, char* argv[]) {

//Person object

Person person1;

person1.setPersonName("Daniel");

//Student object. Inheritence used.

Student student1;

student1.setPersonName("Monica");

student1.setStudentGPA(3.84);

//Employee object. Inheritence used.

Employee employee1;

employee1.setPersonName("Anna");

employee1.setEmployeeSalary(17500);

//Print these values.

std::cout << "Person1's name: " << person1.getPersonName() << "\\"

<< "Student1's name: " << student1.getPersonName() << " & GPA: " << student1.getStudentGPA() << "\\"

<< "Employee1's name: " << employee1.getPersonName() << " & Salary: " << employee1.getEmployeeSalary()

<< std::endl;

return 0;

}

In C++ Write a program that implements these 3 classes mentioned in Person Student-example-1
User Trutheality
by
9.3k points

No related questions found