C++ Virtual function with person class

Description

C++ Virtual function with person class

#include <iostream>
using namespace std;
class person                         //person class
{
   protected:// ww  w  . j a v  a  2s  .  co m
   char name[40];
   public:
   void getName()
   {
      cout << "   Enter name: ";
      cin >> name;
   }
   void putName()
   {
      cout << "Name is: " << name << endl;
   }
   virtual void getData() = 0;        //pure virtual func
   virtual bool isOutstanding() = 0;  //pure virtual func
};
class Student : public person
{
   private:
   float gpa;              //grade point average
   public:
   void getData()          //get Student data from user
   {
      person::getName();
      cout << "   Enter Student's GPA: "; cin >> gpa;
   }
   bool isOutstanding()
   { return (gpa > 3.5) ? true : false; }
};
class professor : public person      //professor class
{
   private:
   int numPubs;             //number of papers published
   public:
   void getData()           //get professor data from user
   {
      person::getName();
      cout << "   Enter number of professor's publications: ";
      cin >> numPubs;
   }
   bool isOutstanding()
   { return (numPubs > 100) ? true : false; }
};
int main()
{
   person* persPtr[100];     //array of pointers to persons
   int n = 0;                //number of persons on list
   char choice;
   do {
      cout << "Enter Student or professor (s/p): ";
      cin >> choice;
      if(choice=='s')                  //put new Student
         persPtr[n] = new Student;     //   in array
      else                             //put new professor
         persPtr[n] = new professor;   //   in array
      persPtr[n++]->getData();         //get data for person
      cout << "   Enter another (y/n)? ";  //do another person?
      cin >> choice;
   } while( choice=='y' );          //cycle until not 'y'
   for(int j=0; j<n; j++)              //print names of all
   {                                //persons, and
      persPtr[j]->putName();           //say if outstanding
      if( persPtr[j]->isOutstanding() )
         cout << "   This person is outstanding\n";
   }
   return 0;
}



PreviousNext

Related