CS201-29 - Introduction to Programming Lecture 29 Private...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Introduction to Programming Lecture 29 Private Public Encapsulation Data Hiding Friend Friend Function class Date { ­­ Friend Function friend functionName ( Argument_list ) ; ­­ } ; Friend functions are NOT the members of the class The class itself declare it’s friend functions The prototype of these functions are written inside the class and the key word frien is appended before the function name Friend functions have access to private and public members of the class Friend Function Example 1 class myClass { friend increment ( myClass , int ) ; private: int topSecret ; public: myClass ( ) { topSecret = 100 ; } void Display ( ) { cout<< “The value of topSecret is “ << topSecret ; } } ; Example 1 void Increment ( myClass A , int i ) { A.topSecret += i ; } main ( ) { myClass x ; x.Display ( ) ; Increment ( x , 10 ) ; x.Display ( ) ; } Example 1 Example 1: Output The value of topSecret is 100 The value of topSecret is 110 class myClassTwo ; class myClassOne { private : int topSecret ; public : void Display ( ) ; myClassOne ( ) { topSecret = 100; } friend AddBoth ( myClassOne , myClassTwo ) ; } ; Example 2 class myClassTwo { private: int topSecret ; public: void Display ( ) ; myClassTwo ( ) { topSecret = 200 ; } friend AddBoth ( myClassOne , myClassTwo ) ; } ; Example 2 main ( ) { myClassOne A ; myClassTwo B ; A.Display ( ) ; B.Display ( ) ; AddBoth ( A , B ) ; } Example 2 int AddBoth ( myClassOne A , myClassTwo B ) { cout << “The value of topSecret in myClassOne object is” << A.topSecret ; cout << “The value of topSecret in myClassTwo object is”<< B.topSecret ; cout << “The sum of topSecret values in “ << “myClassOne and myClassTwo object is ” << A.topSecret + B.topSecret ; Example 2 } class otherClass ; class classOne { private : int topSecret; public : void Display ( ) ; classOne ( ) { TopSecret = 100 ; } friend otherClass ; } ; Example 3 Straight Line y = mx + c Slope Intercept on y axis class straightLine { double slope , intercept ; // member function } ; Straight Line Quadratic y = ax +bx + c 2 class quadratic { private : double a , b , c ; // member function } ; Quadratic Limitations It is NOT Transitive It is NOT Associative ...
View Full Document

Ask a homework question - tutors are online