Professional Documents
Culture Documents
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;
}