Inheritance is one of the key features of Object-oriented programming in C++. It allows user to create a new class (derived class) from an existing class(base class).
The derived class inherits all the features from the base class and can have additional features of its own.
Suppose, in your game, you want three characters – a guitar teacher, a cricketer and a businessman.
Since, all of the characters are persons, they can walk and talk. However, they also have some special skills. A Guitar teacher can teach Guitar, a Cricketer can play cricker and a businessman can run a business.
If we create three class :
1. guitar
2. cricker
3. Buisenessman
In each of the classes, you would be copying the same code for walk and talk for each character.
If you want to add a new feature – eat, you need to implement the same code for each character. This can easily become error prone (when copying) and duplicate codes.
It’d be a lot easier if we had a Person class with basic features like talk, walk, eat, sleep, and add special skills to those features as per our characters. This is done using inheritance.
Using inheritance, now you don’t implement the same code for walk and talk for each class. You just need to inherit them.
So, for Guitar teacher (derived class), you inherit all features of a Person (base class) and add a new feature Teachguitar. Likewise, for a cricketer, you inherit all the features of a Person and add a new feature PlayCricket and so on.
This makes your code cleaner, understandable and extendable.
class Person { ... .. ... }; class GuitarTeacher : public Person { ... .. ... }; class Cricketer : public Person { .... .. ... };
In the above example, class Person is a base class and classes GuitarTeacher and Cricketer are the derived from Person.
The derived class appears with the declaration of a class followed by a colon, the keyword public and the name of base class from which it is derived.
Since, GuitarTeacher and Cricketer are derived from Person, all data member and member function of Person can be accessible from them.
#include <iostream> using namespace std; class Person { public: string profession; int age; Person(): profession("unemployed"), age(16) { } void display() { cout << "My profession is: " << profession << endl; cout << "My age is: " << age << endl; walk(); talk(); } void walk() { cout << "I can walk." << endl; } void talk() { cout << "I can talk." << endl; } }; // GuitarTeacher class is derived from base class Person. class GuitarTeacher : public Person { public: void teachGuitar() { cout << "I can teach Guitar." << endl; } }; // Cricketer class is derived from base class Person. class Cricketer : public Person { public: void playCricket() { cout << "I can play Cricket." << endl; } }; int main() { GuitarTeacher teacher; teacher.profession = "Teacher"; teacher.age = 23; teacher.display(); teacher.teachGuitar(); Cricketer Cricketer; Cricketer.profession = "Cricketer"; Cricketer.age = 19; Cricketer.display(); Cricketer.playFootball(); return 0; }
My profession is: Teacher My age is: 23 I can walk. I can talk. I can teach Guitar. My profession is: Cricketer My age is: 19 I can walk. I can talk. I can play Cricket.
In this program, Person is a base class, while GuitarTeacher and Cricketer are derived from Person.
Person class has two data members – profession and age. It also has two member functions – walk() and talk().
Both GuitarTeacher and Cricketer can access all data members and member functions of Person.
However, GuitarTeacher and Cricketer have their own member functions as well: teachGuitar() and playFootball() respectively. These functions are only accessed by their own class.
In the main() function, a new GuitarTeacher object teacher is created.
Since, it has access to Person’s data members, profession and age of teacher is set. This data is displayed using the display() function defined in the Person class. Also, the teachGuitar() function is called, defined in the GuitarTeacher class.
Likewise, a new Cricketer object Cricketer is also created. It has access to Person’s data members as well, which is displayed by invoking the display() function. The playFootball() function only accessible by the Cricketer is called then after.
1) Single inheritance
2) Multilevel inheritance
3) Multiple inheritance
4) Hierarchical inheritance
5) Hybrid inheritance
In Single inheritance one class inherits one class exactly.
For example: Lets say we have class A and B
B inherits A // here A is base class and B is inheriting A properties
#include < iostream> using namespace std; class A { public: A(){ cout<<"Constructor of A class"<< endl; } }; class B: public A { public: B(){ cout<<"Constructor of B class"; } }; int main() { //Creating object of class B B obj; return 0; }
Constructor of A class Constructor of B class
Did you notice we have creat B class objects then in output we got A class output first why ?
Because B class extend A class so if we create object of B class then first A class Constructor will get call .
Remember this point exam will ask objective on this .
In this type of inheritance one class inherits another child class.
Example :
C inherits B and B inherits A
#include < iostream> using namespace std; class A { public: A(){ cout<<"Constructor of A class"<< endl; } }; class B: public A { public: B(){ cout<<"Constructor of B class"<< endl; } }; class C: public B { public: C(){ cout<<"Constructor of C class"<< endl; } }; int main() { //Creating object of class C C obj; return 0; }
Constructor of A class Constructor of B class Constructor of C class
See above code properly :
Class A – Base class
Class B – inherit A (means it has all properties of A )
Class C – inherit B (Which inherit A means C has all properties of B and A )
Thats why after creating object of C class first B class call from B class A class and thats why in our output first line is output from Class A .
In multiple inheritance, a class can inherit more than one class. This means that in this type of inheritance a single child class can have multiple parent classes.
Example :
C inherits A and B both
#include < iostream> using namespace std; class A { public: A(){ cout<<"Constructor of A class"<< endl; } }; class B { public: B(){ cout<<"Constructor of B class"<< endl; } }; class C: public A, public B { public: C(){ cout<<"Constructor of C class"<< endl; } }; int main() { //Creating object of class C C obj; return 0; }
Constructor of A class Constructor of B class Constructor of C class
Lets check above code carefully In Multilevel inheritance what happened :
C->B->A
Means C inherit B and B inherit A
But in multiple inheritance B not extending A class (This is the main difference keep in mind) But C class extend both A and B .
So after Create object of C class it will call A and then B class .
Thats why first is A output then B output .
Now I will give one excerices do a small change in above program :
Replace :
class C: public A, public B
With
class C: public B, public A
and lets what will be the output . this excerices for you solve it ..
In this type of inheritance, one parent class has more than one child class. For example:
Class B and C inherits class A
#include < iostream> using namespace std; class A //single base class { public: int x, y; void getdata() { cout << "\nEnter value of x and y:\n"; cin >> x >> y; } }; class B : public A //B is derived from class base { public: void product() { cout << "\nProduct= " << x * y; } }; class C : public A //C is also derived from class base { public: void sum() { cout << "\nSum= " << x + y; } }; int main() { B obj1; //object of derived class B C obj2; //object of derived class C obj1.getdata(); obj1.product(); obj2.getdata(); obj2.sum(); return 0; } //end of program
Enter value of x and y: 2 3 Product= 6 Enter value of x and y: 2 3 Sum= 5
In this example, there is only one base class A from which two class B and C are derived.
Both derived class have their own members as well as base class members.
The product is calculated in the derived class B, whereas, the sum is calculated in the derived class C but both use the values of x and y from the base class.
Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.
#include < iostream> using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; //base class class Fare { public: Fare() { cout<<"Fare of Vehicle\n"; } }; // first sub class class Car: public Vehicle { }; // second sub class class Bus: public Vehicle, public Fare { }; // main function int main() { // creating object of sub class will // invoke the constructor of base class Bus obj2; return 0; }
This is a Vehicle Fare of Vehicle
Note : Exam will ask some tricky objective question on inheritance so watch all videos then you can solve any problem very easily .
Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first the base class default constructor is executed and then the derived class’s constructor finishes execution.
Whether derived class’s default constructor is called or parameterised is called, base class’s default constructor is always called inside them.
To call base class’s parameterised constructor inside derived class’s parameterised constructo, we must mention it explicitly while declaring derived class’s parameterized constructor.
Above videos are more than enough for this day .
Think how can u implement inheritance here . Output looks like : Enter employee's basic info: Enter Name: Abhijeet Enter Emp. Id: 1121 Enter Gender: F Enter employee's department info: Enter Department Name: Testing Enter assigned work: Test Game OEM Enter time in hours to complete work: 70 Employee's Information is: Basic Information...: Name: Abhijeet Employee ID: 1121 Gender: F Department Information...: Department Name: Testing Assigned Work: Test Game OEM Time to complete work: 70 Note : implement multiple Inheritance .
Output : Enter an integer number: 10 Square of 10 is: 100 Enter an integer number: 20 Cube of 10 is: 8000
Note : Exam will ask some tricky objective question on inheritance so watch all videos then you can solve any problem very easily .