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
// 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started