Constructor in derived classes

As we know,the constructor play in important role in initializing object.We did not use them earlier in the derived classes for the sake of simplicity.

one important thing to note here is that, 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 argument,then it is mandatory for the derived class to have a constructor and pass the argument to the base class constructor.

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 case of multiple inheritance,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 constructor will be executed in the order 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 the classes together,when a derived class object is declared.How are they passed to the base class constructor so that they can do their job? 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.

The general form of defining a derived constructor is:





The header line of derived-constructor function contains two parts separated by a 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.

base1(arglist1),base2(arglist2)...are function calls to base constructor base1(),base2(),....and therefore arglist1,arglist2..etc.
represent the actual parameters that are passed to the base constructor. arglist1 through arglistN are the argument declarations for base constructor base1 through baseN. arglistD provides the parameters that are necessary to initialize the member of the derived class.




EXAMPLE:

D(int a1, int a2, float b1 ,float b2, int d1);
A(a1, a2);     /* call to constructor A */
B(b1, b2);    /*call to constructor B */
{
     d=d1;       // executes its own body
}

A(a1,a2) invokes the base constructor A() and B(b1,b2) invokes another base constructor B(). The constructor D() supplies the values for these four argument.In addition,it has one argument of its own.The constructor D() has a total of five argument.D() may be invoked as follows:

.......
D objD(5,12,2.5,7.54,30);
.......

These values are assigned to various parameters by the constructor D() follows:

5 =a1
12=a2
2.5=a3
7.54=a4
30=d1






program with constructor in derived class:

#include<iostream>
using namespace std;
class A
{
private:
int x;
public:
A(int a)
{
x=a;
cout<<"A initialize \n";
}
void show1(void)
{
cout<<"x="<<x<<"\n";
}
};
class B
{
private:
float y;
public:
B(float b)
{
y=b;
cout<<"B initialize \n";
}
void show2(void)
{
cout<<"y="<<y<<"\n";
}
};
class C : public A,public B
{
private:
int m,n;
public:
C(int r,float s,int t,int u) : A(r),B(s)
{
m=r;
n=s;
cout<<"C initialize \n";
}
void show3(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};
int main()
{
C c(5,10.75,20,30);
cout<<"\n";
c.show1();
c.show2();
c.show3();
return 0;

}

output:


0 comments:

Copyright © 2013 free coding