Overview - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Overview

Description:

Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 32
Provided by: humb60
Category:

less

Transcript and Presenter's Notes

Title: Overview


1
Overview
  • Class Scope
  • Review
  • Object parameters passed by
  • value
  • reference
  • constant reference
  • Friend function
  • Overloading operator

2
Object parameters
  • Objects can be passed by
  • Value
  • Reference
  • Constant Reference

3
Fraction Class
  • // class declaration
  • class Fraction
  • private
  • int num, den
  • public
  • Fraction(int0, int1)
  • void add(Fraction, Fraction)
  • void reduce()
  • float decimal()
  • void reveal(int , int )

4
Object parameters
There are three ways to declare the add() member
function of the Fraction class version 1 void
add(Fraction, Fraction) version 2 void
add(Fraction , Fraction ) version 3 void
add(const Fraction , const Fraction )
5
Object parameters
Here is part of the main() Fraction
x(1,2) Fraction y(3,4) Fraction z
  z.add(x,y) Function invocation same
regardless of its declaration
6
Object parameters Diagram of version 1 when
z.add(x,y) executes
main()

num 1
x
den 2
add()
num 3
y

den 4
num 1
A
den 2
num 0
z
num 3
den 1
B
den 4
7
Object parameters Explanation of version 1 when
z.add(x,y) executes
Parameters passed by value A copy of the actual
parameters x and y are sent to formal parameters
A and B. This is OK for small object but very
inefficient for larger objects
8
Object parameters Diagram of version 2 when
z.add(x,y) executes
main()

num 1
x
den 2
add()
num 3
y

den 4
A
num 0
z
den 1
B
9
Object parameters Explanation of version 2 when
z.add(x,y) executes
Parameters passed by reference More
efficient Dangerous function can inadvertently
(or maliciously) change an
actual parameter (in the main) by changing the
formal parameter. It is not the case here.
10
Object parameters Diagram of version 3 when
z.add(x,y) executes
main()

num 1
x
den 2
add()
num 3
y

den 4
A
num 0
z
den 1
B
11
Object parameters Explanation of version 3 when
z.add(x,y) executes
  • Parameters passed by constant reference
  • Both of the best world security and efficiency
  • No Danger compiler will not allow to modify the
    actual parameter, even the formal
    parameters.
  • If the function tries to do this
  • A.num 1000 //Will not be allowed by the
    compiler
  • int i
  • i A.num //Will be allowed by the compiler

12
Friend Functions
A friend function is not a member function of a
class but is allowed to touch anything protected
or private It is not a category such as Private
or Public
13
Friend or Foe??
  • Let's look at Fraction again. Consider the
  • Fractionadd() function.
  • It's a __________ function.
  • Can it be restructured as a friend function?
  •  

14
Friend Functions
  • To convert Fractionadd() function from a member
    function to a friend function
  • 1. put it onto the friends list
  • 2. change its return type
  • 3. remove its class scope operator

15
add() Member Version
  • declaration
  • void add(Fraction, Fraction)
  • definition
  • void Fractionadd(Fraction A, Fraction B)
  • num A.numB.den B.numA.den
  • den A.den B.den

16
add() Member Version
  • invocation
  • Fraction x(1,2), y(3,4), z
  • z.add(x,y) //now z holds the value 5/4
  •  

17
add() Friend Version
  • declaration
  • friend Fraction add(Fraction, Fraction)
  •  
  • definition
  • Fraction add(Fraction A, Fraction B)
  • Fraction C
  • C.num A.numB.den B.numA.den
  • C.den A.den B.den
  • return C

18
add() Friend Version
  • invocation
  •  
  • Fraction x(1,2), y(3,4), z
  • z add(x,y)

19
Friend Functions
  • Member function example
  •  
  • z.reduce()
  •  
  • Friend function example
  •  
  • z reduce(z)
  •  
  • How would you make reduce() a friend function???

20
Friend Functions
Review Function overloading What does it mean
to "overload" a function?   same function name
with different argument types   functions must
differ by more than return type!  
21
Friend Functions
An example a findLowest function   char
findLowest(char, char, char) int findLowest(int,
int, int) float findLowest(float, float,
float) double findLowest(double, double,
double) long findLowest(long, long,
long) BankAccount findLowest(BankAccount,BankAcco
unt,BankAccount)   Another example constructor
functions  Triangle() Triangle(int, int, int)
22
Friend Functions
  • It's a very short jump from the friend add
    function to
  • an overloaded operator
  •  
  • Fraction add(Fraction A, Fraction B)
  • fraction C
  • C.num A.num B.num
  • C.den B.den A.den
  • C.reduce()
  • return C

23
Friend Functions
  • Fraction operator (fraction A, fraction B)
  • Fraction C
  • C.num A.num B.num
  • C.den B.den A.den
  • C.reduce()
  • return C

24
Friend Functions
  • Now in main instead of
  •   z add(x, y)
  • we can say
  •   z x y
  •  
  • How would the declaration of the fraction class
  • need to change?
  •  
  • In fraction.h
  • friend Fraction add(Fraction, Fraction)
  •  
  • friend Fraction operator(Fraction, Fraction)

25
Operator Overloading
We can also overload C operators. We can make
our own operator in .h file void operator
(Fraction)   in .cpp file void
Fractionoperator (Fraction rightSide) n
um rightSide.num den rightSide.den  
26
Operator Overloading
We can make our own operator   in
main   Fraction x(1,2) Fraction
z z.operator(x) //or just z x
27
include ltiostream.hgt include "fraction_class.h"
void main() int i, j Fraction x(1,2)
x.reveal(i,j) coutltlt"Fraction x is
"ltltiltlt"/"ltltjltltendl Fraction z
z.operator(x) z.reveal(i,j) coutltlt"Fraction
z is "ltltiltlt"/"ltltjltltendl   z x
z.reveal(i,j) coutltlt"Fraction z is
"ltltiltlt"/"ltltjltltendl
Fraction x is 1/2 Fraction z is 1/2 Fraction z is
1/2
28
Operator Overloading
What happens when Fractionoperator() is
called?                         
29
Operator Overloading
in .h file void operator (Fraction )   in
.cpp file   void Fractionoperator (Fraction
rightSide) num rightSide.num den
rightSide.den   What is the danger now??
30
Operator Overloading
in .h file void operator (const
Fraction)   in .cpp file   void
Fractionoperator (const Fraction
rightSide) num rightSide.num den
rightSide.den This is an example of
"overloading" an operator.
31
Operator Overloading
How would we overload the operator for
fractions??   Two ways   1) use analogy to
the operator example 2) use a friend function
Write a Comment
User Comments (0)
About PowerShow.com