How to Define the Default Constructor in C++? Last Updated : 29 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a constructor that takes no parameters is called a default constructor. A default constructor gets automatically invoked when an object of a class is created without any arguments. It is mainly used to initialize legal initial values to the member variables or perform other setup tasks. In this article, we will look at how to define a default constructor in C++ Defining a Default Constructor in C++To define a default constructor explicitly, you must create a member function with the same name as the class with no parameters. In simple terms, the default constructor is defined like any other constructor but without parameters. Syntax to Define a Default Constructorclass ClassName { public: ClassName(); // Other class members };C++ Program to Define a Default Constructor C++ // C++ program to define a Default constructor #include <iostream> using namespace std; class MyClass { public: // Default constructor MyClass() { cout << "Default constructor called\n"; } }; int main() { // Creating an object of MyClass MyClass obj; return 0; } OutputDefault constructor called Explanation: In the above example, We have "MyClass" that has a default constructor. When an object of MyClass is created then the default constructor is invoked. We can also write initialization code inside the default class "MyClass". Comment More infoAdvertise with us Next Article How to Declare a Copy Constructor in a Derived Class? R rudra1807raj Follow Improve Article Tags : C++ Programs C++ cpp-constructor C++-Constructors Constructors CPP Examples +2 More Practice Tags : CPP Similar Reads How to Define a Move Constructor in C++? In C++, we have a move constructor which is a part of C++11 move semantics and is used to handle resources for temporary and rvalue objects. In this article, we will learn how to write a move constructor in C++. How to Write Move Constructor in C++?The move constructor is defined similarly to a copy 2 min read How to Declare a Copy Constructor in a Derived Class? In C++, a copy constructor is a constructor that initializes an object as a copy of an existing object of the same class. In this article, we will learn how to declare a copy constructor in a derived class. Declaring a Copy Constructor in a Derived ClassWe can declare a copy constructor in a derived 3 min read How to Handle Incorrect Values in a Constructor? In C++, constructors are used to construct and initialize the object of its class. If incorrect values are given to the constructor, then it may cause inconsistency in a program. In this article, we will learn how to handle incorrect values in a constructor in C++ Handling Incorrect Arguments for Co 2 min read How to Create a Class with Constructors and Destructors in C++? In C++, a class is a user-defined data type encapsulating data members and member functions. The constructors and destructors are special member functions of a class used for initializing objects and cleaning up resources respectively. In this article, we will discuss how to create a class with cons 2 min read How to Create a Template Class in C++? In C++, template classes are used to create generic classes that can work for different data types. In this article, we will learn how to create a template class in C++. Create a Template Class in C++ To create a template class in C++, we can follow the below syntax: Syntax of Template Classtemplate 2 min read How to Create a shared_ptr in C++? A std::shared_pointer is a smart pointer introduced in C++11 that manages the lifetime of a dynamically allocated object through reference counting. In this article, we will learn how to create a shared_pointer. shared_ptr in C++A std::shared_pointer can be created by using the std::make_shared meth 2 min read Like