Introduction To Operator Overloading: Cout NX + Ny Endl
Introduction To Operator Overloading: Cout NX + Ny Endl
Introduction To Operator Overloading: Cout NX + Ny Endl
. !" # $
%&
. ++. - . +)
. (..... =
" ' ( )$ ' $ * ++ $ '
+ ,' * -
Mystring cStrin
!!!!!!! !. 0 )&
2 3 / 4 % 5 2 / ( ' $ ) + - 1!/ !
52 7 ( ' $ )+ , ' 6 *
2 4 %
)$ " ) 5 ( ' $ )+ / $ #'
'
. 5 + / #!
, ' ' + %
... , +
- 5 2 5 9 %+
$ 8 2
:: (%
, ;7 5 ( # )$ - %.. !
3 / + )$
3 / + )$ , ;7 5
++ 7
(; ' $ ' )$ < ' 52
:= (! - ! > 2
( ' $ )+ ( ' $ )+ '4 % -
% + ! - 524 % 6! 7 "
. + = ! =
' $ 1& ! - ! ' ' $ ! -
.
. = - <52
2-Overloading the arithmetic operators
2 $ <52 ' / ? * ? -? + 2 / $- *%
. $ 1 '@ " / < * $
6 ! 7 A(4 % 17 ) )$ 8 1 ' %5
. &% ; /
& " % ' $ . + $ $/
*
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents) { m_nCents = nCents; }
int main()
{
Cents c #)%
#*%
'
std::cout << "I have " << cCentsSum .GetCents() << " cents." <<
std::endl;
+
}
: %. +
I have 14 cents.
$ 2 * $
% / $ <
.
! " # $ $ %
: & $
/
Cents operator+(const Cents &c1, const Cents &c2)
{
// use the Cents constructor and operator+(int, int)
return Cents(c1.m_nCents + c2.m_nCents);
}
2 D %' C7
: 52 /- ! ($
% %;
'
Cents operator+
%' - 7
- C7 % %& ( % !
Cents cCentsSum & ! " # %
: %& )$ 6 D
Cents operator+(const Cents &c1, const Cents &c2)
. *! - !
# E
EE $
/# !
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents) { m_nCents = nCents; }
. # 7 1 6 ' - - 7 B
.! ' - $ 9 6 + )$
( & = $ 5): # 7
!
. " %$ %. + 4 % 5( 7 8
2 8 %5 )$. ( & 9 $ 4;
. 5 #' ( , 4;
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents) { m_nCents = nCents; }
int main()
{
#5% )
) #5%
66 7 8"9 &: #% & 66
66 7 8"9 &: #% & 66
+
}
% /- $ 0
#5% )
( & "#$
. (; ) ' ! #
-: * * &
class MinMax
{
private:
int m_nMin; // The min value seen so far
int m_nMax; // The max value seen so far
public:
MinMax(int nMin, int nMax)
{
m_nMin = nMin;
m_nMax = nMax;
}
int main()
{
"; # + >%
"; #* %
"; # %
std::cout << "Result: (" << cMFinal.GetMin() << ", " <<
cMFinal.GetMax() << ")" << std::endl;
+
}
' ! D 8" D %F / 4 % ' ' + 52
. %5 ! ( 8 ) % (; )
! % ' $
%' + %'
+ %'
%' +
%. +
Result: (3, 16)
(:... 6
3-Overloading the I/O operators
:
>> and <<
! "# $% &' (' )
* % &+ ,#- &.
/01-
class Point
{
private:
double m_dX, m_dY, m_dZ;
public:
Point(double dX=0.0, double dY=0.0, double dZ=0.0)
{
m_dX = dX;
m_dY = dY;
m_dZ = dZ;
}
52 % / 4 % / ' 17 - ! 5
@ @ #>&+ )&+ A&+%
cout << "(" << cPoint.GetX() << ", " <<
cPoint.GetY() << ", " <<
cPoint.GetZ() << ")";
%& 5( ( % ! @ ** )) 4
@ @ #>&+ )&+ A&+%
cout << cPoint;
4; )) . ( % + 4; ! .
&" + , +, $
. - /- 4 ( )) ) * -
- $ ' cout << cPoint <52 $'
. %' 4 %2 - %' 2
. - 4 % %' 2
%&
friend ostream& operator<< (ostream &out, Point &cPoint);
* - , $ ** ++ !
, , 17 - 6
.. ! )) 2$ $ !
6 * $
/
class Point
{
private:
double m_dX, m_dY, m_dZ;
public:
Point(double dX=0.0, double dY=0.0, double dZ=0.0)
{
m_dX = dX;
m_dY = dY;
m_dZ = dZ;
}
:!!.. / 5 ,' # 2
/H ! 4 % ! 2 D,' # !
* 0 $ 4;
cout << cPoint << endl;
# ) ! ". D,' A 5 $
%& 9 (
$ % (
cout << cPoint << endl;
.! / - 06 ! ('
! <5( ('
"cout << cPoint )<< endl;
- 4 D- - #
5%2 2 % A
%C"
cout << cPoint
2 & ' - 5; -
C" ! $% '
0 ** 1
. $ % 1!/ 2 & - <52
- , D,' # 2 5(
%& 5; . - , D,' A D # !
5; .
"cout << cPoint )<< endl;
+
}
%& %. '/
(2.0, 3.0, 4.0) (6.0, 7.0, 8.0)
52 ;$/ ! . 6 * $* 52
(:... ! - , %' 2
4 % * 52
class Point
{
private:
double m_dX, m_dY, m_dZ;
public:
Point(double dX=0.0, double dY=0.0, double dZ=0.0)
{
m_dX = dX;
m_dY = dY;
m_dZ = dZ;
}
)) ** $ / ! * 52
int main()
{
using namespace std;
cout << "Enter a point: " << endl;
Point cPoint;
cin >> cPoint;
+
}
$
/ = > ;
3.0 4.5 7.26
%& %. '/
You entered: (3, 4.5, 7.26)
8 7; 2 ! - /-
5% * 7 $
*- % % * * % *
5%2 7-
. $ 52
4-Overloading operators using member functions
! ' * +&,#"
!. () - ' ( )( ' (=) ' -
: ' 0 1 -/ ! ' .)
! ) ....(-) + *' 2 #
#/
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents) { m_nCents = nCents; }
// Overload -cCents
friend Cents operator-(const Cents &cCents);
};
- -, % (;
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents) { m_nCents = nCents; }
// Overload -cCents
Cents operator-();
};
/ A0 % 52 /- ' - &
%. : $ % < - -,
%' - 4; ) & 2 + 7 - -,
. $ 7 % 4 % 4;
. ) F 2 + )$ < -
&%; $ %I ' %5
52 *
Cents Cents::operator-();
BC- DEFGHI2J KHLG MNOP
Cents operator-(const Cents *this)
%& ( # )
)! ) 2
.
)$ ' )
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents) { m_nCents = nCents; }
)
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents) { m_nCents = nCents; }
! #" 4*5 +3
"
!0 #" ! , */ .
.#
2- 0 6' 0 ! 4
) ' # 6 ! '! *8 7 #"
... 91 &,
5-Overloading the increment and decrement operators
*2*:decrement (--) (++) increment /; I$;<!
prefix D# ( eg. nX++; nY--;) postfix 3 # =# <
. ( eg. ++nX; )
--nY;
Digit& operator++();
Digit& operator--();
Digit& Digit::operator++()
{
// If our number is already at 9, wrap around to 0
if (m_nDigit == 9)
'( Q +
// otherwise just increment to next number
else
++m_nDigit;
return *this;
}
Digit& Digit::operator--()
{
// If our number is already at 0, wrap around to 9
if (m_nDigit == 0)
'( Q R
// otherwise just decrement to next number
else
--m_nDigit;
return *this;
}
- ++ ; < 2 3 2 : .& %! - : $ (5
.& % !0 4"* $ # --
#, $ ! , 5 2 = # : !94*5
.$ ! % , # "
'
5 > ,! '( ! 2 = 2 #"
2 =#" % * , &, . 2 99 8
Digit& Digit::operator++()
{
// If our number is already at 9, wrap around to 0
if (m_nDigit == 9)
'( Q +
// otherwise just increment to next number
else
++m_nDigit;
return *this;
}
Digit& Digit::operator--()
{
// If our number is already at 0, wrap around to 9
if (m_nDigit == 0)
'( Q R
// otherwise just decrement to next number
else
--m_nDigit;
return *this;
}
Digit Digit::operator++(int)
{
// Create a temporary variable with our current digit
Digit cResult(m_nDigit);
5 - (71
** - &&1
** -1
: %1
!. * % 7 !. ) @
$ * 6! & B 4 2' %; 2
.
,' J)J F 8 4 % ( %; 4;
. " B $ D %' -
$
%& 2
D 4; " B $ %' - ,' J -
" B $ <'. $ D %' - ' B
!!:::!!!!!!!!.
D0; D 8 / 2$ %& <52 ! 7A
.( 4 1 ) M ;4 % * %' -
)$ " 5% 1 - ! )J D 8 ,' J *
)$ D D 4; $ D %' - /
. M ; %' -
! - ' B 0-
%5 C =5 . 8 )J
D2 J ,' J % - @
. &%; ( J
... N ' 52 % & /@ ! (; %
#include <iostream>
using namespace std;
class Digit
{
private:
int m_nDigit;
public:
Digit(int nDigit=0)
{
m_nDigit = nDigit;
}
return *this;
}
Digit& Digit::operator--()
{
// If our number is already at 0, wrap around to 9
if (m_nDigit == 0)
'( Q R
// otherwise just decrement to next number
else
--m_nDigit;
return *this;
}
Digit Digit::operator++(int)
{
// Create a temporary variable with our current digit
Digit cResult(m_nDigit);
Digit Digit::operator--(int)
{
// Create a temporary variable with our current digit
Digit cResult(m_nDigit);
int main()
{
Q Q #>% Q #+%
Q // calls Digit::operator++(int);
system("pause");
return +
}
6-Overloading the subscript operator
0:#4M N K O# ( [] ) ? G 9
: :#4M / ;
" S " T+U A VV ! 8 value 7 in the first element of
the array
class IntList
{
private:
'(" W T +U
};
int main()
{
IntList cMyList;
+
}
public:
void SetItem(int nIndex, int nData) { m_anList[nIndex] = nData;
}
int GetItem(int nIndex) { return m_anList[nIndex]; }
};
int main()
{
IntList cMyList;
W & 7 '# %
+
}
6 D " ) D D " ) 6 D A D2
. 7 G) 2& 5
'$ ;" ! % <= 2 ! 7A
<= 2 * &. )
- -, % #' , - 2 ,
M D' 4% - 2 ' , 2
;"
class IntList
{
private:
'(" W T +U
public:
int& operator[] (const int nIndex);
};
. / A6 % C7 52 %
7-Overloading the parenthesis operator
I $ /; 3/D# C2( ) % #P #20 J+ ,#@=C /;
parameter /Q /06 ( #2 06 3 HG5,&
0$ ;<! 0 0 Rparameter 2 =# <== 0 &. /
.( ) /Q5 6G<A
SG /Q 3/0 A A <( 3+$% #P 0 3+
. 0 prototype
. member function '0 (#+<( 5 % #P I$;<!
* L. ,#-
class Matrix
{
private:
X " Q" "T5UT5U
public:
Matrix()
{
// Set all elements of the matrix to 0.0
# + 5 %
# Y Z + Y Z 5 Y Z %
" Q" "T Y ZUT U +&+
}
};
! % (; A D <= 4 D- 4
. " / 4 % 17 ( O) - ;" ) "
) ! - ( - ) )$ < <= -
. O * %) "
" ! =5 *
% )$ < % () %
. 2 O % ( ;" %
. O ;" 6 * 8
;" N " <B) " $()
: * &
& ! 17 - ) " 4 % -
Matrix cMatrix;
" ;# % 5&>
66 " ;# %
return adData[nRow][nCol];
}
%& %. '/
4.5
() $
/ -
#include <cassert> // for assert()
class Matrix
{
private:
X " Q" "T5UT5U
public:
Matrix()
{
// Set all elements of the matrix to 0.0
# + 5 %
# Y Z + Y Z 5 Y Z %
" Q" "T Y ZUT U +&+
}
return adData[nRow][nCol];
}
void Matrix::operator()()
{
// reset all elements of the matrix to 0.0
# + 5 %
# Y Z + Y Z 5 Y Z %
" Q" "T Y ZUT U +&+
}
;" C () ! %& ( /
Matrix cMatrix;
" ;# % 5&>
cMatrix(); // erase cMatrix
66 " ;# %
%. '/
0
. ( $ *
%6 ! ( / % () ;& *
... $ 52
8-Overloading typecasts
T/ int D# 3/ T/ @ 3+$0 ++ *G 5, ;/#2 '
.typecast L ? @9
5double &.
/D# 3/ U
int nValue = >
double dValue = nValue; // int implicitly cast to a double
. , - ( % ++
% ! #' M& 4 %<B % - %
. /H, ) , %
. /H, ) 4 % C
: 4 % &
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents=0)
{
m_nCents = nCents;
}
8 ) " $ / ! C7 4 %
.
52 4 % ! % -
D C" ) $ #' ' , 8
: - C7 % * &. ) %
void PrintInt(int nValue)
{
cout << nValue;
}
int main()
{
#A%
@ 7 # &: #%% VV ! A
+
}
$
( <52 ! 8" + %5 ! ! ! /
*
% *
% &%; %+ = %5 %
. - *
%+ =$
( .
) % ! 7
: * &
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents=0)
{
m_nCents = nCents;
}
. P =2 0-
. 5 &% ; *
: %& ( .
int main()
{
#A%
@ 7 # % VV ! A
+
}
:.. " ; $
/
, . 5 &. - -
.
. ' 4 0 O *-
) "/ ! = 2 % 59 *-
"$ *2 ' . !
.. 5 ) * ' <52 $ <5( $
* ' 8 <B) 4 % - %
#A%
int nCents = static_cast<int>(cCents);
(A
& , 1 , %
. ) ;& *
. (& , $ * & $
/
*- ' 4 %# % $
/
.... 4 % & - ) %
class Dollars
{
private:
int m_nDollars;
public:
Dollars(int nDollars=0)
{
m_nDollars = nDollars;
}
int main()
{
Q " Q " #R%
PrintCents(cDollars); // cDollars will be cast to a Cents
+
}
%. '/
@%%
.4 ) - D= 5
. *
% D! ( / % ( ;& *
... $ 52