Answer to Question #209967 in C++ for Dunga

Question #209967

Create a C++ program to demonstrate the working of pure virtual function. Thus, you are required to implement the following code tasks: Write a base class called person that describes a person of either gender. Define two derived classes called man and woman that define gender specific items. Write pure virtual functions in the base class for operations that are common to both sexes yet are handled in different ways by each of them. 



1
Expert's answer
2021-06-23T17:07:17-0400
#include<iostream>
using namespace std;
class Person{
    string name;
   public:
      virtual void print() = 0; 
};


class Woman:public Person {
   public:
      void print() {
         cout << "\nI am a woman";
      }
};
class Man:public Person {
   public:
      void print() {
         cout << "\nI am a man";
      }
};
int main() {
   Person *p;
   Woman w;
   p = &w;
   p->print();
}

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
APPROVED BY CLIENTS