CSC166 ยท 2080 ยท 10 marks
What is constructor? Why constructor is needed in a class? Illustrate the types of constructor with an example.
What is constructor? Why constructor is needed in a class? Illustrate the types of constructor with an example.[10]
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 has the same name as the class and is used to initialize the data members of the class.
Constructor in C++
Definition of Constructor
Key Characteristics of a Constructor:
- It has the same name as the class
- It has no return type (not even
void) - It is called automatically when an object is created
- It cannot be inherited, but a derived class can call the base class constructor
- It can be overloaded
Why is a Constructor Needed in a Class?
When an object is created, its data members may contain garbage values if not initialized. A constructor is needed because:
- Automatic Initialization: It automatically initializes data members when an object is created, avoiding garbage values.
- Memory Allocation: It can allocate dynamic memory using the
newoperator at the time of object creation. - No Manual Call Required: Unlike regular functions, the programmer does not need to explicitly call it; it runs automatically.
- Ensures Valid State: It ensures that an object always starts in a valid, well-defined state.
- Resource Acquisition: It can open files, establish connections, or acquire any resources needed by the object.
As seen in the notes, a class like
stringuses a constructorstring(char *c)to allocate memory usingnewand copy data, ensuring the object is properly set up from the moment it is created.
Types of Constructors
There are three main types of constructors in C++:
1. Default Constructor
A constructor that takes no arguments (or has all default arguments). It is called automatically when an object is created without any parameters.
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
float marks;
public:
// Default Constructor
Student() {
rollNo = 0;
marks = 0.0;
cout << "Default Constructor called!" << endl;
}
void display() {
cout << "Roll No: " << rollNo << ", Marks: " << marks << endl;
}
};
int main() {
Student s1; // Default constructor is called automatically
s1.display();
return 0;
}
Output:
Default Constructor called!
Roll No: 0, Marks: 0.0
2. Parameterized Constructor
A constructor that accepts one or more arguments to initialize data members with specific values at the time of object creation.
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
float marks;
public:
// Parameterized Constructor
Student(int r, float m) {
rollNo = r;
marks = m;
cout << "Parameterized Constructor called!" << endl;
}
void display() {
cout << "Roll No: " << rollNo << ", Marks: " << marks << endl;
}
};
int main() {
Student s1(101, 85.5); // Parameterized constructor called
Student s2(102, 90.0);
s1.display();
s2.display();
return 0;
}
Output:
Parameterized Constructor called!
Parameterized Constructor called!
Roll No: 101, Marks: 85.5
Roll No: 102, Marks: 90.0
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 {
private:
int rollNo;
float marks;
public:
// Parameterized Constructor
Student(int r, float m) {
rollNo = r;
marks = m;
}
// Copy Constructor
Student(const Student &s) {
rollNo = s.rollNo;
marks = s.marks;
cout << "Copy Constructor called!" << endl;
}
void display() {
cout << "Roll No: " << rollNo << ", Marks: " << marks << endl;
}
};
int main() {
Student s1(101, 85.5); // Parameterized constructor
Student s2(s1); // Copy constructor called
s1.display();
s2.display();
return 0;
}
Output:
Copy Constructor called!
Roll No: 101, Marks: 85.5
Roll No: 101, Marks: 85.5
Summary Table
| Type | Arguments | Purpose |
|---|---|---|
| Default Constructor | None | Initializes members with default values |
| Parameterized Constructor | One or more | Initializes members with given values |
| Copy Constructor | Reference to same class object | Creates a copy of an existing object |
Note on Constructor vs Destructor
A class can have both a constructor and a destructor (~classname()). While the constructor initializes/creates the object, the destructor cleans up (e.g., frees memory allocated by new) when the object goes out of scope. Together they manage the full lifecycle of an object.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.