Transcript 22-Mar-06

Overview
Class Scope
Review:
Object parameters passed by
value
reference
constant reference
Friend function
Overloading operator
Object parameters

Objects can be passed by:
 Value
 Reference
 Constant Reference
Fraction Class
// class declaration
class Fraction
{
private:
int num, den;
public:
Fraction(int=0, int=1);
void add(Fraction, Fraction);
void reduce();
float decimal();
void reveal(int &, int &);
};
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 &);
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
Object parameters:
Diagram of version 1 when z.add(x,y) executes
main()
x
num
den
y
num
den
1
2
3
4
num
den
0
1
z
add()
A
num
den
1
2
B
num
den
3
4
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
Object parameters:
Diagram of version 2 when z.add(x,y) executes
main()
x
num
den
y
num
den
1
2
3
4
num
den
0
1
z
add()
A
B
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.
Object parameters:
Diagram of version 3 when z.add(x,y) executes
main()
x
num
den
y
num
den
1
2
3
4
num
den
0
1
z
add()
A
B
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
parameter, even the formal parameters.
actual
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
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
Friend or Foe??
Let's look at Fraction again. Consider the
Fraction::add() function.
It's a __________ function.
Can it be restructured as a friend function?
Friend Functions
To convert Fraction::add() 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
add() – Member Version
declaration:
void add(Fraction, Fraction);
definition:
void Fraction::add(Fraction A, Fraction B)
{
num = A.num*B.den + B.num*A.den;
den = A.den * B.den;
}
add() – Member Version
invocation:
Fraction x(1,2), y(3,4), z;
z.add(x,y);
//now z holds the value 5/4
add() – Friend Version
declaration:
friend Fraction add(Fraction, Fraction);
definition:
Fraction add(Fraction A, Fraction B)
{
Fraction C;
C.num = A.num*B.den +B.num*A.den;
C.den = A.den * B.den;
return C;
}
add() – Friend Version
invocation:
Fraction x(1,2), y(3,4), z;
z = add(x,y);
Friend Functions
Member function example:
z.reduce();
Friend function example:
z = reduce(z);
How would you make reduce() a friend function???
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!
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,BankAccount,BankAccount)
Another example: constructor functions:
Triangle();
Triangle(int, int, int);
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;
}
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;
}
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);
Operator Overloading
We can also overload C++ operators.
We can make our own = operator:
in .h file
void operator = (Fraction);
in .cpp file
void Fraction::operator= (Fraction rightSide)
{
num = rightSide.num;
den = rightSide.den;
}
Operator Overloading
We can make our own = operator:
in main
Fraction x(1,2);
Fraction z;
z.operator=(x);
//or just
z = x;
#include <iostream.h>
#include "fraction_class.h"
void main()
{
int i, j;
Fraction x is 1/2
Fraction z is 1/2
Fraction z is 1/2
Fraction x(1,2);
x.reveal(i,j);
cout<<"Fraction x is "<<i<<"/"<<j<<endl;
Fraction z;
z.operator=(x);
z.reveal(i,j);
cout<<"Fraction z is "<<i<<"/"<<j<<endl;
z = x;
z.reveal(i,j);
cout<<"Fraction z is "<<i<<"/"<<j<<endl;
}
Operator Overloading
What happens when Fraction::operator=() is called?
Operator Overloading
in .h file
void operator = (Fraction &);
in .cpp file
void Fraction::operator= (Fraction & rightSide)
{
num = rightSide.num;
den = rightSide.den;
}
What is the danger now??
Operator Overloading
in .h file
void operator = (const Fraction&);
in .cpp file
void Fraction::operator= (const Fraction & rightSide)
{
num = rightSide.num;
den = rightSide.den;
}
This is an example of "overloading" an operator.
Operator Overloading
How would we overload the + operator for fractions??
Two ways:
1) use analogy to the operator= example
2) use a friend function