2080 1

BIT153 ยท 2080 ยท 10 marks

What is constructor? Explain its types with suitable example.How is the constructor called in a derived class in C++?[6+4]

What is constructor? Explain its types with suitable example.How is the constructor called in a derived class in C++?[6+4]

Object Oriented ProgrammingChapter 3: Constructors and DestructorsConstructor definition and purpose

Quick answer

A constructor is a special member function of a class that is automatically called when an object of that class is created. It is used to initialize the data members of the class.

Constructor in C++

What is a Constructor?

Key Characteristics:

  • Has the same name as the class
  • Has no return type (not even void)
  • Called automatically when an object is created
  • Can be overloaded

Types of Constructors

1. Default Constructor

A constructor that takes no arguments. If no constructor is defined, the compiler provides one automatically.

#include<iostream>
using namespace std;

class Student {
    int roll;
    string name;
public:
    // Default Constructor
    Student() {
        roll = 0;
        name = "Unknown";
        cout << "Default Constructor called" << endl;
    }
    void display() {
        cout << "Roll: " << roll << ", Name: " << name << endl;
    }
};

int main() {
    Student s1;   // Default constructor called automatically
    s1.display();
    return 0;
}

Output:

Default Constructor called
Roll: 0, Name: Unknown

2. Parameterized Constructor

A constructor that accepts one or more arguments to initialize objects with specific values.

#include<iostream>
using namespace std;

class Student {
    int roll;
    string name;
public:
    // Parameterized Constructor
    Student(int r, string n) {
        roll = r;
        name = n;
        cout << "Parameterized Constructor called" << endl;
    }
    void display() {
        cout << "Roll: " << roll << ", Name: " << name << endl;
    }
};

int main() {
    Student s1(101, "Ram");   // Parameterized constructor called
    s1.display();
    return 0;
}

Output:

Parameterized Constructor called
Roll: 101, Name: Ram

3. Copy Constructor

A constructor that creates a new object as a copy of an existing object. It takes a reference to an object of the same class as its argument.

#include<iostream>
using namespace std;

class Student {
    int roll;
    string name;
public:
    Student(int r, string n) {
        roll = r;
        name = n;
    }
    // Copy Constructor
    Student(const Student &s) {
        roll = s.roll;
        name = s.name;
        cout << "Copy Constructor called" << endl;
    }
    void display() {
        cout << "Roll: " << roll << ", Name: " << name << endl;
    }
};

int main() {
    Student s1(101, "Ram");
    Student s2(s1);    // Copy constructor called
    s2.display();
    return 0;
}

Output:

Copy Constructor called
Roll: 101, Name: Ram

4. Constructor Overloading

Multiple constructors with different parameter lists in the same class.

#include<iostream>
using namespace std;

class Rectangle {
    float length, breadth;
public:
    Rectangle() {              // Default
        length = breadth = 0;
    }
    Rectangle(float l, float b) {   // Parameterized
        length = l;
        breadth = b;
    }
    void area() {
        cout << "Area = " << length * breadth << endl;
    }
};

int main() {
    Rectangle r1;           // calls default constructor
    Rectangle r2(5.0, 3.0); // calls parameterized constructor
    r1.area();
    r2.area();
    return 0;
}

Calling Constructor in a Derived Class

When a derived class object is created, both the base class constructor and the derived class constructor are called. The base class constructor is called first, then the derived class constructor.

To explicitly pass arguments to the base class constructor, we use an initializer list with the colon (:) syntax.

Syntax:

DerivedClass(parameters) : BaseClass(arguments) {
    // derived class constructor body
}

Order of Execution:

  1. Base class constructor executes first
  2. Derived class constructor executes next

Example:

#include<iostream>
using namespace std;

// Base Class
class Animal {
    string name;
public:
    Animal(string n) {
        name = n;
        cout << "Animal Constructor: " << name << endl;
    }
};

// Derived Class
class Dog : public Animal {
    string breed;
public:
    // Calling base class constructor using initializer list
    Dog(string n, string b) : Animal(n) {
        breed = b;
        cout << "Dog Constructor: Breed = " << breed << endl;
    }
};

int main() {
    Dog d1("Tommy", "Labrador");
    return 0;
}

Output:

Animal Constructor: Tommy
Dog Constructor: Breed = Labrador

Key Points:

AspectDetail
Base constructor callAlways called before derived constructor
SyntaxDerived(params) : Base(args) { }
Default base constructorCalled automatically if not specified
Destructor orderReverse of constructor order

Note: If the base class has only a parameterized constructor (no default constructor), the derived class must explicitly call it using the initializer list, otherwise a compile-time error occurs.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.