// Define and use a class that utilizes the virtual function's concept. // Assignment-29 // virtprs.cpp, Author - Krishna Kumar Khatri. // Uncomment ONLY the following line if you are using Turbo/Borland compiler. // #define TC #ifdef TC #include #else #include using namespace std; #endif #include // For getch. const int MAXLEN = 50; #ifdef TC enum bool {false, true}; #endif class Person { char* name; char dob[11]; public: Person() : name(new char[MAXLEN]) { } virtual void getdata(); // Will be overriden. virtual void putdata(); // Will be overriden. virtual bool outstanding() = 0; // Pure virtual function. }; void Person::getdata() { cout << flush << "Enter name: "; cin.getline(name, MAXLEN, '~'); cout << "Enter DoB (mm/dd/yyyy): "; cin >> dob; } void Person::putdata() { cout << endl << name; cout << ", DoB: " << dob; cout << ", Outstanding: "; outstanding() ? cout << "Yes" : cout << "No"; } class Student : public Person { float gpa; // Avg grade point. public: void getdata(); void putdata(); bool outstanding(); }; void Student::getdata() { Person::getdata(); cout << "Enter GPA: "; cin >> gpa; } void Student::putdata() { Person::putdata(); cout << ", GPA: " << gpa; } bool Student::outstanding() { return gpa > 90.0 ? true : false; } class Professor : public Person { int numpubs; // Number of papers published. public: void getdata(); void putdata(); bool outstanding(); }; void Professor::getdata() { Person::getdata(); cout << "Enter number of papers published: "; cin >> numpubs; } void Professor::putdata() { Person::putdata(); cout << ", Papers published: " << numpubs; } bool Professor::outstanding() { return numpubs > 100 ? true : false; } int main() { #ifdef TC clrscr(); #endif Student std; Professor pfs; Person* prs; prs = &std; prs->getdata(); prs->putdata(); cout << endl << endl; prs = &pfs; prs->getdata(); prs->putdata(); cout << endl << endl << "Program over. Press any key to exit..."; getch(); return 0; // Successful termination. }