Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator.

Similar presentations


Presentation on theme: "Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator."— Presentation transcript:

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

2 Object parameters l Objects can be passed by: l Value l Reference l Constant Reference

3 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 &); };

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 num 1 den 2 num 3 den 4 num 0 den 1 x z y num 1 den 2 num 3 den 4 B A main() add() Object parameters: Diagram of version 1 when z.add(x,y) executes

7 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: Explanation of version 1 when z.add(x,y) executes

8 num 1 den 2 num 3 den 4 num 0 den 1 x z y B A main() add() Object parameters: Diagram of version 2 when z.add(x,y) executes

9 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: Explanation of version 2 when z.add(x,y) executes

10 num 1 den 2 num 3 den 4 num 0 den 1 x z y B A main() add() Object parameters: Diagram of version 3 when z.add(x,y) executes

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 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 Functions

13 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?

14 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

15 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

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

17 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

18 invocation: Fraction x(1,2), y(3,4), z; z = add(x,y); add() – Friend Version

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

20 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 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

22 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

23 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

24 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); Friend Functions

25 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

26 We can make our own = operator: in main Fraction x(1,2); Fraction z; z.operator=(x); //or just z = x; Operator Overloading

27 #include #include "fraction_class.h" void main() { int i, j; 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; } Fraction x is 1/2 Fraction z is 1/2

28 What happens when Fraction::operator=() is called? Operator Overloading

29 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

30 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

31 How would we overload the + operator for fractions?? Two ways: 1) use analogy to the operator= example 2) use a friend function Operator Overloading


Download ppt "Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator."

Similar presentations


Ads by Google