Q:

C++ Inheritance | Find output programs | Set 3

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Inheritance (set 3).

Program 1:

#include <iostream>
#include <string.h>
using namespace std;

class Class1 {
public:
    ~Class1()
    {
        cout << "Class1 dtor called " << endl;
    }
};

class Class2 : public Class1 {
public:
    ~Class2()
    {
        cout << "Class2 dtor called " << endl;
    }
};

class Class3 : public Class2 {
public:
    ~Class3()
    {
        cout << "Class3 dtor called " << endl;
    }
};

int main()
{
    Class3 OB;
    return 0;
}

Program 2:

#include <iostream>
#include <string.h>
using namespace std;

class Class1 {
public:
    ~Class1()
    {
        cout << "Class1 dtor called " << endl;
    }
};

class Class2 : public Class1 {
public:
    ~Class2()
    {
        cout << "Class2 dtor called " << endl;
    }
};

class Class3 : Class1, Class2 {
public:
    ~Class3()
    {
        cout << "Class3 dtor called " << endl;
    }
};

int main()
{
    Class3 OB;
    return 0;
}

Program 3:

#include <iostream>
#include <string.h>
using namespace std;

class Class1 {
protected:
    void fun1()
    {
        cout << "Fun1() called" << endl;
    }
};

class Class2 : public Class1 {
public:
    void fun2()
    {
        cout << "Fun2() called" << endl;
    }
};

class Class3 : Class2 {
public:
    void fun3()
    {
        cout << "Fun3() called" << endl;
    }
};

int main()
{
    Class3 OB;
    OB.fun1();
    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

Class3 dtor called
Class2 dtor called
Class1 dtor called

Explanation:

Here, we created three classes Class1, Class2, and Class3, each of them is containing destructor.

Here, Class1 is inherited by Class2 and Class2 inherited by Class3. This type of inheritance is known as multi-level inheritance.

In main() function, here we created the object of Class3. There are no more statements in the main() function. So when the scope of the object gets finished then destructors will be called.

In the C++, Destructors in the inheritance called in the reverse direction. Derive class destructor will call before the base class destructor that is why the destructor of Class3 will be called before the destructor of Class2 and the destructor of Class2 will be called before the destructor of Class1.

Answer Program 2:

Output:

Class3 dtor called
Class2 dtor called
Class1 dtor called
Class1 dtor called

Explanation:

Here, we created three classes Class1, Class2, and Class3, each of them is containing destructor.

Here Class1 is inherited by Class2 and Class1, Class2 inherited by Class3. This type of inheritance is known as “multiple” inheritance.

In the main() function, here we created the object OB of the class Class3. There are no more statements in the main() function. So when the scope of the object gets finished then destructors will be called.

In C++, Destructors in the inheritance called in the reverse direction. Derive class destructor will call before the base class destructor.

Here, first of all destructor of Class3 will be called, and Class3 inherits Class1 and Class2. Then Class2 destructor will be called and Class2 inherits Class1 then Class1 destructor will be called and Class3 also inherit Class1 then Class1 destructor will be called one more time.

Answer Program 3:

Output:

main.cpp: In function ‘int main()’:
main.cpp:32:13: error: ‘void Class1::fun1()’ is protected within this context
     OB.fun1();
             ^
main.cpp:7:10: note: declared protected here
     void fun1()
          ^~~~
main.cpp:32:13: error: ‘Class1’ is not an accessible base of ‘Class3’
     OB.fun1();
             ^

Explanation:

It  will generate an error, because protected members of the class can be accessed in the derived class, here in the main() function we created the object of Class3 and trying to access the member function of  Class1 that’s why fun1() function is in-accessible using the object of Class3.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Inheritance | Find output programs | Set 4... >>
<< C++ Inheritance | Find output programs | Set 2...