←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Constructors in C++: Default & Parameterized Constructors

Study material and explanations for Unit 4

Constructors in C++: Default & Parameterized Constructors

1. What is a Constructor? (In Simple Words)

In standard C or basic classes, when you create an object, its variables contain random garbage values until you manually call an input function. If you forget to call it, your program will produce wrong answers or crash!

C++ solves this problem with Constructors.

A Constructor is a special member function of a class that is called automatically by the compiler the exact moment an object is created in RAM. Its main job is to give proper initial values to the object’s variables.

How a Constructor Executes Automatically
Object Created
Student s1; statement runs in main()
── Compiler triggers automatically ──▶
Constructor Runs
Student() sets rollNo = 0, marks = 0.0
── Ready for use ──▶
Initialized Object
Zero garbage values! Clean object ready to work.

Real-Life Analogy: Think of buying a brand-new smartphone. As soon as you unbox it and press power, it automatically loads factory settings (language, date, sound profiles). You don’t have to build the software from scratch! That automatic initial setup is a constructor.


2. Key Characteristics of Constructors (Golden Exam Points)

Examiners love asking this 5-mark question. Remember these 6 rules:

  1. Same Name: It has the exact same name as the class.
  2. No Return Type: It has no return type at all, not even void!
  3. Placed in Public: It is almost always placed in the public: section so objects can be created freely from main().
  4. Runs Automatically: It executes automatically when an object is declared; you do not call it with a dot operator (.).
  5. Cannot be Inherited: A child class does not inherit the parent class constructor (though it can call it).
  6. Cannot be Virtual: Constructors cannot be marked as virtual.

3. Types of Constructors: Default vs. Parameterized

A. Default Constructor (Takes No Arguments)

A constructor that takes no parameters is called a Default Constructor. It gives fixed, starting values (like 0) to all objects.

#include <iostream>
using namespace std;

class Point {
    private:
        int x, y;

    public:
        // 1. DEFAULT CONSTRUCTOR (No arguments)
        Point() {
            x = 0;
            y = 0;
            cout << "Default Constructor called! Coordinates set to (0, 0)" << endl;
        }

        void display() const {
            cout << "Point: (" << x << ", " << y << ")" << endl;
        }
};

int main() {
    Point p1; // Automatically runs Point()
    p1.display(); // Prints: Point: (0, 0)
    return 0;
}

Warning: When creating an object with a default constructor, write Point p1;. Never write Point p1(); because C++ will think it is a function declaration instead of an object!


B. Parameterized Constructor (Takes Arguments)

What if you want different students to have different roll numbers and marks right at the time of creation?

A Parameterized Constructor accepts arguments so you can initialize each object with custom values:

#include <iostream>
#include <string>
using namespace std;

class Student {
    private:
        int rollNo;
        string name;
        float marks;

    public:
        // PARAMETERIZED CONSTRUCTOR (Takes 3 inputs)
        Student(int r, string n, float m) {
            rollNo = r;
            name = n;
            marks = m;
            cout << "Initialized: " << name << endl;
        }

        void show() const {
            cout << "Roll: " << rollNo << "\tName: " << name << "\tMarks: " << marks << endl;
        }
};

int main() {
    // Method 1: Implicit Call (Short and most popular)
    Student s1(101, "Rahul Sharma", 88.5f);

    // Method 2: Explicit Call
    Student s2 = Student(102, "Sneha Patel", 94.0f);

    s1.show();
    s2.show();

    return 0;
}

4. Comparison: Default vs. Parameterized Constructor

Feature Default Constructor Parameterized Constructor
Number of Arguments Takes 0 arguments Takes 1 or more arguments
Values Assigned Assigns fixed starting values (like 0 or empty) to all objects Assigns custom values passed by the user
How to Call? Student s1; (No parentheses!) Student s1(101, "Rahul", 85);
Auto-generation If you write NO constructor, compiler provides one automatically Compiler never provides this; you must write it yourself

DAVV Exam Preparation Tips

Frequently Asked 5-Mark Questions:

  1. Define a Constructor. State four essential properties of constructors in C++.
    Model Answer:
    • Define: A special member function with the same name as the class that initializes objects automatically upon creation.
    • Properties: Same name as class, no return type (not even void), executed automatically, declared under public:.
  2. Differentiate between Implicit and Explicit calls of a parameterized constructor.
    Model Answer:
    • Implicit call (shorthand): Complex c1(2, 5);.
    • Explicit call: Complex c1 = Complex(2, 5);.
    • Both produce the exact same result in memory.

Frequently Asked 10-Mark Questions:

  1. Explain the purpose of constructors in OOP. Differentiate between Default and Parameterized constructors. Write a complete C++ program to represent a BankAccount class with appropriate constructors and deposit/withdraw methods.
    Model Answer:
    • Explain why automatic initialization prevents garbage values.
    • Draw the comparison table between Default and Parameterized constructors.
    • Write full code for class BankAccount with:
      • Default constructor: initializes balance to 0.0.
      • Parameterized constructor: initializes account with customer name and opening balance.
      • deposit() and withdraw() functions with balance validation.