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.
#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();
}
Comments
Leave a comment