Download presentation
Presentation is loading. Please wait.
Published byMildred Wade Modified over 6 years ago
1
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar functions that can be applied to objects of different types. Example: compiler calls this print() int main () { print("Vana"); print(32); return 0; } void print(string name) { cout << "My name is " << name << endl; } void print(int age) { cout << "I am " << age << " years old" << endl; compiler calls this print()
2
Operator Overloading Operators such as =, ==, +, << etc. are often overloaded for various user-defined types. What does x+y mean (assuming x is an object of type T)? It means "apply operator + to object x, with input y" Another way to write this is x.operator+(y) operator+ is a member of class T. Its prototype is: T operator+ (const T&) 1. Take an argument of type T 2. Add it to the current object 3. Return the result
3
Overloading functions
What does cout << x; mean? In cout << x; the left operand of << is cout and NOT a T object. Therefore, the operator<< cannot be a member function of class T However, operator<< will probably need to access the private data members of T. This can be done through accessors, OR We can declare operator<< as a friend of class T.
4
friend functions In many cases, we would like a function to have access to private data members of the class, without the function being a member of the class. Examples: the << problem we just discussed a function that needs to operate on two or more objects of the same class e.g. a function that takes as arguments two Points and computes the distance between them a function that needs to operate on objects of different classes. e.g. a function HaveCollided that takes as arguments a Ship and a Torpedo object.
5
friend functions A class may allow a function to access its private data members by declaring it as a friend function. Example: class Torpedo; class Ship { private: Shiptype type; char *name; Coords position; public: ... friend bool HaveCollided(Torpedo& , Ship& ); }; ... bool HaveCollided(Torpedo& t, Ship& s) { }
6
friend classes We may also declare a class A to be a friend of class B. This will give A access to the private members of B. IMPORTANT: This does not mean that B has access to the private data members of A class City { private: Coords latitude; Coords longitude; public: ... friend class CityNetwork; }; class CityNetwork { private: City *citylist; Road *highways; public: ... }; Now, CityNetwork can access latitude and longitude
7
evil friends! Friendship may only be granted, not taken.
Friend functions and classes violate the principles of encapsulation and information hiding. They should be avoided whenever possible. You must always have very good reasons for using friends. Keep in mind that a friend function is dependent on the implementation of the class that declared it as a friend. If the implementation changes, the function may need to be modified and will certainly need to be recompiled.
8
back to overloading <<
As discussed earlier, we have strong justification for making the overloaded operator<< a friend of our T class. But what does its implementation look like? class T { int a; int b; ... } ostream& operator<< (ostream& output, const T& member) { output << a << " " << b << endl; return output; } this allows chaining: T obj1, obj2; cout << obj1 << obj2;
9
More overloading See the sample code in ~b11/labs/Overloading on T-lab. Experiment with it!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.