35
Lab 2: Polymorphism One Name, Many Purposes Two forms: Function Overloading Operator Overloading

Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Embed Size (px)

Citation preview

Page 1: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Lab 2: Polymorphism

One Name, Many Purposes

Two forms: Function Overloading Operator Overloading

Page 2: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Function Overloading

Two or more functions can share the same name provided that: Type of their arguments differsOR Number of their arguments differs

To overload a function, simply declare and define all required versions.

Page 3: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

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;

}

Page 4: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Operator Overloading

Operator overloading is just a type of function overloading.

An operator is always overloaded relative to a user-defined type, such as a class.

To overload an operator, you create an operator function.

Page 5: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Member Operator function

An operator function is often a member of the class for which it is defined.

Syntax:return-type class-name::operator#(arg-list){

// operation to be performed}

Page 6: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Binary Operator

When a member operator function overloads a binary operator, the function will have only one parameter.

Consider: x + yx: object that generates the call to the operator functiony: parameter of the operator function

Page 7: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Example: coord class

class coord {int x, y; // coordinate values

public:// Constructor function is overload

edcoord() {x = 0; y = 0;}coord(int i, int j) {x = i; y = j;} coord operator+(coord ob2);

}

Page 8: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Example: operator+()

// Overload + relative to coord classcoord coord::operator+(coord ob2) {

coord temp;temp.x = x + ob2.x;temp.y = y + ob2.y;return temp;

}

Page 9: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

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;

}

Page 10: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Example: coord classclass coord {

int x, y; // coordinate valuespublic:

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

}

Page 11: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Overloading – and =

The operator –() can be implemented similarly to operator+(), and we omit the details.

We focus on the assignment operator function, operator=().

Page 12: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Example: operator=()

void coord::operator=(coord ob2) {x = ob2.x;y = ob2.y;

}int main() {

coord o1(4,6), o2;o2 = o1;return 0;

}

Page 13: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Assignment Operator

In C++, the following type of statement is syntactically correct:a = b = c = d = 0;

However, in our example, the following statement is invalid (why?):o3 = o2 = o1;

How to modify our program so as to support the above statement?

Page 14: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Introducing this pointer

this is a pointer: automatically passed to any member

function when it is called. pointing to the object that generates the

call. For example, consider:

ob.f1(); // assume ob is an objectf1() is automatically passed a pointer

to ob. This pointer is referred to as this.

Page 15: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

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 validreturn 0;

}

Page 16: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Overload + for (object + integer)

Suppose you want to overload operator+() so that you can add an integer value to a coord object.

For example,use the statement o1 + 3 to represent

o1.x + 3 and o1.y + 3

Page 17: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

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

}

Page 18: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Overload + for (object + integer)

coord::operator+(int i) {coord temp;

temp.x = x + i;temp.y = y + i;return temp;

}

Page 19: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

How about integer + object?

What happens when the compiler sees the following statement?

o3 = 19 + o1;This statement generates a compile-

time error.How to solve this problem?

Page 20: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Introducing Friend Functions

A friend is NOT a member of a class but still has access to its private elements.

Situations when friend functions are useful: Operator overloading Creation of certain types of I/O functions One function has to access the private

members of two or more different classes.

Page 21: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Example: friend function

class myclass {int n, d;

public:myclass(int i, int j) {n = i; d = j;}//declare a friend of myclassfriend int isfactor(myclass ob);

}

Page 22: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

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;

}

Page 23: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Example: friend function

int main() {myclass ob1(10,2);

if (isfactor(ob1))cout << “2 is a factor of 10\n”;

return 0;}

Page 24: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Friend Function

It is important to understand that a friend function is NOT a member of the class.

For example, // wrong; isfactor() is not a member functionob1.isfactor()

Friends are called just like regular functions.

Page 25: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

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

}

Page 26: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Overload for integer + object

coord operator+ (int i, coord ob1) {coord temp;

// private variables of ob1 can be accessedtemp.x = ob1.x + i;temp.y = ob1.y + i;return temp;

}

Page 27: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

C++ I/O Basics

C++ I/O system operates through streams, e.g. cin, cout, …etc.

The ios class contains many member functions and variables that control the operation of a stream.

Each C++ stream has associated with it a number of format flags that determine how data is displayed.

Page 28: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Some Format Flags

enum {skipws = 0x0001;

:dec = 0x0010,oct = 0x0020,hex = 0x0040,

:};

Page 29: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Formatted I/O: Hexadecimal

Using setf() and unsetf() to set and unset a flag.Example:

cout.unsetf(ios::dec); cout.setf(ios::hex);cout << 10;

In the above example, “a” will be displayed, which is the hexadecimal representation of 10.

Page 30: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

width() and fill()

A minimum field width can be specified by using the width() function.

Example:cout.width(10);cout << “hello” << “\n”;cout.fill(‘%’);cout.width(10);cout << “hello” << “\n”;

Page 31: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

width() and fill() (cont’d)

The program displays the following output:

hello

%%%%%hello

Page 32: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Overload Insertion Operator

In C++, the output operation is called an insertion and the << is called the insertion operator.

In the next example, you’ll learn how to overload C++’s insertion operator <<.

Page 33: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

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

}

Page 34: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Example: Overload <<

ostream &operator<<(ostream &out, coord ob){

out << ob.x << “, ” << ob.y << ‘\n’;return out;

}

Page 35: Lab 2: Polymorphism zOne Name, Many Purposes zTwo forms: yFunction Overloading yOperator Overloading

Example: Overload <<

int main() {coord a(1,1);cout << a;return 0;

}This program displays the following:

1, 1