EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Constructor in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Constructor in C++

Constructor in C++

Introduction to Constructor in C++

The constructor may be defined as the special kind of method that has the name same as that of the class. The constructor gets invoked right after the object is initialized and is not required to call the constructor explicitly. Once the constructor is invoked, it assigns memory to the resources.

A constructor is a special class member function of a class that initializes objects i.e. class instance). In C++, Constructor has same name as the class itself. If object is created, Constructor is automatically called. Constructor can be defined either inside the class definition or outside the class definition using class name and scope resolution (::) operator.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

It makes the application more efficient and using it can be considered as a good approach to programming. It can be declared as similar to the other methods. The only way it is different from other methods in that class is due to the name of the function. Like other functions, values can be passed to the construction while initialization. It can be of any return type based on the requirement of the program. In this topic, we are going to learn about Constructors in C++.

Syntax

In order to implement the constructor in the program, one must have yo follow the correct syntax. The syntax has to be taken care of while initializing and declaring. Below is the syntax for declaration then we will look at the syntax for initialization.

1. Constructor Declaration:

ReturnType Construtor_name()
{
Statement 1;
Statement 2;
.
.
Statement n;
}

Here the return type defines the type of data that has to be returned from this method. If the data type mentioned is integer then the value that the constructor will return will be the integer value and in the same way, there can be any data type used over there. The statements of the default constructor will start executing right after the instance of the class has been created.

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,899 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

2. Calling Constructor:

ClassName objectName = new ClassName();
Or
ClassName objectName = new ClassName(argument1…. Argument n);

While calling the constructor, it might be needed to pass the value from where it has been called or it may not be required. Above are the syntax of both the ways to call the constructor. While bringing the constructor in use, one has to make sure that the syntax has to be followed correctly else it will lead to error.

How does Constructor Work in C++?

Before we can use a constructor in the program, we have to make sure to understand how it works so that we can use it correctly to make the program efficient. The way it works is very simple and contribute to the application for the betterment. The constructor is used in the program where we need to invoke a set of statements whenever the object for the particular class is created. The constructor is never required to be called explicitly.

Once you create the object or instance of the class, it will implicitly call the constructor and will execute a statement under that. In case if certain values have to be passed, you will need to pass the values at the point where the object of that class is being created. In the above section, we have discussed the syntax to pass the values to the constructor. It is the way the constructor works and it assists in making the program simple and efficient.

Types of Constructor in C++

Based on whether the values have to be passed to the constructor, it has been divided into two types: Default constructor and parameterized constructor. Below we will be discussing both the types.

1. Default Constructor –

The default constructor may be defined as the type of constructor in which no value has to be passed and can be simply invoked by creating an instance of that class. While declaring the default constructor, we just need to use the return type, the constructor name, and the brackets. There is no need to write and parameter between the brackets that come right after the constructor name. Below is how the default constructor looks like.

Int Employee();

In this example, there has been no value accepted and the return type is a string. What all it is required to call such a constructor is just the instance creation of that class.

2. Parameterized Constructor –

The parameterized constructor may be defined as the kind of constructor that needs some values to be passed in it so that those could be used by the variables defined in the constructor. The values that have to be passed can be mentioned by the time of the initialization of the variable. When it comes to the declaration of the parameterized constructor, we will need to use the return type, the constructor name, the brackets and the parameters that will be used to bring the values in. It is similar to the default constructor and the only thing it has, in addition, is the values.

Int Employee(int empID, Sting empName);

In this example of parameterized constructor declaration, the return type is an integer and there are two values that the constructor will be accepting. It can be noted that the values of any data type could be passed as a parameter.

Examples of C++ Constructor

In order to make the concept of constructor more precise, we will see an actual example of how constructor can be used in the real program. In the below example, we have used the default constructor that will be called when the object of the class has been created.

constructor in C++ example

In the above example, the class name is FirstExp and the constructor has been declared and defined within it. If the constructor invokes, it will lead to a print “constructor called”. In the main method, the object ‘f’ has been created for class FirstExp which invoked or called the default constructor and the output of the program was “constructor called”.

Conclusion

The constructor may be defined as the special feature of the programming languages which is used to make the program effective and efficient. It can also be considered as a special type of method that has the same name as that of the class and can be invoked whenever the object of that class is created. Based on the requirement of the constructor once can choose between the default and the parameterized constructor. It has to be understood that it can only be used in the case when there is something that has to be called immediately right after the instance of the class has been created.

Recommended Article

This is a guide to Constructor in C++. Here we discuss the Constructor Types and How does the Constructor in C++ works. You may also look at the following article to learn more –

  1. Python Frameworks
  2. C++ Commands
  3. Python Collections
  4. Constructor in C

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More