Classes and Objects With Constructors/ Destructors: Module - 3
Classes and Objects With Constructors/ Destructors: Module - 3
Programming in C++
18
WITH CONSTRUCTORS/
DESTRUCTORS
In the previous lesson you have learnt about structure, typedef and enumerated
data types. In object oriented programming, the emphasis is on data rather than
function. Class is a way that binds the data & function together. Constructor
is a specially designed class and destructor returns the memory addresses back
to the system. In this lesson you will learn about classes, objects with
constructors and destructors.
OBJECTIVES
After reading this lesson, you will be able to:
z define class and object;
z access the members of the class;
z learn about three visibility modes: public, private and protected;
z define constructor with default arguments;
z use destructor.
18.1 CLASS
A class is a way to bind the data and its associated functions together. It allows
data functions to be hidden, if necessary from external use. A class specification
has two parts.
(i) Class declaration
(ii) Class function definitions
The keyword class is followed by the name of the class. The body of the class
is enclosed between braces and terminated by semi-colon. The class body
contains the declaration of variables and functions. These are collectively called
members. The variables declared inside the class are called as data members.
The functions are known as member functions. The keywords public, private
and protected are called as visibility modes / access specifiers.
The data member and member
By default, the members of a class are
functions present in private
private. Private data members and
and protected mode can be
private functions can be accessed only by
accessed by the member
member functions of a class. Public
function in public mode. The
members can be accessed from outside of
private and protected modes
the class.
are exactly the same except
that protected can be inherited
(explained later in this lesson) but private mode cannot be inherited. The data
member and member functions present in public mode can be accessed within
the class and also by other class of same program.
The use of keyword private is optional. By default, the members of a class are
private. If the labels are missing, members are private by default.
Notes return ( z ) ;
}
void greatest : : getdata ( ) //function definition outside class
{
cout <<“Enter values of x, y, z” << “\n”;
cin >> x >> y >> z ;
}
void greatest : : display ( ) //function definition outside class
{
cout << “largest value” << largest ( ) << “\n”; //nesting of member function
}
void main ( )
{
greatest A; //object of class
A. getdata ( ); //accessing class members with dot operator
A. display ( );
}
The statement e [ i ]. getdata ( ) will get the data of the ith element of the
array e.
18.2 CONSTRUCTOR
A constructor is a special member function that initializes the objects of its class
automatically when it is created. It is special because its name is the same as
the class name. It is invoked automatically whenever an object is created. It is
called constructor because it constructs the values of data members of the class.
It does not have any return data type, not even void.
A constructor is declared and defined as follows:
class student
{
int rn;
int total ;
public :
student ( ) // constructor
{
rn = 0 ; total = 0 ; Constructor should be
declared in the public
} section. It does not have any
}; return data type hence it
The declaration can not return any values.
student st ;
Invokes the constructor, student ( ) and assigns the value 0 to rn and total
variables.
{
rn = x ; total = y ;
}
};
When the object is created, we must supply arguments to the constructor
function. This can be done in two ways:
z By calling the function explicitly
z By calling the function implicitly
The first call is implemented as follows :
student S1 = student ( 1, 70 ) ;
The second call is implemented as follows :
student S1 ( 1, 70 ) ;
The second method is used very often as it is shorter.
18.3 DESTRUCTOR
It is used to destroy the objects that have been created by a constructor. The
destructor is a member function whose name is the same as the class name but
is preceded by a tilde. For example the destructor of the class student can be
defined as
~student ( ) ;
It never takes any argument nor does it return any value. It will be invoked by
the compiler upon exit from the program ( or function or block) to clean the
storage. It is a good practice to declare destructor in a program because it
releases memory space for future use.
TERMINAL EXERCISE
1 What do you understand by visibility modes in class derivations ? What are
these modes?
2 Define a class Teacher with the following specifications :
Private members :
Name 20 characters
Subject 10 characters
Basic, DA, HRA float
Salary float
18.1
1. a) private b) object
c) : : d) Scope resolution
e) data members.
18.2
1 a) class name b) created
c) automatically d) one
Notes e) tilde (~) f) parameterized
2 a) False b) True
c) True d) True
e) True f) True
g) True