0% found this document useful (0 votes)
10 views

CONSTRUCTORS (1)

The document explains constructors and destructors in object-oriented programming, detailing their definitions, characteristics, and types. Constructors are member functions that initialize objects, while destructors free resources held by objects. It also covers the syntax for declaring and defining these functions, along with examples of default, parameterized, and copy constructors.

Uploaded by

antomotongori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CONSTRUCTORS (1)

The document explains constructors and destructors in object-oriented programming, detailing their definitions, characteristics, and types. Constructors are member functions that initialize objects, while destructors free resources held by objects. It also covers the syntax for declaring and defining these functions, along with examples of default, parameterized, and copy constructors.

Uploaded by

antomotongori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CONSTRUCTORS AND DESTRUCTORS

Session Objectives
 Upon completing this session you should be
able to:
 Differentiate constructor and destructor
 List characteristics of constructor and destructor
 Use constructors and destructors
Constructors

 A constructor is a member function that is used for creating objects


and initializing the states of the objects.
 A constructor has the following characteristics:
 Its name is the same as the name of the class within which it has

been declared
 It is called automatically

 It does not have return type

 It is always declared within the public section of a class

 It is not inherited. A constructor cannot be made virtual

 The address of the memory location occupied by a constructor

cannot be referenced
Declaration of Constructors
 A constructor is declared and defined as follows:

Class MyClass { // The class


public: // Access specifier
MyClass() { // Constructor
cout << "Hello World!";
}
};

int main() {
MyClass myObj; // Create an object of MyClass (this will call the constructor)
return 0;
}
Example

class Rectangle
{
private:
int length;
int width;
...
public:
Rectangle(int l, int w); //declaration of constructor
...
};
//definition of constructor
Rectangle::Rectangle(int l, int w)
{
length=l;
width=w;}
Calling Constructors

 Since a constructor is always declared within the public section of a class, it is


accessible from external functions (these are functions that are not members
of the class that has the class).
 For example the main method can call the constructor in the Rectangle class
above as shown below.
Rectangle rect1(6, 5);
 This statement is a call to Rectangle constructor in a class called Rectangle.
The constructor is also passed two integer values, i.e. 6 and 5. The
constructor will construct the object and 6 and 5 values will be used as the
initial state of the object. The object that will be created will be referenced by
rect1 variable.
 If we now want to pass a message to the object that has been created in
Rectangle class, we have to use rect1 to refer to the object as shown below
rect1.find_area(); rect1 is our reference to the object created and find_area()
is the message that we are passing to our object. rect1 refers to the object
created.
Types of Constructors

 There are three types of constructors:


 Default constructors
 Copy constructors
 Parameterized constructors
a) Default Constructor
 A default constructor is a constructor that is not passed any arguments when it is called. A default constructor
can be created automatically by the compiler. For example, the default constructor for Rectangle class above
is:
class Rectangle
{
private: . . .
public:
Rectangle();
};
//definition of default constructor
Rectangle::Rectangle()
{
}
Note that the body of this constructor is empty. Now if we have a statement shown below within the main method
int main(void)
{
Rectangle rect1;
...
return 0; }
 Statement Rectangle rect1; in the above main method is a call to the default. In that case if the Rectangle
class do not have the default constructor the compiler will create one automatically.
b) Parameterized constructors

 Parameterized constructors are constructors that are passed arguments when they are called. Unlike
default constructors, parameterized constructors are not created automatically by the compiler if they
are not defined in a class. And these constructors must be defined. For example:
class Rectangle
{
private: int length;
int width;
...
public:
Rectangle(int l, int w);
...
};
//definition of a parameterized constructor
Rectangle::Rectangle(int l, int w)
{ length=l; width=w; }
 There are two ways of passing arguments to parameterized constructors when they are called: by
calling the constructor explicitly and by calling the constructor implicitly. For example Rectangle
rect1= Rectangle( 6, 5); //explicit call Rectangle rect1(6, 5); //implicit call Implicit call is the one that
is normally used.
c) Copy constructors

 Copy constructors are constructors that used to create and initialize an


object using another object.
 For example Rectangle rect1(6, 5); //a call to a parameterized
constructor Rectangle rect2(rect1); // a call to a copy constructor to
create and initialized rect2 object //using rect1 object
 #include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// initialize variables with parameterized constructor
Wall(double len, double hgt) {
length = len;
height = hgt; }
// copy constructor with a Wall object as parameter
// copies data of the obj parameter
Wall(Wall &obj) {
length = obj.length;
height = obj.height; }
double calculateArea() {
return length * height; }
};
contd
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);

// copy contents of wall1 to wall2


Wall wall2 = wall1;

// print areas of wall1 and wall2


cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();

return 0;
}
Destructors

 A destructor is a member function that is used to free a location that is


held by an object for use by another object.
 An object is deleted when the flow of control leaves the scope within
which it has been created.
 A destructor has the following properties:
 Its name is the same as the name of the class
 Its name is preceded by a tilde (~) character
 It is not passed arguments and it does not have return type not even void
 It is called implicitly
 A class can have only one destructor
 A destructor can not be overloaded
Destructor Syntax:
 Syntax:
Syntax for defining the destructor within the class
~ <class-name>()
{

 Syntax for defining the destructor outside the class


<class-name>: : ~ <class-name>()
{

}
Example

Rectangle::~Rectangle()
{
cout<< “An object has been destroyed”<<endl;
}
Note that just like other member functions, constructors can have
default arguments and they can be overloaded.
Example
#include<iostream>
using namespace std;
class Test
{
public:
Test()
{
cout<<"\n Constructor executed";
}
~Test()
{
out<<"\n Destructor executed";
}
};
main()
{
Test t;
return 0;
}
Revision Questions

 Differentiate a constructor and a destructor


 List any FOUR characteristics of a destructor
 Explain why a constructor is public and has no return type
 Differentiate a default constructor, a copy constructor and
parameterized constructor
 Explain the difference between the following two statements:
Car myCar(200); and Car yourCar;

You might also like