This document will delve into the concept of constructors in C++, including default and
parameterized constructors, and demonstrate constructor overloading with examples. I'll
prepare both DOCX and PDF versions, maintaining anonymity.
Constructors & Constructor Overloading in C++
What is a Constructor?
In C++, a constructor is a special member function of a class that is automatically called when
an object of that class is created. Its primary purpose is to initialize the object's data
members and set up the object's initial state.
Key characteristics of constructors:
● Same Name as Class: A constructor function must have the exact same name as its
class.
● No Return Type: Constructors do not have a return type, not even void.
● Automatically Called: You cannot call a constructor explicitly like a regular function. It is
invoked automatically when an object is created.
● Can Have Parameters: Constructors can take arguments, allowing you to initialize an
object with specific values at the time of creation.
Types of Constructors
1. Default Constructor:
○ A constructor that takes no arguments.
○ If you don't define any constructor for your class, the C++ compiler automatically
provides a public, empty, default constructor (the implicit default constructor).
○ If you define any constructor (even a parameterized one), the compiler will not
provide the implicit default constructor. In such cases, if you need a default
constructor, you must define it explicitly.
<!-- end list -->ClassName() {
// Initialization code
}
2. Parameterized Constructor:
○ A constructor that takes one or more arguments.
○ It allows you to initialize the object's data members with values passed during
object creation.
<!-- end list -->ClassName(dataType param1, dataType param2, ...) {
// Initialize data members using parameters
}
Constructor Overloading
Just like regular functions, constructors can also be overloaded. This means a class can have
multiple constructors, each with a different set of parameters (a unique signature). This allows
objects to be initialized in various ways depending on the arguments provided during their
creation.
The compiler decides which constructor to call based on the number and type of arguments
, passed when you create an object.
Example Programs
Example 1: Default and Parameterized Constructor
This program demonstrates a Book class with both a default constructor and a parameterized
constructor.
#include <iostream>
#include <string>
class Book {
public:
std::string title;
std::string author;
int pages;
// 1. Default Constructor (no arguments)
Book() {
title = "Unknown Title";
author = "Unknown Author";
pages = 0;
std::cout << "Default Constructor called for a book." <<
std::endl;
}
// 2. Parameterized Constructor (initializes all members)
Book(std::string t, std::string a, int p) {
title = t;
author = a;
pages = p;
std::cout << "Parameterized Constructor called for " << title
<< "." << std::endl;
}
// Member function to display book info
void displayBookInfo() {
std::cout << "Title: " << title << ", Author: " << author <<
", Pages: " << pages << std::endl;
}
};
int main() {
// Creating an object using the Default Constructor
Book book1; // No arguments passed, so default constructor is
called
book1.displayBookInfo();
// Output:
// Default Constructor called for a book.
// Title: Unknown Title, Author: Unknown Author, Pages: 0