What is a constructor in C++?

A constructor in C++ is a special type of function that is automatically called when an object of a class is created. The main purpose of a constructor is to initialize the member variables of the class. Constructors have the same name as the class and do not have a return type.

Key Features of Constructors:

  1. Automatic Invocation: Constructors are called automatically when an object is created.
  2. No Return Type: Constructors do not have a return type, not even void.
  3. Same Name as Class: The name of the constructor must be the same as the class name.

Types of Constructors in C++:

1. Default Constructor

A default constructor is a constructor that either has no parameters or has parameters with default values. If no constructor is provided by the user, C++ provides an implicit default constructor.

class MyClass { public: MyClass() { // Default constructor std::cout << "Object created!" << std::endl; } };

2. Parameterized Constructor

A constructor that takes arguments to initialize objects with specific values is known as a parameterized constructor.

class MyClass { private: int x; public: MyClass(int a) { // Parameterized constructor x = a; } };

3. Copy Constructor

A copy constructor is used to initialize a new object as a copy of an existing object. The default copy constructor performs a shallow copy, but you can define your own to perform deep copying.

class MyClass { private: int x; public: MyClass(const MyClass &obj) { // Copy constructor x = obj.x; } };

Use Cases of Constructors:

  • Initialization: Constructors are primarily used to initialize object data when they are created.
  • Resource Allocation: In more complex applications, constructors may allocate resources such as dynamic memory.

Example:

#include <iostream> using namespace std; class Car { public: string brand; int year; // Parameterized constructor Car(string b, int y) { brand = b; year = y; } // Display function void display() { cout << "Brand: " << brand << ", Year: " << year << endl; } }; int main() { Car car1("Toyota", 2020); car1.display(); // Outputs: Brand: Toyota, Year: 2020 return 0; }

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Who is CEO of OpenAI?
Which MongoDB interview questions to prepare for 10 years experience?
What are the tips for coding interviews at fintech companies?
What is the difference between abstraction and encapsulation?
What coding script does Apple use?
When to use recursive mutex?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.