C++C++11C++14C++17Introduction to C++Learn C++

Learn Constructors in C++ Classes

Object-Oriented Programming is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as a class member. Constructors are one of the members of classes, if you are asking what is a constructor in C++ ? or How you can create a constructor in C++, How you can call constructors? Here below we answer all these;

The Constructor in C++ is a function, a method in the class, but it is a ‘special method’ that is automatically called when an object of a class is created. We don’t need to call this function. Whenever a new object of a class is created, the Constructor allows the class to initialize member variables or allocate storage. This is why the name Constructor is given to this special method.

Creating a Constructor in C++

To create a Constructor, we should use the same name of the class, followed by ( and ) parentheses as below,

Calling a Constructor in C++

To call this contractor just create a new object with this class, it will be automatically called. Like this below,

Constructor Example in C++

Here is the full code about defining a constructor and creating a new object;

Note that , the Constructor has always the same name as the class and it is always public, and it does not have any return value, but a Constructor may have parameters.

Constructor with Parameters in C++

Constructors have no return value but may have parameters. In this example below we define a constructor to set name, age and height of any objects from THuman class,

Now we can create and construct worker1 object at the same time as below,

This is a good full example about The Constructor that creates and constructs two workers.

Constructor with Parameters Example in C++
Constructors has no return value but it may have parameters. In this example below we define a constructor to set name, age and height of any objects from THuman class,

Get started building powerful apps with C++Builder!

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

Learn about Declaration of a Copy Constructor

C++Game DevelopmentLearn C++

Learn to Develop a Text Based Simple Adventure Game in C++ on Windows

C++C++11C++14C++17Learn C++

Learn what is Trivial Default Constructor in C++

C++C++11C++14C++17Introduction to C++Learn C++

Learn to use Deleted Implicitly-Declared Default Constructor

Worth reading...
Discover Class Methods in C++
en_USEnglish