0% found this document useful (0 votes)
11 views3 pages

CSE Lab Report 3

This document outlines an experiment focused on understanding constructors and destructors in C++. It explains their roles, characteristics, and demonstrates their usage through a code example involving polymorphism with a base class pointer. The conclusion emphasizes the importance of virtual destructors for resource management in derived class objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

CSE Lab Report 3

This document outlines an experiment focused on understanding constructors and destructors in C++. It explains their roles, characteristics, and demonstrates their usage through a code example involving polymorphism with a base class pointer. The conclusion emphasizes the importance of virtual destructors for resource management in derived class objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment No.

: 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() {}

// Virtual function for calculating area


virtual double area() const {
return 0;
}
};

class Triangle : public Shape {


public:
Triangle(double dim1, double dim2) : Shape(dim1, dim2) {}

// Override area function


double area() const override {
return 0.5 * dim1 * dim2;
}
};

class Rectangle : public Shape {


public:
Rectangle(double dim1, double dim2) : Shape(dim1, dim2) {}

// Override area function


double area() const override {
return dim1 * dim2;
}
};

int main() {
// Polymorphism with base class pointer
Shape* p;

// Triangle and Rectangle objects


Triangle t(10, 20);
Rectangle r(10, 20);

// 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.

You might also like