
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
C++ - Default Constructors
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.
Default Constructors
C++ default constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are 0.
Syntax
Here is the following syntax for a default constructor:
class MyClass { public: MyClass() { // Default constructor body } };
Example of Default Constructor
A program demonstrating default constructors is given as follows:
#include <iostream> using namespace std; class DemoDC { private: int num1, num2 ; public: DemoDC() { num1 = 10; num2 = 20; } void display() { cout<<"num1 = "<< num1 <<endl; cout<<"num2 = "<< num2 <<endl; } }; int main() { DemoDC obj; obj.display(); return 0; }
Output
num1 = 10 num2 = 20
Explanation
- Here first created the Object of type DemoDC(), where the default constructor automatically invoked.
- It initializes the class members num1 and num2 with the values 10 and 20.
- Then the display() method prints these values.
Implicit vs Explicit Default Constructors
In object-oriented programming (OOP), depending on how a default constructor is declared, it can be divided into two categories, implicit and explicit.
1. Implicit Default Constructor
An implicit default constructor is a constructor that is automatically called by the complier when an object is created, it may also be invoked if the user passes arguments that would be convertible into the constructor's parameters.
Syntax
class MyClass { int x; double y; }; int main() { MyClass obj; // Implicit default constructor is called }
2. Explicit Default Constructor
An explicit default constructor is the one where the constructor is not allowed to be called implicitly by the compiler when performing automatic type conversions or copy-initialization. In other words, the user can only call directly when providing a matching argument.
Syntax
class ClassName { public: explicit ClassName(int x = 10); // Explicit constructor };
Default Constructor vs Parameterized Constructor
A default constructor is a constructor, which takes no parameter or has all its parameters set to default values.
Whereas a parameterized constructor is a constructor, which takes one or more arguments. It allows users to pass values during the creation of an object, which is further used for the initialization of an object.
In this tutorial, we will basically learn about the default constructor.
Syntax
Here is the following Syntax for the Default constructor.
class ClassName { public: // Default constructor ClassName() { // Initialization or setup code } };
Here is the following example for the Default constructor.
Explanation
- In this example, first, we created the class named DemoDC.
- In class, two private member variables num1 and num2 of type int are declared.
- then a default constructor is defined for the DemoDC class. Where this constructor is automatically called when an object of the class is created.
- In this constructor num1 and num2 are initialized to 100 and 20 respectively.
- In int main(), an object of the class DemoDC is created, which triggers the default constructor DemoDC().
- And obj.display() displays the values of num1 and num2.
When Default Constructor Called?
A default constructor is called in various situations, mainly when an object is created without arguments. Here we will discuss more situations where the default constructor is invoked.
- When an Object is created without arguments.
- When a class does not explicitly define any constructors.
- In derived classes, if the base class has a default constructor.
- When dynamically allocating an object.
Overloading the Default Constructor
In C++, overloading the default constructor is said to create multiple constructors within a class, where each constructor has a different parameter list, allowing for different ways of initializing objects.
Example
Here is the following example for Overloading the default Constructor in C++.
#include <iostream> using namespace std; class MyClass { public: int a, b; // Default constructor (no arguments) MyClass() : a(0), b(0) { cout << "Default constructor called" << endl; } // Overloaded constructor with one argument MyClass(int x) : a(x), b(0) { cout << "Overloaded constructor (1 argument) called" << endl; } // Overloaded constructor with two arguments MyClass(int x, int y) : a(x), b(y) { cout << "Overloaded constructor (2 arguments) called" << endl; } void display() { cout << "a: " << a << ", b: " << b << endl; } }; int main() { MyClass obj1; // Calls default constructor MyClass obj2(10); // Calls overloaded constructor (1 argument) MyClass obj3(10, 20); // Calls overloaded constructor (2 arguments) obj1.display(); obj2.display(); obj3.display(); return 0; }
Output
Default constructor called Overloaded constructor (1 argument) called Overloaded constructor (2 arguments) called a: 0, b: 0 a: 10, b: 0 a: 10, b: 20
Explanation
- Here, MyClass() is the default constructor, where initializing a and b to 0.
- MyClass(int x) is the constructor with one argument, where initializing a and b to x and 0 respectively.
- MyClass(int x, int y) is the constructor with two arguments, where initializing both a and b to x and y respectively.