Você está na página 1de 8

Question - What are the different types of Inheritance?

Answer

* Single Inheritance
A (parent class) -> B (child class)
* Multiple Inheritance
A -> C, B -> C
* Hierarchical inheritance
A -> B, A -> C, A -> D
* Multilevel inheritance
A -> B, B -> C
* Hybrid inheritance
A -> B, A -> C, B -> D, C -> D

Question - What is a concrete derived class?


Answer
The derived class that implements the missing functionality of an abstract class is the
concrete derived class.

Question - Explain why and when do we use protected instead of private.


Answer
Private data members cannot be accessed outside the class. When a class inherits a base
class, all the data members except the private get inherited into it. So if we want data
members to be accessible to only derived classes and not privately or publicly accessible,
then we can use protected.

Question - What is a downcast?


Answer
A downcast is a cast from a base class to a class derived from the base class. A downcast
is only safe if the object addressed at runtime is actually addressing a derived class
object.

Define Derived class. Define concrete Derived class.


A derived class is a class that inherits the properties from its super class. For example, a
Cat is a super class and Monx cat is a derived class which has all properties of a Cat and
does not have a tail.
A concrete derived class is a derived class which implements the all functionality that are
missed in the super class.

Question - What is the object slicing?


Answer
In Inheritance, the attributes of the base class get carried to the derived class. However,
we can assign a base class with the derived class without having the contents of the
derived that are uncommon between then, copied to the base class.

Class B
{
public:
int i;
};

class D : public B
{
public:
int j;
};

int main()
{
B B1;
D D1;
B1 = D1; //only i is copied to B1
}

Question - Explain the use of Vtable.


Answer
If Base declares a member function and Derived declares a member function with same
name but different parameter types, then the Base function is "hidden" rather than
"overloaded" or "overridden" even if the Base function is virtual.

The solution to that is that a Derived must have a using declaration of the hidden member
function OR redefine the hidden Base member function(s), even if they are non-virtual.
Normally this re-definition merely calls the hidden Base member function using the ::
syntax.

Question - What is a concrete derived class?


Answer
The derived class that implements the missing functionality of an abstract class is the
concrete derived class.

Question - Explain why and when do we use protected instead of private.


Answer
Private data members cannot be accessed outside the class. When a class inherits a base
class, all the data members except the private get inherited into it. So if we want data
members to be accessible to only derived classes and not privately or publicly accessible,
then we can use protected.

Explain base class with an example using C++.


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”;
}
};

Explain Derived class with an example using C++.


Inheritance is one of the important feature 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 classes which inherit from other classes,
is referred as derived class.

For example, a general class vehicle can be inherited by more specific classes car and
bike. The classes car and bike are derived classes in this case.

class vehicle
{
int fuel_cap;
public:
drive();
};

class car : public class vehicle


{
public:
roll_windows();
};

class bike : public class vehicle


{
public:
kick_start();
};

What is Private inheritance? Provide an example using c++.


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;
}

What is Protected inheritance? Provide an example using c++.


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;
}

How do we implement inheritance in C++?


The inheritance feature in C++ is implemented in the following manner:
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.

Explain multiple inheritance with an example.


When a class is derived from another class ie it inherits functionalities of another class,
this phenomenon is known as inheritance. In some cases, a class can inherit from multiple
classes, ie a derived class can have multiple base classes, it is known as multiple
inheritance.
Consider following example:

class Base1
{
private:
int no1;
public:
void show1()
{
cout << “The no is:” << no1 <<”\n”;
}
};

class Base2
{
private:
int no2;
public:
void show2()
{
cout << “The no is:” << no2 <<”\n”;
}
};
class Derived: public Base1, public Base2 //Multiple inheritance
{
public:
void set(int x, int y)
{
no1 = x; no2 = y;
}
};

int main()
{
Derived ob;
ob.set(10, 20);
ob.show1(); // Derived class obj can access member functions of both base classes
ob2.show2(); //Derived class obj can access member functions of both base class
return 0;
}
More Questions:

Define Derived class.


How do we implement inheritance in C++?
Define concrete Derived class.
What is inheritance?
List the benefit of inheritance.
What is proper inheritance?
What is overloaded function?
What is an overridden function?
What is the hiding rule?
Is it possible to overload a virtual function?
Explain private and protected inheritance.
What are the access rules for private and protected inheritance?
Explain Base class with an example using C++.
Explain Derived class with an example using C++.
What is Private inheritance? Provide an example using c++.
What is Protected inheritance? Provide an example using c++.