Site icon DataFlair

Constructor Overloading with Default Arguments in C++

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

//Program by overloading 
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class Test
{
      int a,b,c;
      public:
      Test(int x,int y, int z=50)
      {   
          cout<<"\nConstructor with 3 Integer arguments";
             a=x;
             b=y;
             c=z;
      }
    Test(int x,int y)  
    {
           cout<<"\nConstructor with 2 Integer arguments";
             a=x;
             b=y;
             c=500;
    }
     void add();
};
void Test::add()
{
    cout<<"\nAddition is:"<<a+b+c;
}

int main()
{
     clrscr();
    Test T1(50,20);
    T1.add();
 return 0;
}

 

Exit mobile version