C++ Inheritance Interview Preparation Guide
Optimize your C++ Inheritance interview preparation with our curated set of 20 questions. Our questions cover a wide range of topics in C++ Inheritance to ensure youre well-prepared. Whether youre new to the field or have years of experience, these questions are designed to help you succeed. Download the free PDF now to get all 20 questions and ensure youre well-prepared for your C++ Inheritance interview. This resource is perfect for in-depth preparation and boosting your confidence.20 C++ Inheritance Questions and Answers:
1 :: Explain Private Inheritance?
The Public and protected members of Base class become private members of the derived class.
2 :: Explain Public Inheritance?
All the public members and protected members are inherited as public and protected respectively.
3 :: Do you know what is Protected Inheritance?
Public and Protected members are derived as protected members.
4 :: Explain protected inheritance?
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;
}
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;
}
5 :: What is a base 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”;
}
};
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”;
}
};
6 :: 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.
Saves time and effort as the main code need not be written again.
7 :: How to implement inheritance in C++?
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.
{
...
};
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.
8 :: Do you know private inheritance?
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;
}
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;
}
9 :: What is base 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";
}
};
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";
}
};
10 :: Explain pure virtual functions?
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.
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.