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 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Generic ProgrammingLearn C++Templates

What Is A Template In C++?

C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

C++C++17Introduction to C++Learn C++Syntax

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++

Worth reading...
Learn About Function Declaration and Definition in C++