You are on page 1of 15

MODULE FOUR

CONSTRUCTORS AND DESTRUCTORS


OBJECTIVES
• By the end of this modules students would
have added to their previous knowledge the
following:
• What constructors are
• How and what they are used for
• How they can be used
What are Constructors?
• A class constructor is a special function in a
class that is responsible for creating new objects
when required.
• A constructor, therefore, provides the opportunity to
initialize objects as they are created and to ensure
that data members only contain valid values.
• A class may have several constructors, enabling you
to create objects in various ways.
• You have no freedom in naming the constructors in a
class — they always have the same name as the
class in which they are defined.
• It also has no return type.
• It is wrong to specify a return type for a
constructor
• it must not even have it as void .
• The primary purpose of a class constructor is to
assign initial values to the data elements of the
class, and no return type for a constructor is
necessary or permitted.
• Constructor
It is a member function having same name as it’s class
and which is used to initialize the objects of that class
type with a legal initial value. Constructor is
automatically called when object is created.
• Types of Constructor
• Default Constructor-: A constructor that accepts no
parameters is known as default constructor. If no
constructor is defined then the compiler supplies a
default constructor.
Circle :: Circle()
{
radius = 0;
}
• Parameterized Constructor -: A constructor that receives
arguments/parameters, is called parameterized constructor.
Circle :: Circle(double r)
{
radius = r;
}
• Copy Constructor-: A constructor that initializes an object
using values of another object passed to it as parameter, is
called copy constructor. It creates the copy of the passed
object.
Circle :: Circle(Circle &t)
{
radius = t.radius;
}

There can be multiple constructors of the same class, provided they have different
signatures.
• // example: class constructor Rectangle::Rectangle (int a, int b)
#include <iostream> {
using namespace std; width = a;
height = b;
class Rectangle
}
{ int main ()
int width, height; {
public: Rectangle rect (3,4);
Rectangle (int a,int b); Rectangle rectb (5,6);
int area () cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
{
return 0;
return (width*height); }
}
};
// Using a constructor // Function to calculate the volume of a box
#include <iostream> double Volume()
using namespace std; {
class CBox // Class definition at global scope return m_Length* m_Width* m_Height;
{ }
public: };
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches int main()
double m_Height; // Height of a box in inches {
CBox box1(78.0,24.0,18.0); // Declare and initialize box1
// Constructor definition CBox matchBox(8.0,5.0,1.0); // Declare and initialize matchBox
CBox(double lv, double bv, double hv) double boxVolume; // Stores the volume of a box
{ boxVolume = box1.Volume(); // Calculate volume of box1
cout << “Constructor called.” << endl ; cout << “Volume of box1 = “ << boxVolume <<
m_Length = lv; // Set values of endl ;
m_Width = bv; // data members cout << “Volume of matchBox = “ <<
m_Height = hv; matchBox.Volume() << endl ;
} return 0;
}
• Class work
Write a program for class triangle that has a
constructor for initializing values for two different
triangle (two objects). The area of the triangles are
to be returned from a function called triarea().
Write a main to execute the constructor and
function.
Overloaded Constructors
• Like any other function, a constructor can also be
overloaded with different versions taking different
parameters: with a different number of parameters
and/or parameters of different types. The compiler
will automatically call the one whose parameters
match the arguments
// overloading class constructors
#include <iostream>
using namespace std;
class Rectangle
{
int width, height;
public:
Rectangle ();
Rectangle (int a, int b);
int area ()
{
return (width*height);
}
};
Rectangle::Rectangle ()
{
width = 5;
height = 5;
}
Rectangle::Rectangle (int a, int b)
{
width = a;
height = b;
}
int main ()
{
Rectangle rect (3,4);
Rectangle rectb;
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
Destructor
A destructor is a member function having same name as that of its class preceded by ~(tilde)
sign and which is used to destroy the objects that have been created by a constructor. It gets
invoked when an object’s scope is over.
~Circle() {}
A destructor has the same name as the constructor (which is the same as the class name)
but is preceded by a tilde:
The class destructor doesn't return a value and doesn't have parameters defined

class Foo
{
private:
int data;
public:
Foo() : data(0) //constructor (same name as class)
{}
~Foo() //destructor (same name with tilde)
{}
};
• Example : In the following program constructors, destructor and other member functions are
defined inside class definitions. Since we are using multiple constructor in class so this
example also illustrates the concept of constructor overloading
#include<iostream>
using namespace std;
class Circle //specify a class
{
private :
double radius; //class data members
public:
Circle() //default constructor
{
radius = 0;
}
Circle(double r) //parameterized constructor
{
radius = r;
}
Circle(Circle &t) //copy constructor
{
radius = t.radius;
}
void setRadius(double r) //function to set data
{
radius = r;
}
double getArea ()
{
return 3.14 * radius * radius;
}

~Circle() //destructor
{}
};
int main()
{
Circle c1; //default constructor invoked
Circle c2(2.5); //parmeterized constructor invoked
Circle c3(c2); //copy constructor invoked
cout << c1.getArea()<<endl;
cout << c2.getArea()<<endl;
cout << c3.getArea()<<endl;
return 0;
}

You might also like