Download presentation
Presentation is loading. Please wait.
Published byClara Siddens Modified over 9 years ago
1
Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading
2
Function Overloading zTwo or more functions can share the same name provided that: yType of their arguments differs OR yNumber of their arguments differs zTo overload a function, simply declare and define all required versions.
3
Exampe: abs() int abs(int n) {return n < 0 ? –n : n;} long abs(long n) {return n < 0 ? –n : n;} double abs(double n) {return n < 0 ? –n : n;} int main() { cout<<“Abs. value of –10:” << abs(-10) << “\n”; cout<<“Abs. value of –10L:” << abs(-10L) <<“\n”; cout<<“Abs. value of –10.1:” << abs(-10.1) <<“\n”; return 0; }
4
Operator Overloading zOperator overloading is just a type of function overloading. zAn operator is always overloaded relative to a user-defined type, such as a class. zTo overload an operator, you create an operator function.
5
Member Operator function zAn operator function is often a member of the class for which it is defined. zSyntax: return-type class-name::operator#(arg-list) { // operation to be performed }
6
Binary Operator zWhen a member operator function overloads a binary operator, the function will have only one parameter. zConsider: x + y x: object that generates the call to the operator function y:parameter of the operator function
7
Example: coord class class coord { int x, y;// coordinate values public: // Constructor function is overloaded coord() {x = 0; y = 0;} coord(int i, int j) {x = i; y = j;} coord operator+(coord ob2); }
8
Example: operator+() // Overload + relative to coord class coord coord::operator+(coord ob2) { coord temp; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; }
9
Example: overloaded + int main() { coord o1(10,10), o2(5,3), o3, o4; o3 = o1 + o2; // a string of additions is allowed. o4 = o1 + o2 + o3; return 0; }
10
Example: coord class class coord { int x, y;// coordinate values public: coord() {x = 0; y = 0;} coord(int i, int j) {x = i; y = j;} coord operator+(coord ob2); coord operator– (coord ob2); coord operator=(coord ob2); }
11
Overloading – and = zThe operator –() can be implemented similarly to operator+(), and we omit the details. zWe focus on the assignment operator function, operator=().
12
Example: operator=() void coord::operator=(coord ob2) { x = ob2.x; y = ob2.y; } int main() { coord o1(4,6), o2; o2 = o1; return 0; }
13
Assignment Operator zIn C++, the following type of statement is syntactically correct: a = b = c = d = 0; zHowever, in our example, the following statement is invalid (why?): o3 = o2 = o1; zHow to modify our program so as to support the above statement?
14
Introducing this pointer zthis is a pointer: yautomatically passed to any member function when it is called. ypointing to the object that generates the call. z For example, consider: ob.f1();// assume ob is an object zf1() is automatically passed a pointer to ob. This pointer is referred to as this.
15
Example: operator=() coord coord::operator=(coord ob2) { x = ob2.x; y = ob2.y; return *this;// return the object } int main() { coord o1(4,6), o2, o3; o3 = o2 = o1;// this statement is now valid return 0; }
16
Overload + for (object + integer) zSuppose you want to overload operator+() so that you can add an integer value to a coord object. zFor example, use the statement o1 + 3 to represent o1.x + 3 and o1.y + 3
17
Example: coord class class coord { int x, y;// coordinate values public: coord() {x = 0; y = 0;} coord(int i, int j) {x = i; y = j;} coord operator+(coord ob2); coord operator+ (int i); }
18
Overload + for (object + integer) coord::operator+(int i) { coord temp; temp.x = x + i; temp.y = y + i; return temp; }
19
How about integer + object? zWhat happens when the compiler sees the following statement? o3 = 19 + o1; zThis statement generates a compile-time error. zHow to solve this problem?
20
Introducing Friend Functions zA friend is NOT a member of a class but still has access to its private elements. zSituations when friend functions are useful: yOperator overloading yCreation of certain types of I/O functions yOne function has to access the private members of two or more different classes.
21
Example: friend function class myclass { int n, d; public: myclass(int i, int j) {n = i; d = j;} //declare a friend of myclass friend int isfactor(myclass ob); }
22
Example: friend function /* This function returns true if d is a factor of n. Notice that the keyword friend is not used in the definition of isfactor(). */ int isfactor (myclass ob) { if (! (ob.n % ob.d)) return 1; else return 0; }
23
Example: friend function int main() { myclass ob1(10,2); if (isfactor(ob1)) cout << “2 is a factor of 10\n”; return 0; }
24
Friend Function zIt is important to understand that a friend function is NOT a member of the class. zFor example, // wrong; isfactor() is not a member function ob1.isfactor() zFriends are called just like regular functions.
25
integer + object (revisited) class coord { int x, y;// coordinate values public: coord() {x = 0; y = 0;} coord(int i, int j) {x = i; y = j;} friend coord operator+(coord ob1, coord ob2); friend coord operator+(coord ob1, int i); friend coord operator+(int i, coord ob1); }
26
Overload for integer + object coord operator+ (int i, coord ob1) { coord temp; // private variables of ob1 can be accessed temp.x = ob1.x + i; temp.y = ob1.y + i; return temp; }
27
C++ I/O Basics zC++ I/O system operates through streams, e.g. cin, cout, …etc. zThe ios class contains many member functions and variables that control the operation of a stream. zEach C++ stream has associated with it a number of format flags that determine how data is displayed.
28
Some Format Flags enum { skipws = 0x0001; : dec = 0x0010, oct = 0x0020, hex = 0x0040, : };
29
Formatted I/O: Hexadecimal zUsing setf() and unsetf() to set and unset a flag. zExample: cout.unsetf(ios::dec); cout.setf(ios::hex); cout << 10; zIn the above example, “a” will be displayed, which is the hexadecimal representation of 10.
30
width() and fill() zA minimum field width can be specified by using the width() function. zExample: cout.width(10); cout << “hello” << “\n”; cout.fill(‘%’); cout.width(10); cout << “hello” << “\n”;
31
width() and fill() (cont’d) zThe program displays the following output: hello %%%hello
32
Overload Insertion Operator zIn C++, the output operation is called an insertion and the << is called the insertion operator. zIn the next example, you’ll learn how to overload C++’s insertion operator <<.
33
Example: Overload << class coord { int x, y;// coordinate values public: coord() {x = 0; y = 0;} coord(int i, int j) {x = i; y = j;} friend ostream &operator << (ostream &out, coord ob); }
34
Example: Overload << ostream &operator<<(ostream &out, coord ob) { out << ob.x << “, ” << ob.y << ‘\n’; return out; }
35
Example: Overload << int main() { coord a(1,1); cout << a; return 0; } zThis program displays the following: 1, 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.