C++ Inheritance
• Inheritance is one of the key features of Object-oriented programming in
C++.
• It allows us to create a new class (derived class) from an existing class
(base class).
• The derived class inherits the features from the base class and can have
additional features of its own.
• class Animal {
• // eat() function
• // sleep() function
• };
• class Dog : public Animal {
• // bark() function
• };
• Here, the Dog class is derived from the Animal class. Since Dog is derived
from Animal, members of Animal are accessible to Dog.
• Inheritance is an is-a relationship. We use inheritance only if an is-a
relationship is present between the two classes.
C++ Inheritance
• Here are some examples:
• A car is a vehicle.
• Orange is a fruit.
• A surgeon is a doctor.
• A dog is an animal.
• include <iostream>
• using namespace std;
• // base class
• class Animal {
• public:void eat() {
• cout << "I can eat!" << endl;
• }
• void sleep() {
• cout << "I can sleep!" << endl;
• }
• };
• // derived class
• class Dog : public Animal {
• public:
• void bark() {
• cout << "I can bark! Woof woof!!" << endl;
• }
• };
• int main() { // Create object of the Dog class
• Dog dog1;
// Calling members of the base class
• [Link]();
• [Link]();
• // Calling member of the derived class
• [Link]();
• return 0; }
Derived Classes
• A Derived class is defined as the class derived from the base class.
• The Syntax of Derived class:
• class derived_class_name :: visibility-mode base_class_name
• {
• // body of the derived class.
• }
• derived_class_name: It is the name of the derived class.
• visibility mode: The visibility mode specifies whether the
features of the base class are publicly inherited or
privately inherited. It can be public or private.
• base_class_name: It is the name of the base class.
Types Of Inheritance
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
• Hybrid inheritance
• Advantage of C++ Inheritance
• Code reusability: Now you can reuse the members of your parent class. So,
there is no need to define the member again. So less code is required in the
class.
• C++ Single Inheritance
• Single inheritance is defined as the inheritance in which a derived class is
inherited from the only one base class.
• Where 'A' is the base class, and 'B' is the derived class.
example
• #include <iostream>
• using namespace std;
• class Account {
• public:
• float salary = 60000;
• };
• class Programmer: public Account {
• public:
• float bonus = 5000;
• };
• int main(void) {
• Programmer p1;
• cout<<"Salary: "<<[Link]<<endl;
• cout<<"Bonus: "<<[Link]<<endl;
• return 0;
• }
• Output:
• Salary: 60000
• Bonus: 5000
C++ Multilevel Inheritance
• Multilevel inheritance is a process of deriving a class from another derived
class.
• When one class inherits another class which is further inherited by another
class, it is known as multi level inheritance in C++. Inheritance is transitive
so the last derived class acquires all the members of all its base classes
Multiple Inheritance
• Multiple inheritance is the process of deriving a new class that inherits the
attributes from two or more classes.
•
example of multi level inheritance in C++
• #include <iostream>
• using namespace std;
• class Animal {
• public:
• void eat() {
• cout<<"Eating..."<<endl;
• }
• };
• class Dog: public Animal
• {
• public:
• void bark(){
• cout<<"Barking..."<<endl;
• }
• };
• class BabyDog: public Dog
• {
• public:
• void weep() {
• cout<<"Weeping...";
• }
• };
• int main(void) {
• BabyDog d1;
• [Link]();
• [Link]();
• [Link]();
• return 0;
• }
• Output:
• Eating...
• Barking...
• Weeping...
Multiple inheritance
• #include <iostream>
• using namespace std;
• class A
• {
• protected:
• int a;
• public:
• void get_a(int n)
• {
• a = n;
• }
• };
•
• class B
• {
• protected:
• int b;
• public:
• void get_b(int n)
• {
• b = n;
• }
• };
• class C : public A,public B
• {
• public:
• void display()
• {
• std::cout << "The value of a is : " <<a<< std::endl;
• std::cout << "The value of b is : " <<b<< std::endl;
• cout<<"Addition of a and b is : "<<a+b;
• }
• };
• int main()
• {
• C c;
• c.get_a(10);
• c.get_b(20);
• [Link]();
•
• return 0;
• }
• Output:
• The value of a is : 10
• The value of b is : 20
• Addition of a and b is : 30
C++ Hybrid Inheritance
• Hybrid inheritance is a combination of more
than one type of inheritance.
• C++ Hierarchical Inheritance
• Hierarchical inheritance is defined as the
process of deriving more than one class from
a base class.
•
Hybrid Inheritance example:
• #include <iostream>
• using namespace std;
• class A
• {
• protected:
• int a;
• public:
• void get_a()
• {
• std::cout << "Enter the value of 'a' : " << std::endl;
• cin>>a;
• }
• };
•
• class B : public A
• {
• protected:
• int b;
• public:
• void get_b()
• {
• std::cout << "Enter the value of 'b' : " << std::endl;
• cin>>b;
• }
• };
• class C
• {
• protected:
• int c;
• public:
• void get_c()
• {
• std::cout << "Enter the value of c is : " << std::endl;
• cin>>c;
• }
• };
•
• class D : public B, public C
• {
• protected:
• int d;
• public:
• void mul()
• {
• get_a();
• get_b();
• get_c();
• std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
• }
• };
• int main()
• {
• D d;
• [Link]();
• return 0;
• }
Syntax of Hierarchical inheritance:
• class A
• {
• // body of the class A.
• }
• class B : public A
• {
• // body of class B.
• }
• class C : public A
• {
• // body of class C.
• }
• class D : public A
• {
• // body of class D.
• }
example:
• #include <iostream>
• using namespace std;
• class Shape // Declaration of base class.
• {
• public:
• int a;
• int b;
• void get_data(int n,int m)
• {
• a= n;
• b = m;
• }
• };
• class Rectangle : public Shape // inheriting Shape class
• {
• public:
• int rect_area()
• {
• int result = a*b;
• return result;
• }
• };
• class Triangle : public Shape // inheriting Shape class
• {
• public:
• int triangle_area()
• {
• float result = 0.5*a*b;
• return result;
• }
• };
• int main()
• {
• Rectangle r;
• Triangle t;
• int length,breadth,base,height;
• std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
• cin>>length>>breadth;
• r.get_data(length,breadth);
• int m = r.rect_area();
• std::cout << "Area of the rectangle is : " <<m<< std::endl;
• std::cout << "Enter the base and height of the triangle: " << std::endl;
• cin>>base>>height;
• t.get_data(base,height);
• float n = t.triangle_area();
• std::cout <<"Area of the triangle is : " << n<<std::endl;
• return 0;
• }