C++ Program to using constructor overloading


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


Constructor overloading in C++

Test() { 
    //statement
}
Test(int) { 
     //statement
}

Example: How to implement constructor overloading in C++.

#include<iostream>
using namespace std;

class Test {
   int x;

public:
   Test() {
       x = 10;
   }

   Test(int y) {
       x = y;
   }

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

int main() {

   Test t1;
   t1.display();

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

   return 0;
}

Output:

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