Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.

Similar presentations


Presentation on theme: "1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define."— Presentation transcript:

1 1 Overloading functions 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: void swap(int& x, int& y) { int temp = x; x = y; y = temp; } void swap(char& x, char& y) { char temp = x; x = y; y = temp; } int main () { int nums[5] = {5,2,3,4,1}; char letters[3] = {‘t’, ‘a’, ‘r’}; swap(nums[0], nums[4]); swap(letters[0], letters[3]); return 0; } compiler calls this swap() compiler calls this swap()

2 2 Overloading functions Operators such as =, ==, +, etc. are often overloaded for various user-defined types. We have already seen examples of an overloaded operator= and operator== Can we overload << for objects of our MemberInfo class so that we can do: and have the name and age printed on the screen? The answer is yes, but... MemberInfo president(“J. Smith”, 50); cout << president << endl;

3 3 Overloading functions In cout << president; the left operand of << is cout and NOT a MemberInfo object. Therefore, the operator<< may not be a member function of MemberInfo However, operator<< must be able to access the private members of MemberInfo. Both of these requirements can be satisfied, if the overloaded operator<< becomes a friend of MemberInfo.

4 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 5 friend functions A class may allow a function to access its private data members by declaring as a friend function. Example: class Torpedo; class Ship { friend bool HaveCollided(Torpedo&, Ship& ); private: Shiptype type; char *name; Coords position; public:... };... bool HaveCollided(Torpedo& t, Ship& s) {... }

6 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 CityNetwork { private: City *citylist; Road *highways; public:... }; class City { friend class CityNetwork; private: Coords latitude; Coords longitude; public:... }; Now, CityNetwork can access latitude and longitude

7 7 evil friends! Friendship may only be granted, not taken. Friend functions and classes violate the principles on 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 8 back to overloading << As discussed earlier, we have strong justification for making the overloaded operator<< a friend of our MemberInfo class. But what does its implementation look like? ostream& operator<< (ostream& output, const MemberInfo& member) { output << “Name = “ << member.name << endl << “Age = “ << member. age << endl; return output; }

9 9 Overloading gone wild Will the following code compile? int add(int a, int b) { return a+b; } double add(double a, double b) { return a+b; } int main () { cout << add(3.14, 99) << endl; return 0; }

10 10 Overloading gone really wild Will this code compile? class A { private: int a; public: A() {a=1;} int incr() { a ++; return a; } friend int incr(int x); }; int incr(int x) { x++; return x; } double incr(double x) { x++; return x; }


Download ppt "1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define."

Similar presentations


Ads by Google