Simple program to create various types of constructor in one class
Simple Program To Create Various Types Of Constructor In One Class
#include<iostream.h>
#include<conio.h>
class ABC
{
int a,b;
char *str;
public:
ABC() //Default Constructor...
{
a=0;
b=0;
cout<<"A = "<<a<<"\tB = "<<b<<endl;
}
ABC(int x,int y) //Parameterized Constructor
{
a=x;
b=y;
cout<<"A = "<<a<<"\tB = "<<b<<endl;
}
ABC(ABC &i) //Copy Constructor
{
a=i.a;
b=i.b;
cout<<"A = "<<a<<"\tB = "<<b<<endl;
}
ABC(char s[]) //Dynamic Constructor
{
int n=strlen(s);
char *str=new char[n];
strcpy(str,s);
cout<<"your string is "<<str;
}
};
void main()
{
ABC A1;
ABC A2(10,20);
ABC A3(&A2);
ABC A4("CPP");
clrscr();
getch();
}
OUTPUT:-
A = 0 B = 0
A = 10 B = 20
A = 10 B = 20

0 comments: