Loading....
Coupon Accepted Successfully!

 

More Programs

11.32 Write a program to explain execution of constructor and destructor in single inheritance.
 

OUTPUT
Base Class Constructor.
Enter Father Name : Manoj
Derived Class Constructor.
Enter Child Name : Sanjay
Derived Class Destructor
Sanjay Manoj
Base Class Destructor.

Explanation: In the above program, two classes, class father and class child, are declared with one character data member each. Both the classes contain a constructor and a destructor. The class child is derived from the class father. Hence, the class father is the base class, and the class child is the derived class.

In function main(), C is an object of the derived class child. The constructor of the base class is executed first followed by the constructor of the derived class. However, the destructor of the derived class is executed first. After this, the destructor of the base class is executed. Here, the destructor of the derived class prints the contents of both the data members. The base class has no knowledge of the derived class. Hence, it cannot access the data members of the derived class.

11.33 Write a program to explain execution of constructor and destructor in multilevel inheritance.
 

OUTPUT
Constructor of class Grandfather
Enter Grandfather Name : x
constructor of class Father
Enter Father Name : y
Constructor of class Child
Enter Child Name : z
Destructor of class Child
Grand Father : x
Father : y
Child : z
Destructor of class Father
Destructor of class Grandfather
 
Explanation: In the above program, three classes are declared. They are grandfather, father, and child. Each one of them has a character data-type member. Each class contains a pair of a constructor and a destructor. The class father is derived from the class grandfather. Similarly, the class child is derived from the class father. The class grandfather is a base class of the class father. The class father is a base class of the class child. The class child is derived from the class father. Theclass father is an intermediate class that acts as a base class as well as a derived class. Constructors are executed from the base class to the derived class, and destructors are executed from the derived class to the base class.
 

11.34 Write a program to explain multilevel inheritance with member functions.

#include<iostream.h>

#include<conio.h>

// Multilevel Inheritance and member functions//

class Grandfather

{

private:

char gname[20];

public:

void getg()

{

cout<<“\n Enter Grandfather Name:”;

cin>>gname;

}

void showg()

{cout<<“\n Grandfather Name:”<<gname;}

};

class Father : public Grandfather

{

private:

char name[20];

public:

void getf()

{

cout<<“\n Enter Father Name:”;

cin>>name;

}

void showf()

{cout<<“\n Father Name:” <<name;}

};

class Child : public Father

{

private:

char cname[15];

public:

void getc()

{

getg();

getf();

cout<<“\n Enter Child Name:”;

cin>>cname;

}
void showc()
{
showg();
showf();
cout<<“\n Child:”<<cname;
}

};
int main()
{clrscr();

Child C;
C.getc();
C.showc();
return 0;

}

OUTPUT
Enter Grandfather Name : XXX
Enter Father Name : YYY
Enter Child Name : ZZZ
Grandfather Name : XXX
Father Name : YYY
Child : ZZZ
 
Explanation: The above program is similar to the previous one. Instead of constructor and destructor members, functions are used.
 

11.35 Write a program to explain how constructor and destructor are executed in multiple inheritance.

#include<iostream.h>

#include<conio.h>

class A

{

public:

A() {cout<<“\n Constructor of class A”;}

~A() {cout<<“\n Destructor of class A”;}

};

class B

{

public:

B() {cout<<“\n Constructor of class B”;}

~B() {cout<<“\n Destructor of class B”;}

};
class C
{

public:
C() {cout<<“\n Constructor of class C”;}
~C() {cout<<“\n Destructor of class C”;}

};
class D : public A,B,C
{

public:
D() {cout<<“\n Constructor of class D”;}
~D() {cout<<“\n\n Destructor of class D”;}

};
int main()
{

clrscr();
D x;
return 0;

}

OUTPUT
Constructor of class A
Constructor of class B
Constructor of class C
Constructor of class D
Destructor of class D
Destructor of class C
Destructor of class B
Destructor of class A
 
Explanation: In the above program the class D is a derived class from base classes A, B, and C. Each class contains a constructor and a destructor. This type of inheritance is called multiple inheritance. The classes A, B, and C are base classes. The execution of construction depends on the order of the base class given while deriving the class. Consider the statement class D: public A, B, C. Here, the execution of constructors starts from base class A to base class C followed by derived class D, If we change the order similar to the statement class D: public C, B, A, the execution of construction starts from class C to class A followed by derived class D.
 

11.36 Write a program to explain execution of constructor and destructor in hybrid inheritance.

#include<iostream.h>

#include<conio.h>

// Constructors and Hybrid Inheritance //
class A
{

public:
A() {cout<<“\n Constructor of class A”;}
~A() {cout<<“\n Destructor of class A”;}

};
class B : public A
{

public:
B() {cout<<“\n Constructor of class B”;}
~B() {cout<<“\n Destructor of class B”;}

};
class C
{

public:
C() {cout<<“\n Constructor of class C”;}
~C() {cout<<“\n Destructor of class C”;}

};
class D : public B,C
{

public:
D() {cout<<“\n Constructor of class D”;}
~D() {cout<<“\n\n Destructor of class D”;}

};
int main()
{

clrscr();
D x;
return 0;

}

OUTPUT
Constructor of class C
Constructor of class A
Constructor of class B
Constructor of class D
Destructor of class D
Destructor of class B
Destructor of class A
Destructor of class C

 

 
Explanation: The above program is similar to the previous one. The class B is derived from class A. The class D is derived from classes B and C. This type of inheritance is called hybrid inheritance. The execution of constructors is similar to multiple inheritance.
 

11.37 Write a program to demonstrate single inheritance.

#include<iostream.h>

#include<conio.h>

class A

{

protected:

int c;

public:

A() {c=0;}

A (int j) {c=j;}

void show() {cout<<endl<<“c=”<<c;}

void operator ++() {c++;}

};

class B : public A

{

public:

void operator --() {c--;}

};

int main()

{

clrscr();

B a;

++a;

a.show();

--a;

a.show();

return 0;

}

OUTPUT
c = 1
c = 0

Explanation:
In the above program, the class B is inherited from class A, and hence, the object of the derived class B can access the member functions of the base class. When the object is created, the constructor of the base class is executed. The derived class has no constructor. The member of the derived class can access the members of the base class if the access specifier is public or protected. A protected member can be accessed by the member function of a similar class or any class derived from it.
 

11.38 Write a program to access function derived from private inheritance.

#include<iostream.h>

#include<conio.h>

class B

{
public:
void one() {cout<<endl<<“one”;}
void two() {cout<<endl <<“two”;}
};
class D : private B
{
public:
B::one;
};
int main()
{
clrscr();
D d;
d.one();
//d.two(); // not accessible
return 0;
}
 
Explanation: In the above program, the class B has two member functions, one() and two(). The class D is derived using private inheritance. The public section of the derived class contains declaration B::one that allows the object of the derived class to access the function. The function one() is accessible, and the function two() is not accessible.
 

11.39 Write a program to derive classes using multilevel inheritance. Create a common constructor in the lowermost derived class.

#include<iostream.h>

#include<conio.h>

class A

{

public:

int x;

A() {}

};

class B : public A

{

public:

int y;

B() {}

};

class C : public B

{

public:

int z;
void show()
{
cout<<endl<<“x=”<<x <<“y=”<<y<<“z=”<<z;
}
C (int j,int k, int l)
{
x=j;
y=k;
z=l;
}

};
int main()
{

clrscr();
C c(4,7,1);
c.show();
return 0;

}

OUTPUT
x = 4 y =7 z = 1

 

 
Explanation: In the above program, classes A, B, and C are declared. The class B is inherited from the class A, and the class C is inherited from the class B. The classes A and B have a zero-argument constructor. The class C has three argument constructors and an initialized element of base classes A and B. Thus, the constructor defined in class C acts as a common constructor between these three classes.
 
In function main(), the variable c is an object of class C, and three integers are passed to it. The function show() is used to display the contents of the variables.
 

11.40 Write a program to explicitly call constructor of base classes in multiple inheritances.

#include<constream.h>

#include<iostream.h>

struct X

{

X (int a) // constructor

{

cout<<a;

}

};

struct Y // constructor
{

Y (int b) {cout<<b;}

};
struct Z : public X, public Y
{

Z (int p, int q,int r): X(p), Y(q) // constructor
{
cout<<r;
}

};
int main()
{

clrscr();
Z z(1,2,3); // Object of derived class
return 0;

}

OUTPUT
123

 

 
Explanation: The constructors of the base class are executed first followed by the derived class constructor. When a class is derived using multiple inheritance, the execution of base class constructors depends on the sequence of the base class given in the derivation statement. The statement is as follows:
 
struct Z : public X, public Y
 
Here, both X and Y are base classes, and the constructor of class X is executed first followed by the constructor of class Y.
 
Both base and derived classes contain a one-argument constructor. When an object is declared, three values are passed to it. In the derived class constructor, constructors of both the classes are explicitly invoked with one argument. Thus, arguments of base as well as derived classes are initialized using constructors.





Test Your Skills Now!
Take a Quiz now
Reviewer Name