0% found this document useful (0 votes)
56 views4 pages

Assignment of Programming

The document discusses destructors, passing objects as function parameters, and returning objects from member functions in C++. A destructor is called when an object ends its lifetime and frees any resources. It is declared with a tilde followed by the class name and empty parentheses. Destructors cannot accept arguments or return a value. Objects can be passed to functions as parameters like other variables. Returning an object from a member function is similar to returning a simple variable - the return type matches the object type. The program example adds object contents, stores the result in a temporary object, and returns it.

Uploaded by

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

Assignment of Programming

The document discusses destructors, passing objects as function parameters, and returning objects from member functions in C++. A destructor is called when an object ends its lifetime and frees any resources. It is declared with a tilde followed by the class name and empty parentheses. Destructors cannot accept arguments or return a value. Objects can be passed to functions as parameters like other variables. Returning an object from a member function is similar to returning a simple variable - the return type matches the object type. The program example adds object contents, stores the result in a temporary object, and returns it.

Uploaded by

Rabiya Saleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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.

You might also like