C++ examples for Class:Inheritance
Models Employee database using inheritance
#include <iostream> using namespace std; const int LEN = 80; //maximum length of names class Employee //Employee class { private:/*from www .j a v a 2s . co m*/ char name[LEN]; //Employee name unsigned long number; //Employee number public: void getdata() { cout << "\n Enter last name: "; cin >> name; cout << " Enter number: "; cin >> number; } void putdata() const { cout << "\n Name: " << name; cout << "\n Number: " << number; } }; class Manager : public Employee //management class { private: char title[LEN]; //"vice-president" etc. double dues; //golf club dues public: void getdata() { Employee::getdata(); cout << " Enter title: "; cin >> title; cout << " Enter golf club dues: "; cin >> dues; } void putdata() const { Employee::putdata(); cout << "\n Title: " << title; cout << "\n Golf club dues: " << dues; } }; class Scientist : public Employee //Scientist class { private: int pubs; //number of publications public: void getdata() { Employee::getdata(); cout << " Enter number of pubs: "; cin >> pubs; } void putdata() const { Employee::putdata(); cout << "\n Number of publications: " << pubs; } }; class Programmer : public Employee //Programmer class { }; int main() { Manager m1, m2; Scientist s1; Programmer l1; cout << endl; //get data for several Employees cout << "\nEnter data for Manager 1"; m1.getdata(); cout << "\nEnter data for Manager 2"; m2.getdata(); cout << "\nEnter data for Scientist 1"; s1.getdata(); cout << "\nEnter data for Programmer 1"; l1.getdata(); //display data for several Employees cout << "\nData on Manager 1"; m1.putdata(); cout << "\nData on Manager 2"; m2.putdata(); cout << "\nData on Scientist 1"; s1.putdata(); cout << "\nData on Programmer 1"; l1.putdata(); cout << endl; return 0; }