Base and Derived Class
Inheritance
- Inheritance is the process of organizing information in a hierarchical form.
- It helps in code reusability.
- Inheritance is the mechanism of code reusability by deriving a new class which can inherits all or some of the characteristics of base class and also can add new features.
- The class which is used to derive new classes is called base class.
- The new classes derived from the existing class are called derived class.
- Base class is also known as ancestor, parent or super class.
-Derived class is also known as descendant, child or sub class.
Protected Access Specifier
- It causes a member to be visible by the member function and friend function of a class where it is declared.
- The protected members are visible to derived class members and friend of classes derived from base class.
- The purpose of making data member protected in a class is to make such members accessible to derived class function.
Derived Class Declaration
- Derived class inherits all features of its parent class and add its own features too.
- Syntax
class derived_class_name : visibility_mode base_class_name { //members of derived class };
Member Function Overriding
- The process of creating members in the derived class with the same name as that of the visible members of base class is called overriding.
- It is called overriding as the new name overrides or hides the old name inherited from base class.
Illustration of Overriding
#include
class base { protected: int num; public: void read() { cout << "Enter number in base : "; cin >> num; } void show() { cout << "The number in base is : " << num; } };
class derived : public base { private: int num; public: void read() { cout << "Enter number in derived : "; cin >> num; } void show() { cout << "The number in derived is : " << num; } };
int main() { derived dl; dl.read(); dl.show(); return 0; }
Output: ----------------- Enter number in derived : 5 The number in derived is : 5
Forms of Inheritance
Single Inheritance
- When a class is derived from only one base class, such derivation is called single inheritance.
Illustration:
#include
class student { private: char name[30]; int id; int age; public: void getdata() { cout << "Enter name, id and age : "; cin >> name >> id >> age; } void show() { cout << name << id << age; } };
class leader : public student { private: char union_name[25]; public: void getdata() { student :: getdata(); cout << "Enter name of student union : "; cin >> union_name; } void show() { student :: show(); cout >> union_name; } };
int main() { leader ld; ld.getdata(); ld.show(); return 0; }
Multiple Inheritance
- When a class is derived from two or more base class, it is called multiple inheritance.
Illustration:
#include
class school { private: char school_name[30]; char address[40]; public: void getdata() { cout << "Enter name of school and address : "; cin >> school_name >> address; } void show() { cout << school_name << address; } };
class college { private: char col_name[40]; long int phone; public: void getdata() { cout << "Enter name of college and phone number : "; cin >> col_name, phone; } void show() { cout << col_name << phone; } };
class student : public school, public college { private: char name[30]; int age; public: void getdata() { school :: getdata(); college :: getdata(); cout << "Enter name and age of student : "; cin >> name >> age; } void show() { school :: show(); college :: show(); cout << name << age; } };
int main() { student s1; s1.getdata(); s1.show(); return 0; }
Multi Level Inheritance
- The derivation of a class from another derived class is called multi level inheritance.
Illustration:
#include
class company { private: char company_name[30]; char address[40]; public: void getdata() { cout << "Enter name of conpany and address : "; cin >> company_name >> address; } void show() { cout << company_name << address; } };
class employee : public company { private: char emp_name[40]; int id; public: void getdata() { company :: getdata(); cout << "Enter name of employee and id : "; cin >> emp_name >> id; } void show() { company :: show(); cout << emp_name << id; } };
class manager : public employee { private: float salary; public: void getdata() { employee :: getdata(); cout << "Enter salary of manager : "; cin >> salary; } void show() { employee :: show(); cout << salary; } };
int main() { manager m1; m1.getdata(); m1.show(); return 0; }
Hierarchical Inheritance
- When two or more classes are derived from single base class, it is called hierarchical inheritance.
Illustration:
#include
class employee { private: char emp_name[40]; int id; public: void getdata() { cout << "Enter name of employee and id : "; cin >> emp_name >> id; } void show() { cout << emp_name << id; } };
class manager : public employee { private: float salary; public: void getdata() { employee :: getdata(); cout << "Enter salary of manager : "; cin >> salary; } void show() { employee :: show(); cout << salary; } };
class salesperson : public employee { private: char degree[30]; public: void getdata() { employee :: getdata(); cout << "Enter degree of salesperson : "; cin >> degree; } void show() { employee :: show(); cout << degree; } };
int main() { manager m1; m1.getdata(); m1.show(); salesperson s1; s1.getdata(); s1.show(); return 0; }
Hybrid Inheritance
- When more than one form of inheritance are mixed, it is called hybrid inheritance.
Multipath Inheritance and Virtual Base Class
- Creating a new derived class by multiple inheritance from base classes which are derived earlier from the same base class is called multipath inheritance.
- In multipath inheritance shown in figure alongside, the classes B and C inherits the features of class A. The class D inherits the same features from A through two paths B and C, causing ambiguity in accessing first base class A members.
- This ambiguity is eliminated by declaring base class as virtual while creating derived classes from the first base class.
Illustration of Multipath Inheritance
#include
class student { int roll; char name[30]; public: void getdata() { cout << "Enter name and roll number : "; cin >> name >> roll; } void show() { cout << name << roll; } };
class test : virtual public student { protected: float sem1, sem2; public: void getdata() { cout << "Marks of sem1 and sem2 : "; cin >> sem1 >> sem2; } void show() { cout << sem1 << sem2; } };
class sport : virtual public student { protected: float score; public: void getdata() { cout << "Score in sports : "; cin >> score; } void show() { cout << score; } };
class result : public test, public sport { float total; public: void getdata() { stuent :: getdata(); test :: getdata(); sport :: getdata(); } void show() { total = sem1 + sem2 + score; student :: show(); test :: show(); sport :: show(); cout << total; } };
int main() { result r; r.getdata(); r.show(); return 0; }
Constructor Invocation
Constructor in Single and Multiple Inheritances
- In inheritance, when object of derived class is created, the base class constructor is called first and the constructor in derived class is called next.
- If base class has no constructor or has a no argument constructor, it is not necessary to define constructor in derived class.
- If base class has parameterized constructor, the derived class must define a constructor and explicitly pass argument to base class constructor. Otherwise, the base class constructor with argument is never invoked.
Constructor in Base Class Only
class base { public: base() { cout << "From base class"; } };
class derived : public base
{
// Body of derived class
};
int main()
{
derived d;
}
Output
---------------------------
From base class
Constructor in Derived Class Only
class base { //Body of base class };
class derived : public base
{
public:
derived()
{
cout << "From derived class";
}
};
int main()
{
derived d;
}
Output
--------------------------
From derived class
Constructor in both base and derived class
- When there are default constructors in base as well as in derived class, the constructor of base is executed first and then constructor of derived class is executed, when an object of derived class is created.
Constructor in Multiple Inheritance
- First constructor of base class on the order how base class is inherited.
- Then only the constructor of derived class.
For parameterized constructor
#include
class alpha { int x; public: alpha(int i) { x = i; cout << "Alpha initialized"; } void show() { cout << x; } };
class beta { int y public: beta(int j) { y = j; cout << "Beta initialized"; } void show_y() { cout << y; } };
class gamma : public alpha, public beta { int m, n; public: gamma(int a, int b, int c, int d) : alpha(a), beta(b) { m = c; n = d; cout << "Gamma initialized"; } void show_mn() { cout << m << n; } };
int main() { gamma g(5, 10, 15, 20); g.show(); g.show_y(); g.show_mn(); return 0; }
Output ------------------------------ alpha initialized beta initialized gamma initialized 5 10 15 20
Destructors in Inheritance
- Calling of destructor is just in opposite sequence of that of the constructor.
Ⓒ Copyright ESign Technology 2019. A Product of ESign Technology. All Rights Reserved.