
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Does C++ compiler create default constructor when we write our own?
In this tutorial, we will be discussing a program to understand if the C++ compiler creates a default constructor when we write our own.
Generally, the C++ compiler uses the default constructor when no one is defined, but always uses the one defined by the user if any.
Example
#include<iostream> using namespace std; class myInteger{ private: int value; //other functions in class }; int main(){ myInteger I1; getchar(); return 0; }
Output
Compiles successfully
Example
#include<iostream> using namespace std; class myInteger{ private: int value; public: myInteger(int v) //user-defined constructor { value = v; } //other functions in class }; int main(){ myInteger I1; getchar(); return 0; }
Output
Gives error about user-defined constructor not being defined
- Related Articles
- When should we write our own assignment operator in C++?
- When should we write our own assignment operator in C++ programming?
- How can we create our own choice MySQL database?
- When we eat our food, where does the food goes?
- How to create a default constructor in Java?
- How to create our own Listener interface in android?
- C++ default constructor
- Java default constructor
- Does any part of our body vibrate when we speak? Name the part.
- How to create our own/custom functional interface in Java?\n
- Default constructor in C#
- Default constructor in Java
- Can we initialize static variables in a default constructor in Java?
- Why do we close our eyes when we sneeze?
- Why do we need a copy constructor and when should we use a copy constructor in Java?

Advertisements