Presentation is loading. Please wait.

Presentation is loading. Please wait.

of functions, methods & operators

Similar presentations


Presentation on theme: "of functions, methods & operators"— Presentation transcript:

1 of functions, methods & operators
Overloading of functions, methods & operators

2 Overloading Definition: Declaring two or more operations for a single function, method or operator, depending on the number and data types of the arguments or operands.

3 Overloading For functions and methods: Two or more functions/methods with the same name/class but that differ by number, order and data type of arguments.

4 Overloading Purpose: - to give options to programmers using the code, often defaults. - to define the action of an operator on objects.

5 Overloaded Functions Examples: functions // prototypes
void print(float f); void print(float f, int ndpdp); void print(float f, int ndpdp, int width);

6 Overloaded Functions Examples: functions // print a float number, no formatting void print(float f) { cout << f; }

7 Overloaded Functions Examples: functions // print a float number with precision void print(float f, int ndpdp) { cout << fixed << setprecision(ndpdp); cout << f; }

8 Overloaded Functions Examples: functions // print with precision and in field width void print(float f, int ndpdp, int width) { cout << fixed << setprecision(ndpdp); cout << setw(width) << f; }

9 Overloaded Functions Examples: functions const float pi = ; void main() { print(pi); // no formatting print(pi, 3); // 3 past dec point print(pi, 3, 10); // 3 pdp and field of } // width 10

10 Overloaded Functions Examples: functions - using defaults const int DEF_NDPDP = 2; const int DEF_WIDTH = 10;

11 Overloaded Functions Examples: functions - using defaults // use default precision and width void print(float f) { cout << fixed << setprecision(DEF_NDPDP); cout << setw(DEF_WIDTH) << f; }

12 Overloaded Functions Examples: functions - using defaults // use default width void print(float f, int ndpdp) { cout << fixed << setprecision(ndpdp); cout << setw(DEF_WIDTH) << f; }

13 Overloaded Functions Examples: functions - using (no) defaults // using no defaults void print(float f, int ndpdp, int width) { cout << fixed << setprecision(ndpdp); cout << setw(width) << f; }

14 Overloaded Functions Examples: functions - using defaults, even better // invoke "no defaults" function with defaults void print(float f) { print(f, DEF_NDPDP, DEF_WIDTH); } void print(float f, int ndpdp) { print(f, ndpdp, DEF_WIDTH);

15 Overloaded Methods The exact same idea as with functions, but in a class class fraction { private: int numer, int denom; public: void set(int n); // def denom=1 void set(int n, int d); };

16 Overloaded Methods void fraction::set(int n, int d) { numer = n; denom = d; } void fraction::set(int n) { set(n,1); // invoke no-def overloaded } // method with default value

17 Overloaded Methods void main() { fraction x,y; // both constr. to 0/1 x.set(1,2); // numer=1, denom=2 y.set(3); // numer=3, denom=1 }

18 Overloaded Operators Some operators we've used are already Overloaded: int a = 2 + 3; // + adds two ints float b = ; // + adds two floats float e = ; // + adds int to float string c = "Hello"; // + concatenates string d = c + " World"; // two strings

19 Overloaded Operators How can we overload + to add two fractions, when WE created class fraction? fraction a, b, c; a.set(1,2); b.set(1,3); c = a + b; // compiler error!!! cout << c.getNumer() << "/" // want it to << c.getDenom() << endl; // print 5/6

20 Overloaded Operators Syntax: retType operator op (type a1, type arg2) { ... return value/variable/object; }

21 Overloaded Operators // how to add two fractions, resulting in a fraction fraction operator + (fraction a, fraction b) { fraction c; int denom = a.getDenom() * b.getDenom(); int numer = (a.getNumer() * b.getDenom()) + (b.getNumer() * a.getDenom()); c.set(numer, denom); return c; }

22 Overloaded Operators Overloaded operators are NOT part of a Class
and so do not have access to private members/methods However, they are commonly coded in the class Implementation, and prototyped in the class Interface after the class interface declaration.

23 Overloaded Operators class fraction { private: int numer, int denom; public: void set(int n); void set(int n, int d); int getNumer(); int getDenom(); }; fraction operator + (fraction a, fraction b);

24 Overloaded Operators void fraction::set(int n, int d) {numer=n; denom=d;} void fraction::set(int n) { set(n,1); } int fraction:: getNumer() { return numer; } int fraction:: getDenom() { return denom; } fraction operator + (fraction a, fraction b) { fraction c; ... return c; }

25 Overloaded Operators Overloaded operators are NOT part of a Class
and so do not have access to private members/methods UNLESS they are FRIENDS

26 Friend of a class Definition:
A function, other class, or overloaded operator that has access to the private members of a class.

27 Friend of a class Syntax: class classname { friend function prototype;
friend class otherclassname; friend overloaded operator prototype; public: private: };

28 Friend of a class Example: class fraction { friend void main();
friend class fractionList; friend fraction operator + (fraction a, fraction b) ; public: private: };

29 Friend of a class // rewritten as a friend of class fraction fraction operator + (fraction a, fraction b) { fraction c; c.denom = a.denom * b.denom; c.numer = (a.numer * b.denom) + (b.numer * a.denom); return c; }

30 Vocabulary Overloading
Term Definition Overloading Declaring two or more operations for a single function, method or operator, depending on the number and data types of the arguments or operands. Overloaded Function/Method Two or more functions/methods with the same name but with arguments that differ by number, type and order. Overloaded Operator An operator that can operate on two or more types of operands. Friend A function, other class, or overloaded operator that has access to the private members of a class.


Download ppt "of functions, methods & operators"

Similar presentations


Ads by Google