Derived Class - Inheritance

download Derived Class - Inheritance

of 8

Transcript of Derived Class - Inheritance

  • 8/14/2019 Derived Class - Inheritance

    1/8

    Question - What are the different types of Inheritance?

    Answer

    * Single Inheritance

    A (parent class) -> B (child class)

    * Multiple InheritanceA -> 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 theconcrete 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 datamembers to be accessible to only derived classes and not privately or publicly accessible,

    then we can use protected.

    Question - What is a downcast?

    AnswerA downcast is a cast from a base class to a class derived from the base class. A downcastis 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

  • 8/14/2019 Derived Class - Inheritance

    2/8

    {

    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 theconcrete derived class.

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

    AnswerPrivate 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

  • 8/14/2019 Derived Class - Inheritance

    3/8

    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

  • 8/14/2019 Derived Class - Inheritance

    4/8

    {

    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

    using namespace std;

    class base

    {int i, j;

    public:

    void setij(int a, int b){

    i = a;

    j = b;}

    void showij()

    {cout

  • 8/14/2019 Derived Class - Inheritance

    5/8

    //setij();

    k = i + j;

    }void showall()

    {

    cout

  • 8/14/2019 Derived Class - Inheritance

    6/8

    cout

  • 8/14/2019 Derived Class - Inheritance

    7/8

    Consider following example:

    class Base1{

    private:

    int no1;public:

    void show1()

    {cout

  • 8/14/2019 Derived Class - Inheritance

    8/8

    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++.