Practical No19.oop
Practical No19.oop
I Practical Significance:
The concept of operator overloading helps to assign the new meaning to the
existing operator and helps to extend the concept of polymorphism.
CHNI
This practical is expected to develop the following skills as:
l provide additional meaning to the unary operator using member function and
friend function.
2. Define anduse the overloaded operator function in the class.
Syntax:
I/Statements;
Maharashtra State Board of Technology (K Scheme) 107
Object Oriented Programming Using C++t (313304)
The operator keyword is used for overloading the operators.
There are a few operators whichcannot beoverloaded are follows,
1. Scope resoluticn operator ()
2. sizeof
3. member selector(.)
4. member pointer selector ()
5. ternary operator (?:)
There are some restrictions considered while implementing the operator overloading.
1 The number of operands cannot be changed. Unary operator remains
unary, binary remains binary ctc.
2. Only existing operators can be overloaded.
3. The precedence and associativity of an operator cannot be changed.
STATE 4. Cannot redefine the meaning of a procedure.
TheUnary Operators can be overloaded by using:
HTRAS
1. Member Function
2. Friend Function
In the case of anon-static member function,the unary operator should rot have
an argument.
In the case of a friend function, the unary operator should have only one
argument.
They perform various operations such as incrementing/decrementing a value
by one, negating an expression, or inverting the value of a boolea11
ANIC
VII Precautions to befollowed
1. Handle computer systenn and peripherals with care.
2. Follow safety practices.
IX Exercise:
Distance(int f inti)
feet= f:
inches= i;
void displayDistance(Q{
Cout <"E: "<<feet<<"I:"<<inches <<endi;
int main)
Distance Dl(ll, 10), D2(-5, |1);
-Di:
DI.displaylDistance();
Maharashtra State Board of Technology (K Schemne) 109
PAGE Nn
DATE
EXercise
iFunction OUerlo acing
EuctOn f e t ure
ier
Oaaneters
type Dwrm ber
The loe CanOt
be
iOperator Overoaclinq
thcut
featture
to
DATE:
Gaple
pri vote
public
Veid getclatcCinta,intb,into,
ptctcLc)
Void operator
PAGE NO.
DATE:
VOid main CO
Sample s:
SgetclatCL Ci,20, 30),
Gdisplay
ge tch O:
whaty perator
-Cminus using ftienc
urion
#inclce2i0sreom}
DGtance
private
SceetbIhceG
Dietace3 fOet(o, inches (o)?
DStanceCint int i )
feet =
incheG
PAGE No..
DATE:
DistcCe
clrscrCO
Distasce D3-DI
Dy dis plDiSton ce c
ge teh
Object Oriented Programming Using C+(313304)
-D2:
D2.display Distance():
return 0:
XI Resources Used
OF TECHA
Sr. Name of Resource Suggested Broad Specification Quantity
No.
XII Result(s)
XIII Conclusion
...s....h-e.ar.c.ti.Gad........sahAJe
.have...eaTta.AMte..prOgramS
t.eerle.uNay..Fer.cAtOr.Sig
Simember Fuction2 Fiend unction
XIV Practical Related Questions
Note: Below given are few sanple questions for reference. Teacher must
design nore such questions so as to ensure the achievement of identified CO.
(Note: for all relevant programming exercise use blank pages provided or attach
more pages if needed.)
Write a C+t program to overload unary operators (++) increment and(--) decrement
by using member function and friend function.
Write C++ program to find the area of various geometrical shapes by
function overloading. (eg. Area of circle, circumference of circle etc...)
PracicculRelatecl 0westios
"00er locc uNaryoerotrs uging
member
la Counter
private Couti
C o t e r (O Cont Co)L?
COwnto Cint c ) i c o u t CC ?
Cowteroseratottt C
retTthS
endlj
PAGE NO:
DATE:
int
COhter
c-cl is p l y
wain
friend
#incluce< ioSt n e m h
clase Countek
pHV o t e int
public
PAGE No
DATE
uoid displaO
CoLnt
operator++ C Counter c)
returnc
int
clclisploy
ge tcc)
PAGE NO.:
DATE:
+ ihcluce<coiO h
VOid
areaCint l, int b)
clrsCr c
COt
racliug
arec Cr):
intl
Breacith
QreaCI bi;
float Sl
Cout< E n t e r t h e sie.
U0id ate a C i n r )
iteS
reg=3
+he circle"2<res:
int es
tes b
Cowt 4 rea of t h e restcnele<tesL;
foat area C o a t g)
reNCres2
res3,
ATE
res 3 Ob
e t u n Cres3);
Dbject Oriented Programming Using C++ (313304)
3.Complete the following table:
Programn Code Write & justify
Output
Doimt oneG
a)#includeciostream.h> dimenionG
class 3D
int x, y, Z: ireret
OF
TECH
publie:
3D (intA
a0,K
int b-O, int c-O) The elementS
Xa;
y=b;
PointoheG
dimensi onS
3D operatort+0 Cfter incer
X=X+ 1;
y=yt l; 3,y:5,z6
Z+ 1;
Point t o G
return *this;
clise orS
3D operator ++(int) before
icremet
3Dt= *this:
the
X=X+ 1; Cre
y =y+ 1; lentS
Z=Zt 1; re
return t:
Point t u o S
BD show()
diensiorzG
cout<<"The elements are:\n" cout<<"x:"<<this-x<<". CferiCheme
y:<<this-y <<",z:'"'<<this-z: nt re: -
Dci8,y:2,2:
int main(0
3D pti(2,4,5), pt2(7,1,3):
cout<<"Point one's dimensions before increment are:"<<
ptl.show(0:
*ptl;
cout<<"Point one's dimensions after increment are:"'<<
ptl.show):
cout<<"Point two's dimensions before increment are:"<<
pt2.show):
pt2++;
Maharashtra State Board of Technology (K Scheme) 111
Object Oriented Programming Using C+-+ (313304)
cout<<"Point two's dimensions after increment are:"<
pt2.show0:
return 0:
b) #include <iostream.h>
Outpt
/C+t program to show unary
/ operator overloading Eee t
#include <iostream> ncheS
using namespace std: Secremet
STATE
RA
class Distance
|public:
int feet, inch;
/Constructor to initialize
/ the object's value
Distance(int f, int i)
TECH
this->feet =f:
this->inch = i;
/Overloading(-) operator to
l perform decrement operation
l of Distance object
voidoperator-)
feet--;
inch--;
cout << "nFeet & Inches(Decrement):" <<
feet << " << inch:
Driver Code
int main(0
Distance d1(8, 9);
I/Use (-) unary operator by
l single operand
-dl;
return 0;