Sei sulla pagina 1di 6

Constructors in Derived Classes

The constructors play an important role in initializing objects. As long as no base class
constructor takes any arguments, the derived class need not have a constructor function.
However, if any base class contains a constructor with one or more arguments, then it is
mandatory for the derived class to have a constructor and pass the arguments to the base class
constructors. Remember, while applying inheritance we usually create objects using the derived
class, Thus, it makes sense for the derived class to pass arguments to the base class constructor.
When both the derived and base classes contain constructors, the base constructor is executed
first and then the constructor in the derived class is executed.

In ease of multiple inheritances, the base classes are constructed in the order in which they
appear in the declaration of the derived class. Similarly, in a multilevel inheritance, the
constructors will be executed in the order of inheritance.

Since the derived class takes the responsibility of supplying initial values to its base classes, we
supply the initial values that are required by all the classes together, when a derived class object
is declared. C++ supports a special argument passing mechanism for such situations.

The constructor of the derived class receives the entire list of values as its arguments and passes
them on to the base constructors in the order in which they are declared in the derived class. The
base constructors are called and executed before executing the statements in the body of the
derived constructor.

derived constructor (arg1, arg2,.argN, argD):

base1(arg1),
base2(arg2),
..
..
..
baseN(argN)
{
Body of derived constructor
}

The header line of derived Constructor function contains two parts separated by colon The first
part provides the declaration of the arguments that are passed to the derived constructor and the
second part lists the function calls to the base constructors.

Basel(arg1) base2(arglist2) ... are function calls to base constructors base 1(), base2() and
therefore argl, arg2t ... etc. represent the actual parameters that are passed to the base
constructors. argl through argN are the argument declarations for base constructors base1
through baseN. argD provides the parameters that are necessary to initialise the members of the
derived class.
Constructor in multiple inheritances
class alpha
{
int x;
public:
alpha(int a)
{
x=a;
cout<<we are in alpha<<endl;
}
void showx()
{
cout<<alpha<<x<<endl;
}
};
class beta
{
float y;
public:
beta(int b)
{
y=b;
cout<<we are in beta<<endl;
}
void showy()
{
cout<<beta<<y<<endl;
}
};
class gamma:public beta, public alpha
{
int m,n;
public:
gamma( int i, float j,int p, int q):
alpha(i),beta(j)
{
m=p;
n=q;
cout<<we are in gamma<<endl;
}
void showmn()
{
cout<<gamma m<<m<<endl;
cout<<gamma n<<n<<endl;
}
};
int main()
{
clrscr();
gamma g(5,10.5,6,7);
g.showx();
g.showy();
g.showmn();

return 0;
getch();
}

Constructor in multilevel inheritances

#include<iostream.h>
#include<conio.h>
class A
{
int a;
public:
A(int x)
{
a=x;
}
void sh()
{
cout<<"A"<<a<<endl;
}
};
class B:public A
{
int b;
public:
B(int k, int g):A(k)
{
b=g;
}
void dis()
{
cout<<"B"<<b<<endl;
}
};
class C:public B
{
int c;
public:
C( int l, int m, int n):B(l,m)
{
c=n;
}
void put()
{
sh();
dis();
cout<<"C"<<c<<endl;
}

};
void main()
{
clrscr();
C c1(10,20,30);
c1.put();
getch();
}

/*PROGRAM TO IMPLEMENT HIERARCHICAL INHERITANCE USING


PARAMETERIZED CONSTRUCTOR IN CLASSES*/
#include<conio.h>
#include<iostream.h>
#include<string.h>
class base
{
char name[15];
int age;
public:
base(char n[15],int a)
{
strcpy(name,n);
age=a;
}
void show()
{
cout<<Name: <<name<<endl;
cout<<Age: <<age<<endl;
}
~base()
{
cout<<Destructor of base class is executed<<endl;
}
};
class derived1:public base
{
int salary;
public:
derived1(char n[15],int a,int s):base(n,a)
{
salary=s;
}
void show1()
{
cout<<Salary: <<salary<<endl;
}
~derived1()
{
cout<<Destructor of derived1 class is executed<<endl;
}
};
class derived2:public base
{
int salary;
public:
derived2(char n[15],int a,int s):base(n,a)
{
salary=s;
}
void show2()
{
cout<<Salary: <<salary<<endl;
}
~derived2()
{
cout<<Destructor of derived2 class is executed<<endl;
}
};
void main()
{
clrscr();
cout<<Data for first person:<<endl;
{
derived1 d1(abc,21,25000);
d1.show();
d1.show1();
}
cout<<data for second person:<<endl;
{
derived2 d2(xyz,25,30000);
d2.show();
d2.show2();
}
getch();
}

Potrebbero piacerti anche