1 / 31

Overview

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 {

ivrit
Download Presentation

Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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(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. 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 z num 0 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 z num 0 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 z num 0 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 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. 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; }

  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: friendFraction 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; }

  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,BankAccount,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 Fraction::operator= (Fraction rightSide) { num = 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 <iostream.h> #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 Fraction z is 1/2

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

  29. 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??

  30. 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.

  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

More Related