C++ examples for Class:Inheritance
Multiple inheritance with Employees
#include <iostream> using namespace std; const int LEN = 80; //maximum length of names class Student//from ww w.j a v a 2 s . com { private: char school[LEN]; //name of school or university char degree[LEN]; //highest degree earned public: void getedu() { cout << " Enter name of school or university: "; cin >> school; cout << " Enter highest degree earned \n"; cout << " (Highschool, Bachelor's, Master's, PhD): "; cin >> degree; } void putedu() const { cout << "\n School or university: " << school; cout << "\n Highest degree earned: " << degree; } }; class Employee { private: 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 : private Employee, private Student //management { 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; Student::getedu(); } void putdata() const { Employee::putdata(); cout << "\n Title: " << title; cout << "\n Golf club dues: " << dues; Student::putedu(); } }; class Scientist : private Employee, private Student //Scientist { private: int pubs; //number of publications public: void getdata() { Employee::getdata(); cout << " Enter number of pubs: "; cin >> pubs; Student::getedu(); } void putdata() const { Employee::putdata(); cout << "\n Number of publications: " << pubs; Student::putedu(); } }; class Programmer : public Employee { }; int main() { Manager m1; Scientist s1, s2; Programmer l1; cout << endl; cout << "\nEnter data for Manager 1"; //get data for m1.getdata(); //several Employees cout << "\nEnter data for Scientist 1"; s1.getdata(); cout << "\nEnter data for Scientist 2"; s2.getdata(); cout << "\nEnter data for Programmer 1"; l1.getdata(); cout << "\nData on Manager 1"; //display data for m1.putdata(); //several Employees cout << "\nData on Scientist 1"; s1.putdata(); cout << "\nData on Scientist 2"; s2.putdata(); cout << "\nData on Programmer 1"; l1.putdata(); cout << endl; return 0; }