C++ Program to using default constructor


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


Default constructor in C++

Test() { }


Example: How to implement default constructor in C++

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

class Test {
public:
   int a;

   Test() {
       cout << "Enter a Number :";
       cin >> a;
   }

   void display() {
       cout << "\nValue of a is :" << a;
   }
};

int main() {

   Test obj;
   obj.display();

   return 0;
}

Output:

Enter a Number :12
Value of a is :12