CSE Lab Report 3
CSE Lab Report 3
: 03
Experiment Name: Introduction to Constructor and Destructor
Objectives
To Learn what constructors and destructors are and their purpose in C++
programming.
To Examine their respective roles and usage scenarios.
To Explore how constructor overloading works and how constructors can be chained.
Introduction
Constructor:
A constructor is a special member function in a class that is automatically called when an
object of the class is created. It is primarily used to initialize the object and allocate resources,
if necessary.
Key Characteristics:
It has the same name as the class.
It does not have a return type (not even void).
It can be overloaded to create multiple constructors with different parameter lists.
If no constructor is explicitly defined, the compiler provides a default constructor.
Destructor:
A destructor is a special member function in a class that is automatically called when an
object goes out of scope or is explicitly deleted. Its primary role is to release resources
allocated to the object, such as memory or file handles.
Key Characteristics:
It has the same name as the class, preceded by a tilde (~).
It does not take arguments or have a return type.
There can only be one destructor per class, and it cannot be overloaded.
It is automatically invoked and cannot be called explicitly in most cases.
Code
#include <iostream>
using namespace std;
class Shape {
public:
double dim1, dim2;
// Constructor
Shape(double dim1, double dim2) : dim1(dim1), dim2(dim2)
{}
// Virtual destructor
virtual ~Shape() {}
int main() {
// Polymorphism with base class pointer
Shape* p;
// Pointing to Triangle
p = &t;
cout << "Triangle area = " << p->area() << endl;
// Pointing to Rectangle
p = &r;
cout << "Rectangle area = " << p->area() << endl;
return 0;
}
Explanation
1. In main(), two objects are created: a Triangle object (t) and a Rectangle object (r).
The respective constructors for these classes (Triangle::Triangle and
Rectangle::Rectangle) are called, which in turn call the Shape constructor.
2. A Shape pointer (p) is used to demonstrate polymorphism.
When p points to the Triangle object (t), the Triangle's overridden area()
function is invoked.
When p points to the Rectangle object (r), the Rectangle's overridden area()
function is invoked.
3. When main() ends, the destructors for t and r are called in reverse order of their
creation.
First, the Triangle destructor is called (implicitly calling the Shape destructor).
Then, the Rectangle destructor is called (also invoking the Shape destructor).
By making the destructor in Shape virtual, the code ensures proper cleanup for derived class
objects when accessed via a Shape pointer.
Output
Conclusion
This program illustrates how constructors and destructors are used to initialize and clean up
objects in C++. It emphasizes the importance of virtual destructors in ensuring proper
resource management for derived class objects when accessed via base class pointers.
Additionally, it demonstrates polymorphism, enabling dynamic function calls for calculating
the area of different shapes, making the code flexible and extensible.