Overloading of functions methods operators Overloading Definition Declaring

  • Slides: 30
Download presentation
Overloading of functions, methods & operators

Overloading of functions, methods & operators

Overloading Definition: Declaring two or more operations for a single function, method or operator,

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.

Overloading For functions and methods: Two or more functions/methods with the same name/class but

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.

Overloading Purpose: - to give options to programmers using the code, often defaults. -

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

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

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

Overloaded Functions Examples: functions // print a float number, no formatting void print(float f)

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

Overloaded Functions Examples: functions // print a float number with precision void print(float f,

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

Overloaded Functions Examples: functions // print with precision and in field width void print(float

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

Overloaded Functions Examples: functions const float pi = 3. 1415926; void main() { print(pi);

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

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

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

Overloaded Functions Examples: functions - using defaults // use default precision and width void

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

Overloaded Functions Examples: functions - using defaults // use default width void print(float f,

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

Overloaded Functions Examples: functions - using (no) defaults // using no defaults void print(float

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

Overloaded Functions Examples: functions - using defaults, even better // invoke "no defaults" function

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

Overloaded Methods The exact same idea as with functions, but in a class fraction

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

Overloaded Methods void fraction: : set(int n, int d) { numer = n; denom

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

Overloaded Methods void main() { fraction x, y; x. set(1, 2); y. set(3); }

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

Overloaded Operators Some operators we've used are already Overloaded: int a = float b

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

Overloaded Operators How can we overload + to add two fractions, when WE created

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. get. Numer() << "/" // want it to << c. get. Denom() << endl; // print 5/6

Overloaded Operators Syntax: ret. Type operator op (type a 1, type arg 2) {.

Overloaded Operators Syntax: ret. Type operator op (type a 1, type arg 2) {. . . return value/variable/object; }

Overloaded Operators // how to add two fractions, resulting in a fraction operator +

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

Overloaded Operators Overloaded operators are NOT part of a Class and so do not

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.

Overloaded Operators class fraction { private: int numer, int denom; public: void set(int n);

Overloaded Operators class fraction { private: int numer, int denom; public: void set(int n); void set(int n, int d); int get. Numer(); int get. Denom(); }; fraction operator + (fraction a, fraction b) ;

Overloaded Operators void fraction: : set(int n, int d) {numer=n; denom=d; } void fraction:

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

Overloaded Operators Overloaded operators are NOT part of a Class and so do not

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

Friend of a class Definition: A function, other class, or overloaded operator that has

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

Friend of a class Syntax: classname { friend function prototype; friend class otherclassname; friend

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

Friend of a class Example: class fraction { friend void main(); friend class fraction.

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

Friend of a class // rewritten as a friend of class fraction operator +

Friend of a class // rewritten as a friend of class 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; }

Vocabulary Term Definition Overloading Declaring two or more operations for a single function, method

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