CSC166 ยท 2079 ยท 10 marks
What is the use of constructor and destructor? Write a program for illustrating default constructor, parameterized constructor and copy constructor.
What is the use of constructor and destructor? Write a program for illustrating default constructor, parameterized constructor and copy constructor.[10]
Quick answer
A constructor is a special member function of a class that is automatically called when an object is created. Its main uses are: Automatic initialization of data members when an object is created; Eliminates the need to manually initialize each object; Useful when handling arrays of objects (e.
Constructor and Destructor in C++
Use of Constructor
A constructor is a special member function of a class that is automatically called when an object is created. Its main uses are:
- Automatic initialization of data members when an object is created
- Eliminates the need to manually initialize each object
- Useful when handling arrays of objects (e.g., initializing 100s of
Personobjects with defaultage = 0) - Executes code immediately after an object is created
- Has the same name as the class, no return type, and can be overloaded
Use of Destructor
A destructor is a member function that destructs or deletes an object and frees resources. Its main uses are:
- Automatically releases memory or resources when an object goes out of scope
- Called automatically when:
- A function ends
- The program ends
- A block containing local variables ends
- The
deleteoperator is called
- Has the same name as the class preceded by
~ - Takes no arguments and returns nothing
- There can be only one destructor in a class
Program: Illustrating Default, Parameterized, and Copy Constructor
#include<iostream.h>
#include<conio.h>
class Student {
private:
int rollNo;
float marks;
public:
// 1. Default Constructor
// Called automatically when no arguments are passed
Student() {
rollNo = 0;
marks = 0.0;
cout << "Default Constructor called" << endl;
}
// 2. Parameterized Constructor
// Called when arguments are passed during object creation
Student(int r, float m) {
rollNo = r;
marks = m;
cout << "Parameterized Constructor called" << endl;
}
// 3. Copy Constructor
// Called when one object is initialized with another object
Student(Student &s) {
rollNo = s.rollNo;
marks = s.marks;
cout << "Copy Constructor called" << endl;
}
// Destructor
~Student() {
cout << "Destructor called for Roll No: " << rollNo << endl;
}
// Display function
void display() {
cout << "Roll No : " << rollNo << endl;
cout << "Marks : " << marks << endl;
}
};
void main() {
clrscr();
// Calls Default Constructor
cout << "--- Object s1 (Default Constructor) ---" << endl;
Student s1;
s1.display();
// Calls Parameterized Constructor
cout << "\n--- Object s2 (Parameterized Constructor) ---" << endl;
Student s2(101, 85.5);
s2.display();
// Calls Copy Constructor
cout << "\n--- Object s3 (Copy Constructor) ---" << endl;
Student s3(s2);
s3.display();
getch();
}
Output
--- Object s1 (Default Constructor) ---
Default Constructor called
Roll No : 0
Marks : 0
--- Object s2 (Parameterized Constructor) ---
Parameterized Constructor called
Roll No : 101
Marks : 85.5
--- Object s3 (Copy Constructor) ---
Copy Constructor called
Roll No : 101
Marks : 85.5
Destructor called for Roll No: 101
Destructor called for Roll No: 101
Destructor called for Roll No: 0
Summary Table
| Constructor Type | When Called | Arguments |
|---|---|---|
| Default Constructor | Student s1; | None |
| Parameterized Constructor | Student s2(101, 85.5); | One or more |
| Copy Constructor | Student s3(s2); | Reference to same class object |
| Destructor | Object goes out of scope | None |
Note: Destructors are called in reverse order of object creation, as shown in the output above.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.