1⟩ Explain the advantages of inheritance?
Allows the code to be reused as many times as needed. The base class once defined and once it is compiled, it need not be reworked.
Saves time and effort as the main code need not be written again.
“C++ Inheritance frequently Asked Questions by expert members with experience in C++ inheritance. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts”
Allows the code to be reused as many times as needed. The base class once defined and once it is compiled, it need not be reworked.
Saves time and effort as the main code need not be written again.
class Square: public Shape
{
...
};
In the above example all public members of Shape can be reused by the class Square. If public key word is left, private inheritance takes place by default. If protected is specified the inheritance is applied for any descendant or friend class.
Inheritance is one of the important features of OOP which allows us to make hierarchical classifications of classes. In this, we can create a general class which defines the most common features. Other more specific classes can inherit this class to define those features that are unique to them. In this case, the class from which other classes are inherited is referred as base class.
For example, a general class vehicle can be inherited by more specific classes car and bike. The class vehicle is base class in this case.
class Base
{
int a;
public:
Base()
{
a = 1;
cout <<”inside Base class”;
}
};
class Derived:: public Base //class Derived is inheriting class Base publically
{
int b;
public:
Derived()
{
b = 1;
cout <<”inside Derived class”;
}
};
When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class. When inheritance is protected:
Private members of base class are not accessible to derived class.
Protected members of base class remain protected in derived class.
Public members of base class become protected in derived class.
#include <iostream>
using namespace std;
class base
{
protected:
int i, j;
public:
void setij(int a, int b)
{
i = a;
j = b;
}
void showij()
{
cout <<”nI:”<<i<<”n J:<<j;
}
};
class derived : protected base
{
int k;
public:
void setk()
{
setij();
k = i + j;
}
void showall()
{
cout <<”nK:”<<k<<show();
}
};
int main()
{
derived ob;
//ob.setij(); // not allowed. Setij() is protected member of derived
ob.setk(); //ok setk() is public member of derived
//ob.showij(); // not allowed. Showij() is protected member of derived
ob.showall(); // ok showall() is public member of derived
return 0;
}
Public and Protected members are derived as protected members.
All the public members and protected members are inherited as public and protected respectively.
Overloading helps to create different behaviors of methods with the same name and scope. For instance we can overload a method to return float values and integer values.
Overriding on the other hand changes the behavior of a class to make it behave different than its parent class.
When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class. When inheritance is private:
i. Private members of base class are not accessible to derived class.
ii. Protected members of base class become private members of derived class.
iii. Public members of base class become private members of derived class.
#include <iostream>
using namespace std;
class base
{
int i, j;
public:
void setij(int a, int b)
{
i = a;
j = b;
}
void showij()
{
cout <<"nI:"<<i<<"n J:"<<j;
}
};
class derived : private base
{
int k;
public:
void setk()
{
//setij();
k = i + j;
}
void showall()
{
cout <<"nK:"<<k<<show();
}
};
int main()
{
derived ob;
//ob.setij(); // not allowed. Setij() is private member of derived
ob.setk(); //ok setk() is public member of derived
//ob.showij(); // not allowed. Showij() is private member of derived
ob.showall(); // ok showall() is public member of derived
return 0;
}
A pure virtual function is a function which has no definition in the base class. Its definition lies only in the derived class ie it is compulsory for the derived class to provide definition of a pure virtual function. Since there is no definition in the base class, these functions can be equated to zero.
The general form of pure virtual function is:
virtual type func-name(parameter-list) = 0;
Consider following example of base class Shape and classes derived from it viz Circle, Rectangle, Triangle etc.
class Shape
{
int x, y;
public:
virtual void draw() = 0;
};
class Circle: public Shape
{
public:
draw()
{
//Code for drawing a circle
}
};
class Rectangle: public Shape
{
Public:
void draw()
{
//Code for drawing a rectangle
}
};
class Triangle: public Shape
{
Public:
void draw()
{
//Code for drawing a triangle
}
};
Thus, base class Shape has pure virtual function draw(); which is overridden by all the derived classes.
Defining a function in the derived class with same name as in the parent class is called overriding. In C++, the base class member can be overridden by the derived class function with the same signature as the base class function. Method overriding is used to provide different implementations of a function so that a more specific behavior can be realized.
The Public and protected members of Base class become private members of the derived class.
Public and Protected members are derived as protected members.
Pure virtual functions are also called 'do nothing functions'.
e.g. virtual void abc() = 0;
When a pure virtual fnction is declared in the base class, the compiler necessitates the derived classes to define those functions or redeclare them are pure virtual functions. The classes containing pure virtual functions cannot be used to declare objects of their own. Such classes are called as abstract base classes.
Inheritance is one of the important features of OOP which allows us to make hierarchical classifications of classes. In this, we can create a general class which defines the most common features. Other more specific classes can inherit this class to define those features that are unique to them. In this case, the class from which other classes are inherited is referred as base class.
For example, a general class vehicle can be inherited by more specific classes car and bike. The class vehicle is base class in this case.
class Base
{
int a;
public:
Base()
{
a = 1;
cout <<"inside Base class";
}
};
class Derived:: public Base //class Derived is inheriting class Base publically
{
int b;
public:
Derived()
{
b = 1;
cout <<"inside Derived class";
}
};
If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor.
Hence, by making the destructor in the base class virtual, we ensure that the derived class destructor gets called before the base class destructor.
class a
{
public:
a(){printf("nBase Constructorn");}
~a(){printf("nBase Destructorn");}
};
class b : public a
{
public:
b(){printf("nDerived Constructorn");}
~b(){printf("nDerived Destructorn");}
};
int main()
{
a* obj=new b;
delete obj;
return 0;
}
Output:
Base Constructor
Derived Constructor
Base Destructor
By Changing
~a(){printf("nBase Destructorn");}
to
virtual ~a(){printf("nBase Destructorn");}
Output:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class. When inheritance is private:
i. Private members of base class are not accessible to derived class.
ii. Protected members of base class become private members of derived class.
iii. Public members of base class become private members of derived class.
#include <iostream>
using namespace std;
class base
{
int i, j;
public:
void setij(int a, int b)
{
i = a;
j = b;
}
void showij()
{
cout <<”nI:”<<i<<”n J:”<<j;
}
};
class derived : private base
{
int k;
public:
void setk()
{
//setij();
k = i + j;
}
void showall()
{
cout <<”nK:”<<k<<show();
}
};
int main()
{
derived ob;
//ob.setij(); // not allowed. Setij() is private member of derived
ob.setk(); //ok setk() is public member of derived
//ob.showij(); // not allowed. Showij() is private member of derived
ob.showall(); // ok showall() is public member of derived
return 0;
}
All the public members and protected members are inherited as public and protected respectively.
The Public and protected members of Base class become private members of the derived class.
Allows the code to be reused as many times as needed. The base class once defined and once it is compiled, it need not be reworked.
Saves time and effort as the main code need not be written again.
When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class. When inheritance is protected:
Private members of base class are not accessible to derived class.
Protected members of base class remain protected in derived class.
Public members of base class become protected in derived class.
#include <iostream>
using namespace std;
class base
{
protected:
int i, j;
public:
void setij(int a, int b)
{
i = a;
j = b;
}
void showij()
{
cout <<"nI:"<<i<<"n J:<<j;
}
};
class derived : protected base
{
int k;
public:
void setk()
{
setij();
k = i + j;
}
void showall()
{
cout <<"nK:"<<k<<show();
}
};
int main()
{
derived ob;
//ob.setij(); // not allowed. Setij() is protected member of derived
ob.setk(); //ok setk() is public member of derived
//ob.showij(); // not allowed. Showij() is protected member of derived
ob.showall(); // ok showall() is public member of derived
return 0;
}