Multiple Constructor In C++
When
we create different-different type of class inside in a constructor to declare so that method is known as multiple
constructor in a class.
Fig:Multiple Constructor In C++
Fig:Multiple Constructor In C++
Syntax:
class
classname
{
public:
class
name()
{
}
classname(argument)
{
}
classname(classname
&objectname)
{
}
};
Program:
#include<iostream.h>
#include<conio.h>
class
BCA
{
private:
int
a,b,c;
public:
BCA()
{
cout<<”multiple
constructor”;
a=12;
b=23;
c=a+b;
}
BCA(int
x,int y,int z)
{
a=x;
b=y;
c=z;
}
BCA(BCA
&p)
{
a=p.a;
b=p.b;
c=a*b;
}
void
display()
{
cout<<a;
cout<<b;
cout<<c;
}
};
void
main()
{
BCA
a1;
BCA
a2(10,20,30);
BCA
a3(a2);
a1.display();
a2.display();
a3.display();
getch();
}
Output:
Multiple
constructor
12
23 35
10
20 30
10
20 200
No comments:
Post a Comment