C++ Program to using constructor overloading


In this program, You will learn how to implement constructor overloading in C++.


Constructor overloading in C++

Test() { }, Test( int ) { }


Example: How to implement constructor overloading in C++

Run it Now
#include<iostream>
using namespace std;

class Test {
   int x;

public:
   Test() {
       x = 10;
   }

   Test(int y) {
       x = y;
   }

   void display() {
     cout << "The value of x is :" << x << endl;
   }
};

int main() {

   Test t1;
   t1.display();

   Test t2(20);
   t2.display();

   return 0;
}

Output:

The value of x is :10
The value of x is :20