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!

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

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

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

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

How To Learn The Move Constructors In Modern C++?

Worth reading...
Learn to Use Object Arrays and Object Pointer Arrays in C++