C++ Inheritance | Find output programs | Set 3

This section contains the C++ find output programs with their explanations on C++ Inheritance (set 3).
Submitted by Nidhi, on July 05, 2020

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

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.

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

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.

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

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.