CHAPTER 6 Class-Advanced Concepts - Inheritance Page 1
Introduction: The idea of deriving a new class from the existing class. Provides the idea of code reusability. Existing class is called as base class (or) super class. Derived class is called as sub class. Derived class inherits the data members and member functions from the previously defined base class. Example: Base class Student Shape Loan Employee Account Derived classes GraduateStudent UndergraduateStudent Circle Triangle Rectangle CarLoan HomeImprovementLoan MortgageLoan FacultyMember StaffMember CheckingAccount SavingsAccount Types of Inheritance 1. Single Inheritance - One base class and one derived class. ssblc esbb ssac esbb 2. Multiple Inheritance - A class is derived from more than one base classes ssblc esbb1c ssblc esbbc2c Page 2 ssac esbb
3. Multilevel Inheritance - A sub class inherits from a class which inherits from another class. ssblc esbbc ssac esbbc1 ssac esbbc2 4. Hierarchical Inheritance - More than one subclass inherited from a single base class. ssblc esbbc Implementation of public inheritance: The general format of derived class from base class in public mode is: class subclass_name:public baseclass_name.. ; ssac esbbc1 ssac esbbc2 ssac esbbc3 Page 3
protected: Intermediate level of protection between private and public access specifies. Derived-class members can refer to public and protected members of the base class simply by using the member names Note that protected data breaks encapsulation Note: When you derive the sub class from the base class in public mode, specify the base class data members as protected members. The protected members are accessible in derived class. But the private members are not accessible. This is the only difference between the private and protected access specifiers. Single Inheritance: Example Program: Write a C++ program to implement the single inheritance on the classes employee and salary. Derive the class salary from the class employee in public mode. The class employee consists of employee number, employee name and designation as data members, and getdata() as a member function. The class salary consists of basic pay, human resource allowance, dearness allowance and Profitability Fund as data members. The member functions are getsalary(), calculatesalary() and display(). #include<iostream> using namespace std; class employee protected: int eno; char name[20]; char des[20]; public: void getdata(); ; Page 4
void employee::getdata() cout<<"enter the employee number"<<endl; cin>>eno; cout<<"enter the employee name"<<endl; cin>>name; cout<<"enter the designation"<<endl; cin>>des; class salary:public employee private: float bp,hra,da,pf,np; public: void getsal(); void calsal(); void display(); ; void salary::getsal() cout<<"enter the basic pay"<<endl; cin>>bp; cout<<"enter the Human Resource Allowance"<<endl; cin>>hra; cout<<"enter the Dearness Allowance"<<endl; cin>>da; cout<<"enter the Profitability Fund"<<endl; cin>>pf; void salary::calsal() np=bp+hra+da-pf; Page 5
void salary::display() cout<<""<<endl; cout<<"employee Number is :"<<eno<<endl; cout<<"employee Name is :"<<name<<endl; cout<<"employee Designation is :"<<des<<endl; cout<<"employee Basic pay is :"<<bp<<endl; cout<<"employee Human Resource Allowance is :"<<hra<<endl; cout<<"employee Dearness Allowance is :"<<da<<endl; cout<<"employee Profitability Fund is :"<<pf<<endl; cout<<"employee Net pay is :"<<np<<endl; cout<<""<<endl; int main() int i,n; salary s[10]; cout<<"enter the number of employee"<<endl; cin>>n; for(i=0;i<n;i++) s[i].getdata(); s[i].getsal(); s[i].calsal(); for(i=0;i<n;i++) s[i].display(); return(0); Page 6
Output: Enter the number of employee 3 Enter the employee number 1234 Enter the employee name ahmed Enter the designation manager Enter the basic pay 1200 Enter the Human Resource Allowance 34 Enter the Dearness Allowance 45 Enter the Profitability Fund 67 Enter the employee number 2345 Enter the employee name salim Enter the designation accountant Enter the basic pay 1000 Enter the Human Resource Allowance 25 Enter the Dearness Allowance 34 Enter the Profitability Fund 48 Enter the employee number 3456 Enter the employee name fathma Page 7
Enter the designation receptionist Enter the basic pay 700 Enter the Human Resource Allowance 23 Enter the Dearness Allowance 25 Enter the Profitability Fund 33 Employee Number is :1234 Employee Name is :ahmed Employee Designation is :manager Employee Basic pay is :1200 Employee Human Resource Allowance is :34 Employee Dearness Allowance is :45 Employee Profitability Fund is :67 Employee Net pay is :1212 Employee Number is :2345 Employee Name is :salim Employee Designation is :accountant Employee Basic pay is :1000 Employee Human Resource Allowance is :25 Employee Dearness Allowance is :34 Employee Profitability Fund is :48 Employee Net pay is :1011 Employee Number is :3456 Employee Name is :fathma Employee Designation is :receptionist Employee Basic pay is :700 Page 8
Employee Human Resource Allowance is :23 Employee Dearness Allowance is :25 Employee Profitability Fund is :33 Employee Net pay is :715 Exercises: 1. Write a C++ program to implement the single inheritance on the classes student and mark. Derive the class mark from the class student in public mode. The class student consists of student id, student name and sex as data members, and getdata() as a member function. The class mark consists of mark1, mark2, mark3 and average as data members. The member functions are getmark(), calculateaverage() and display(). Page 9