Single and Multilevel inheritance and Destructor in oop(object oriented programming)

 Single and Multilevel inheritance and  Destructors

Task 1:

Write a program in which class A is base class for class B. While Class C is derived from class B and class D is derived from class E. Provide explicit implementation of default constructors and destructors. Write a test program which initializes each class object and shows execution order of constructor and destructor by printing appropriate messages.

#include<iostream>
using namespace std;
class A{
public:
            A()
            {
                        cout << "Constructor of A" << endl;
            }
            ~A()
            {
                        cout << "Destructor of A" << endl;
            }
};
class B :public A{
public:
            B()
            {
                        cout << "Constructor of B with A" << endl;
            }
            ~B()
            {
                        cout << "Destructor of B with A" << endl;
            }
};
class C :public B{
public:
            C()
            {
                        cout << "Constructor of C with B" << endl;
            }
            ~C()
            {
                        cout << "Destructor of C with B" << endl;
            }
};
class E{
public:
            E()
            {
                        cout << "Constructor of E" << endl;
            }
            ~E()
            {
                        cout << "Destructor of E" << endl;
            }
};
class D :public E{
public:
            D()
            {
                        cout << "Constructor of D with E" << endl;
            }
            ~D()
            {
                        cout << "Destructor of D with E" << endl;
            }
};
int main()
{
            A a1; B b1; C c1; D d1; E e1;
            a1.~A(); b1.~B(); c1.~C(); d1.~D(); e1.~E();
            getchar();
            getchar();
            return 0;
}

        

          

       
 

Task 2:

Modify the above classes and provide overloaded constructor implementation for each class. (Note: Use base initialize for this purpose)

#include<iostream>
using namespace std;
class A{
protected:
            int a;
public:
            A()
            {
                        cout << "Constructor of A" << endl;
            }
            A(int x)
            {
                        a = x;
                        cout << "Constructor set the val of A is:"<<a<< endl;
            }
            ~A()
            {
                        cout << "Destructor of A" << endl;
            }
};
class B :public A{
public:
            B()
            {
                        cout << "Constructor of B with A" << endl;
            }
            B(int x) :A(x)
            {
                        a = x;
                        cout << "Constructor set the val of B is:" << a << endl;
            }
            ~B()
            {
                        cout << "Destructor of B with A" << endl;
            }
};
class C :public B{
public:
            C()
            {
                        cout << "Constructor of C with B" << endl;
            }
            C(int x) :B(x)
            {
                        a = x;
                        cout << "Constructor set the val of C is:" << a << endl;
            }
            ~C()
            {
                        cout << "Destructor of C with B" << endl;
            }
};
class E{
protected:
            int b;
public:
            E()
            {
                        cout << "Constructor of E" << endl;
            }
            E(int x)
            {
                        b = x;
                        cout << "Constructor set the val of E is:" << b << endl;
            }
            ~E()
            {
                        cout << "Destructor of E" << endl;
            }
};
class D :public E{
public:
            D()
            {
                        cout << "Constructor of D with E" << endl;
            }
            D(int x) :E(x)
            {
                        b = x;
                        cout << "Constructor set the val of D is:" << b << endl;
            }
            ~D()
            {
                        cout << "Destructor of D with E" << endl;
            }
};
int main()
{
            A a1; B b1; C c1; D d1; E e1; cout << endl << endl << endl;
            C(12); D(1); cout << endl << endl << endl;
            a1.~A(); b1.~B(); c1.~C(); d1.~D(); e1.~E();
            getchar();
            getchar();
            return 0;
}      

          

       
 


 

Task 3:

Declare classes Person and Student where Student is derived from Person class. Person has Name and Student has RollNo as its private member. Create a Student class object and initialize it with the already existing object of Student class. Analyze the behavior of default constructors of Person and Student classes.

#include<iostream>
#include<string>
using namespace std;
class person{
            string name;
public:
            person()
            {
                        name = '0';
                        cout << "Defaulr name is:" << name << endl;
            }
};
class Student :public person{
            int roll;
public:
            Student()
            {
                        roll = 0;
                        cout << "Default roll is:" << roll << endl;
            }
            Student(int x)
            {
                        roll = x;
                        cout << "Roll is updated:" << roll << endl;
            }
            void show()
            {
                        cout << "Value is:" << roll << endl;
            }
};
int main()
{
            person p1;
            Student s1(12),s2;
            s2 = s1;
            s2.show();
            getchar();
            getchar();
            return 0;
}       

          

       
 

Comments

Popular posts from this blog

Multiple inheritance,friend function and multiple file in oop(object oriented programming)

Concepts of OOP (object oriented programming)

Concepts in c++........