Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define an abstract class Person that has protected data name and an abstract met

ID: 3635921 • Letter: D

Question

Define an abstract class Person that has protected data name and an abstract method display. Define two derived classes called Male and Female that each define the display method and return void. The method in male identifies the person as a male and states his name. The method in Female identifies the person as a female and states her name. Write appropriate constructors. Write a test program that declares an array of 10 Persons that are instantiated as either Male or Female depending on info from the user. Then write a for loop to display the sex and name of each. What principle of object oriented programming is illustrated in this simple example? Can you give other examples of this principle?

Explanation / Answer

The principles illustrated: inheritance(derived classes).
Other examples of inheritance:
Class Animal is a base class. Derived classes: Class Dog, Class Cat.

Actually here there is also an illustration of polymorphism, as we have class Animal - abstract base class with abstract function display(); 

Ask if u have any other questions, Compiled in VS2010:

#include <string>
#include <iostream>
using namespace std;

class Person
{
protected:
string name;
public:
Person() { name = "unnamed"; }
virtual void display() = 0;
};

class Male : public Person
{
public:
Male() { name = "unnamed"; }
void assign(string iName) { name = iName; }

void display(){ cout << "Male, name: "<< name << endl; }
};

class Female : public Person
{
public:
Female() { name = "unnamed"; }
void assign(string iName) { name = iName; }

void display(){ cout << "Female, name: "<< name << endl; }
};

int main()
{
int f, m;
string name;

cout << "Please insert the number of women: ";
cin >> f;
cout << "Please insert the number of men: ";
cin >> m;
Female *women = new Female[f];
Male *men = new Male[m];

for (int i = 0; i < f; i++)
{
cout << "Please insert the name for woman" <<i+1<<": ";
cin >> name;

women[i].assign(name);
}

for (int i = 0; i < m; i++)
{
cout << "Please insert the name for man" <<i+1<<": ";
cin >> name;

men[i].assign(name);
}

cout << endl;
for (int i = 0; i < f; i++)
women[i].display();

cout << endl;
for (int i = 0; i < m; i++)
men[i].display();

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote