Advertisement

Default Constructor in C++

In this blog we are learning about constructor in C++ || Code with abuzar 


What is Default Constructor ?


What is Default constructor default constructor is a type of constructor which do not pass any type of parameters or arguments .


Constructors are functions of a class that are executed when new objects of the class are created. The constructors have the same name as the class and no return type, not even void. They are primarily useful for providing initial values for variables of the class. The two main types of constructors are default constructors and parameterized constructors. 


Example 

#include <iostream>
using namespace std;
class Demo {
   private:
   int num1, num2 ;
   public:
   Demo() {
      num1 = 10;
      num2 = 20;
   }
   void display() {
      cout<<"num1 = "<< num1 <<endl;
      cout<<"num2 = "<< num2 <<endl;
   }
};
int main() {
   Demo obj;
   obj.display();
   return 0;
}


Output :

num1 = 10
num2 = 20


In this above program , we are declaring the default parameter which can not pass any type of parameter o augments.



Please ! Write comment if you find anything incorrect in this website .

This article is contribute by Mohd Abuzar . If you like code with abuzar or want to contribute with us , So mail at - abuzar.abuzar.mohd325@gmail.com 


Post a Comment

0 Comments