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

A Simple Example Program of Constructor In C++

A Simple Example Program of Constructor In C++

Constructors are distinguished from all other member functions by having the same name as the class they belong to. When an object of that class is created or is being copied, the appropriate constructor is called implicitly. If you are developing a C++ app of course using Object Oriented methods, Classes and Objects – the two main features of OOP. In this post, we teach you how you can create a constructor in a simple program.

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 – part of the class’s code and they are generally referred to as class members. Object-Oriented Programming (OOP) is a good way to work on data and work with functions in memory. Classes and Objects are the best way to work on properties and functions. Object-Oriented Programming has many advantages over procedural programming, and it is the most characteristic feature of the C++ programming language. 

What are Classes and Objects in C++?

Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint for the objects and they are user-defined data types that we can use in our program, and they work as an object constructor. 

Objects are an instantiation of a class. In C++ programming, most of the commands are associated with classes and objects, along with their attributes and methods. 

The Class is a type (T word in front of most classes represents Type i.e. TButton class), and an object is a clone block of that class (i.e. Button1), that has the same properties and methods of its class. We can give an example in real life, a Human can be a class which has attributes like name, age, weight, height properties, etc. and methods like walking, swimming, seeing, smelling, etc. and Worker is an Object of this class and it has all same attributes and methods of this Human class.

Is there a simple example program of constructor in C++?

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.

here is a simple program of constructor in C++

Constructors for global variables are called before the main function is called. Global variable constructors are also called prior to #pragma startup functions. Local objects are created as the scope of the variable becomes active. A constructor is also invoked when a temporary object of the class is created.

There are different contractor types and the Default Constructor in classes is one of these. This method is not only used in classes but also used with struct and union data types A Default Constructor is a constructor type in Classes that is called when class is defined with no arguments, or it is defined with an empty parameter list, or with default arguments provided for every parameter. A type with a public default constructor is Default Constructible;

Do you want to learn how to declare a Default Constructor Inside of Class Definition? In the definition of a default Constructor, class_name must name the current class or current instantiation of a class template. It must be a qualified class name when declared at namespace scope or in a friend declaration. Here are examples below to show how we declare a constructor in C++.

Here is a simple program example of default constructor in C++ (inside of a class definition)

We can declare a default constructor inside of a class. Just add class name with ( and )inside that class as below; create a method


An example of Declaration of a Default Constructor Inside of Class Definition with a class as follows,

This is a full example how a Constructor work in a class. Here we just create a new test object, and it automatically sets x=100 and calls the constructor method.

And the output will be:

A Simple Example Program of Constructor In C++ the C++ Builder Logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

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++?