Inheritance PART2
Inheritance PART2
In case of inheritance where both base and derived class are having
constructors, when an object of derived class is created then___________ .
A. constructor of derived class will be invoked first
B. constructor of base class will be invoked first
C. constructor of derived class will be executed first followed by base class
D. constructor of base class will be executed first followed by derived class
Q2
If base class has constructor with arguments, then it will be ________________ for the
derived class to have constructor and pass the arguments to base class constructor.
A. Optional
B. Mandatory
C. Compiler dependent
D. Compiler Error Situation
Q3
In case of inheritance where both base and derived class are having constructor and
destructor, then which if the following are true ?
1. Constructors are executed in their order of derivation
2. Constructors are executed in reverse order of derivation
3. Destructors are executed in their order of derivation
4. Destructors are executed in reverse order of derivation
A. Only 2 ,4
B. Only 1 , 3
C. Only 1 , 4
D. Only 2, 3
Q4
#include <iostream>
using namespace std;
class Base
{
public:
Base() { cout << "Base"; }
};
class Derived : public Base
{
public:
Derived(int i) { cout << i; }
};
int main()
{
Derived d2(10);
return 0;
}
A. Base10
B. 10Base
C. Base
D. 10
Order of execution of constructor and destructor during Multilevel inheritance
Program-Order of execution of constructor and destructor during Multilevel
inheritance
#include<iostream> class B:public A
using namespace std; {
class A int l,m;
{ public:
int x,y; B(int p,int q,int r,int s):A(r,s)
public: {
A(int r,int s) l=p;
{ m=q;
x=r; cout<<"\nCalling derived class B constructor:"<<l<<"
y=s; "<<m;
cout<<"\nCalling base class constructor:"<<x<<" "<<y; }
} ~B()
~A() {
{ cout<<"\nCalling derived B class destructor";
cout<<"\nCalling base class destructor"; }
} };
};
Program-Order of execution of constructor and destructor during Multilevel
inheritance….Continued
class C:public B
{
int n,m;
public:
C(int u,int v,int p,int q,int r,int s):B(p,q,r,s)
{
n=u;
m=v;
cout<<"\nCalling derived class C constructor with
values:"<<n<<" "<<m;
}
~C()
{
cout<<"\nCalling derived class C destructor";
}
};
int main()
{
C obj1(1,2,3,4,5,6);
return 0;
}
Order of execution of constructor and destructor during Multiple inheritance
}
};