Question: For the following C++ code: #include #include using namespace std; // Base class Person class Person { protected: string name; int age; public: // Constructor

For the following C++ code:

#include #include using namespace std;

// Base class Person

class Person {

protected:

string name;

int age;

public:

// Constructor for Person

Person(string name = "NA", int age = 0) {

this->name = name;

this->age = age;

}

// Virtual method Show for Person

virtual void Show() {

cout << "Name: " << name << endl;

cout << "Age: " << age << endl;

}

};

// Derived class Farmer

class Farmer : public Person {

private:

string role;

int yearsOfService;

double salary;

public:

// Constructor for Farmer

Farmer(string name = "NA", int age = 0, string role = "NA", int yearsOfService = 0, double salary = 0.0) : Person(name, age) {

this->role = role;

this->yearsOfService = yearsOfService;

this->salary = salary;

}

// Overridden method Show for Farmer

void Show() override {

cout << "Name: " << name << endl;

cout << "Age: " << age << endl;

cout << "Role: " << role << endl;

cout << "Years of service: " << yearsOfService << endl;

cout << "Salary: " << salary << endl;

}

};

// Derived class Client

class Client : public Person {

private:

int clientCode;

string city;

public:

// Constructor for Client

Client(string name = "NA", int age = 0, int clientCode = 0, string city = "NA") : Person(name, age) {

this->clientCode = clientCode;

this->city = city;

}

// Overridden method Show for Client

void Show() override {

cout << "Name: " << name << endl;

cout << "Age: " << age << endl;

cout << "Client code: " << clientCode << endl;

cout << "City: " << city << endl;

}

};

int main() {

// Create objects of each class

Person person("Paolo", 30);

Farmer farmer("Liam", 40, "Farmer", 10, 1500.00);

Client client("Farah", 20, 12345, "Jordan");

// Call Show method for each object

person.Show();

cout << endl;

farmer.Show();

cout << endl;

client.Show();

return 0;

}

Create a second version of the base class Person and the two derived classes Farmer and Client. Add the virtual method Show() and modify the main() in order to access the method through a same base class pointer (pointing to the different objects).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!