C++ Program For Constructor Overloading
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
//Program by call by reference
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class Test
{
int a,b,c,d;
public:
Test(int x,int y,int z)
{
cout<<"\nConstructor with 3 argument";
a=x;
b=y;
c=z;
}
Test(int x,int y)
{
cout<<"\nConstructor with 2 argument";
a=x;
b=y;
c=100;
}
Test(int x)
{
cout<<"\nConstructor with 1 argument";
a=x;
b=50;
c=100;
}
Test()
{
cout<<"\nConstructor without argument";
a=b=c=0;
}
public:
void add();
void sub();
};
void Test::add()
{
d=a+b+c;
cout<<"\nAddition is: "<<d;
}
void Test::sub()
{
d=a-b-c;
cout<<"\nsubtraction is: "<<d;
}
int main()
{
clrscr();
Test T1(50,30,20);
Test T2(10,20);
Test T3(5);
Test T4;
T1.add();
T2.add();
T3.add();
T4.add();
T1.sub();
T2.sub();
T3.sub();
T4.sub();
return 0;
}
Did you like this article? If Yes, please give DataFlair 5 Stars on Google

