You are on page 1of 4

The University of Faisalabad

Submitted to

: Maam Sania Nayab

Submitted by

: Group members

Reg# bscs_fa15- : 012 Rabiya Saleem


: 122 Seerat Iqbal
: 160 Ayesha Butt
Subject

: Object oriented Programming

Date

: 27-04-16

Topic

: Destructor, object parameters, returning object

Destructor:
A destructor is a special member function that is called when the lifetime of an object ends. The
purpose of the destructor is to free the resources that the object may have acquired during its
lifetime. Designate a function as a class's destructor by preceding the class name with a tilde (~).
Syntax:
The syntax of declaring destructor is as follows:
~name()
{
Destructor body
}
~name()
It indicates the name of the destructor. The name must be same as the name of the
class in which the constructor is declared.

Rules of destructor:

Do not accept arguments.


Cannot specify any return type (including void).
Cannot return a value using the return statement.
Cannot be declared as const, volatile, or static. However, they can be invoked for the
destruction of objects declared as const, volatile, or static.

Object as function parameters:


Object can also be passed as parameters to member functions. The method of passing objects to
functions as parameters is same as passing other variables.
Objects can be passed to function in similar way as variables and structures .
Procedure and syntax:

Returning Objects from Member Function:


The method of returning an object from member function is same as returning a simple variable.
If a member function returns an object, its return type should be the same as the type of object to
be returned. The syntax and procedure to return object is similar to that of returning
structure from function.

Syntax and procedure:

Program:
The working of below program is, The add() function in below program accepts a parameter
object of type Marks. It adds the contents of parameter and calling object and stores the result in
a temporary object. The function then returns the whole object back to main() function that is
stored in r. The program finally displays the result using r.show() statement.
Following program containing Destructor, Object as function parameters and returning object
from member functions.