VIRTUAL FUNCTIONS AND PERSON CLASS
Now that we understand this topic. It uses the person class and it add two derived classes , student and professor.These derived classes each contain a function called isoutstanding( ).This function makes it easy for the school administrators to creat a list of outstanding students and professors for the award day cermoney. Here the class is ...........
#include <iostream>
#include <string> // for string
using namespace std;
class person
{
protected:
string name;
public:
void get()
{
cout<<"enter name";
cin>>name;
}
void print ()
{
cout<<"name is"<<name;
}
virtual void read()=0; // pure virtual function
virtual bool isoutstanding()=0; // pure virtual func..
};
class student:public person
{
protected:
float cgpa; // average marks
public:
void read()
{
person::get();
cout<<"enter cgpa";
cin>>cgpa;
}
bool isoutstanding()
{
if(cgpa>=3.5)
return true;
else
return false;
}
};
class professor:public person
{
protected:
int nop; // number of publications
public:
void read()
{
person::get();
cout<<"enter nop";
cin>>nop;
}
bool isoutstanding ()
{
if(nop>=100)
return true;
else
return false;
}
};
void main ()
{
person *arr[30];
int n=0;
char choice;
do
{
cout<<"enter data for (s/p)";
cin>>choice;
if(choice=='s')
arr[n]=new student;
else
arr[n]=new professor;
arr[n]->read();
n++;
cout<<"Do you want to continue (y/n)";
cin>>choice;
}
while(choice=='y');
for(int i=0;i<n;i++)
{
arr[i]->print();
if(arr[i]->isoutstanding() )
cout<<"This person is outstanding";
else
cout<<"This person is not outstanding";
}
}