Lecture12-Friends__Operator_Overloading

Lecture12-Friends_O - Computer Programming II 1 Friends and Operator Overloading Computer Programming II 1 Lecture 12 Computer Programming II 2

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Programming II 1 Friends and Operator Overloading Computer Programming II 1 Lecture 12 Computer Programming II 2 Learninig Objectives • Friendship • To understand how to declare a friend function • To understand how to declare a friend class • Operator Overloading • To understand the types of operators in C++ • To understand the advantages of operator overloading • To understand how to overload operators as class methods • To understand how to overload operators as friend functions Computer Programming II 3 Friendship • Friendship allows a class to selectively grant other function or class the access to its private and protected members . • A friend function is a function that can access private and protected members of a class, even though the function itself is not a member of the class. • A friend class is a class that can access private and protected members of another class Computer Programming II 4 Friendship • Friendship is granted . So for a function A or class B to access the private and protected members of class C, C must grant A and B the friendship • Friendship is not automatically bi-directional . When A makes B a friend, B does not automatically make A a friend • Use of friend is generally not recommended except when doing certain operator overloading (next lecture) which is still unavoidable Computer Programming II 5 // Problem class Human { string secret; public: Human (string secret) : secret(secret) {} }; void tellSecret (Human h) { cout << h.secret ; // ERROR } int main() { Human h("I have 2 gf:P\n"); tellSecret(h); } Error: Global function cannot access private member Example: Friend Function Computer Programming II 6 // Friend function solution class Human { string secret; friend void tellSecret (Human); public: Human (string secret) : secret(secret) {} }; void tellSecret (Human h) { cout << h.secret ; // Fine } int main() { Human h("I have 2 gf:P\n"); tellSecret(h); } Output: I have 2 gf:P Example: Friend Function Fine. Friend function can access private member. If you cannot change the main function, the only solution is to grant tellSecret() friendship in Human class. tellSecret() is now a friend, not a member . Computer Programming II 7 // Non friend solution, better class Human { string secret; public: Human (string secret) : secret(secret) {} void tellSecret () { cout << secret ; // Fine } }; int main() { Human h("I have 2 gf:P\n"); h. tellSecret () ; } Output: I have 2 gf:P Example: Better Non-Friend Solution Fine. Method can access private member. If you can change the main function, a better solution is to make tellSecret() a method of Human class. Computer Programming II 8 // Problem class Human { string secret; public: Human (string secret) : secret(secret) {} }; class Parrot { public: void tellSecret (Human h) { cout << h.secret ; // ERROR } }; Example: Friend Class Error. Non-member cannot access private member....
View Full Document

This note was uploaded on 02/03/2012 for the course IT 1241 taught by Professor Pang during the Spring '11 term at Multimedia University, Cyberjaya.

Page1 / 32

Lecture12-Friends_O - Computer Programming II 1 Friends and Operator Overloading Computer Programming II 1 Lecture 12 Computer Programming II 2

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online