Inheritence in object oriented programming c++

5 comments 225 views

Why we use inheritence?

When we have more than one classes and the same code is repeating in all classes then it means we have been writing the same piece of code in all classes and we are wasting our time and effort by writing the same piece of code again and again in all classes. The solution is inheritance in object-oriented programming c++.

What is inheritence?

In inheritance, we make one class parent of the other class. So, one class is called a parent or base class and the other class is known as derived or child class. The child class can access all the public and protected data members and functions of the parent class.

Access Specifiers in inheritence

Private: If a class have private data members, it cannot be used in anyother class and it can be only used within the same class.

Public: If a class has public data members, it can be used within the same class and also outside the class anywhere, because it is public.

Protected: If a class has protected data members, it can be used within the same class and within its child classes. That’s why we make data members of the parent class as protected.

You should know:

Parent class is also known as Super Class or Base class.

Child class is also known as Sub class or Derived class.

Watch Video to learn the concepts of inheritence and solve the below questions for learning.

Click Here to take QUIZ of inheritance!

Exercise Questions:

Question1: Create a class “BASE” and print “Parent class” in its constructor and write a function which prints “I’m function of Base Class”.

Create another class “Derived” which inherits the above class  and print “Second Class” in its constructor and write a function in it which prints “I’m function of Derived Class”

Part (a): Create object of BASE class and call function of parent class.

Part (b): Create object of Derived class and call functions of both classes.

Question2: Create two classes named Mammals and MarineAnimals.

Create another class named BlueWhale which inherits both the above classes.

Now, create a function in each of these classes which prints “I am mammal”, “I am a marine animal” and “I belong to both the categories: Mammals as well as Marine Animals” respectively.

 Now, create an object for each of the above class and try calling

1 – function of Mammals by the object of Mammal

2 – function of MarineAnimal by the object of MarineAnimal

3 – function of BlueWhale by the object of BlueWhale

4 – function of each of its parent by the object of BlueWhale

Question3: Write down short answer the following questions:

  1. Why should we make functions of parent class protected instead of public?
  2. Can we call the functions of child class by the object of parent class? YES/NO
  3. Can we call functions of parent class by the object of child class?  YES/NO
  4. Suppose we have two classes, parent and child. Both classes have constructors in them. The child class is inherited from the parent class.

i. You have to tell which constructor will be called which object of the parent class is created?
ii. Which constructor will be called when object of the child class is created?

Click Here for Solution of above questions

You may also like

5 comments

rida January 11, 2021 - 11:42 am

Sir I understand inheritance very greatly. Now inheritance topic I memorize in my whole life Allah bless you .
sir plz aik video polymorphism and virtual function pr bhi bna dyn

Reply
Muneefa Malik January 24, 2021 - 6:42 am

Can’t open the solutions

Reply
Usman Malik March 29, 2021 - 12:29 pm

sol 2

#include
using namespace std ;

class Mammals{
public :
void Mammal(){
cout<<"I am Mammal ";
}

};

class Marine{
public :
void marine(){

cout<<"I am Marine "<<endl;

}
};

class Bluewhale : public Mammals,public Marine{
public :
void Whale(){

cout<<"I belong to both the categories: Mammals as well as Marine Animals ";
}
};

int main()
{
Mammals objMammal;

Marine objmarine;

Bluewhale objWhale ;

objWhale.Mammal();

objWhale.marine();

objWhale.Whale();
}

Reply
Usman Malik March 29, 2021 - 10:57 am

sol of Q no 1

#include
using namespace std ;

class Base
{

public :
Base(){

cout<<"Parent class "<<endl;
}
void Function()
{

cout<<"I am Function of Base class "<<endl;

}
};

class Second: public Base
{

public :
Second()
{

cout<<"Second Class "<<endl;

}
void Functn()
{

cout<<"I am Function of Second class "<<endl;
}

};

int main()
{

Base b1;
b1.Function();
Second s1;
b1.Function();
s1.Functn();

return 0 ;

}

Reply
Usman Malik March 29, 2021 - 12:27 pm

Q no 2 sol

#include
using namespace std ;

class Mammals{
public :
void Mammal(){
cout<<"I am Mammal ";

}

};

class Marine{
public :
void marine(){

cout<<"I am Marine "<<endl;

}

};

class Bluewhale : public Mammals,public Marine{
public :
void Whale(){

cout<<"I belong to both the categories: Mammals as well as Marine Animals ";

}

};

int main()
{
Mammals objMammal;

Marine objmarine;

Bluewhale objWhale ;

objWhale.Mammal();

objWhale.marine();

objWhale.Whale();

}

Reply

Leave a Comment