Upcoming SlideShare
×

# Lecture 7 Templates, Friend Classes

834 views

Published on

Published in: Technology, Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
834
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
37
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Lecture 7 Templates, Friend Classes

1. 1. Lecture # 7
2. 2. Function Templates <ul><li>We use function templates to write generic functions that can be used with arbitrary types. </li></ul><ul><li>For example, one can write searching and sorting routines which can be used with any arbitrary type. </li></ul><ul><li>Lets have an example………. </li></ul>
3. 3. <ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>//max returns the maximum of the two elements </li></ul><ul><li>template <class T> </li></ul><ul><li>T max(T a, T b) </li></ul><ul><li>{ </li></ul><ul><li>return a > b ? a : b ; </li></ul><ul><li>} </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>cout << &quot;max(10, 15) = &quot; << max(10, 15) << endl ; </li></ul><ul><li>cout << &quot;max('k', 's') = &quot; << max('k', 's') << endl ; </li></ul><ul><li>cout << &quot;max(10.1, 15.2) = &quot; << max(10.1, 15.2) << endl ; </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
4. 4. <ul><li>The concept of encapsulation and data hiding dictate that nonmember functions should not be able to access the object’s private or public data. </li></ul><ul><li>The policy is that if you are not a member, you cannot get in. however there are situations where such rigid discrimination leads to considerable inconvenience. </li></ul>Friend Functions
5. 5. Example: <ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>class beta; </li></ul><ul><li>class alpha </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>int data1; </li></ul><ul><li>public: </li></ul><ul><li>alpha( ) </li></ul><ul><li> { </li></ul><ul><li> data1 = 3; </li></ul><ul><li> } </li></ul><ul><li>~alpha( ) </li></ul><ul><li> { } </li></ul><ul><li>friend int function1( alpha, beta ); // Friend Function </li></ul><ul><li>}; </li></ul>
6. 6. <ul><li>class beta </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>int data1; </li></ul><ul><li>public: </li></ul><ul><li>beta( ) </li></ul><ul><li> { </li></ul><ul><li> data1= 7; </li></ul><ul><li> } </li></ul><ul><li>~beta( ) </li></ul><ul><li> { } </li></ul><ul><li>friend int function1( alpha, beta ); // Friend Function </li></ul><ul><li>}; </li></ul>
7. 7. <ul><li>int function1( alpha a, beta b) // Function Definition </li></ul><ul><li>{ </li></ul><ul><li>int temp; </li></ul><ul><li>temp = a.data1 + b.data1; </li></ul><ul><li>return temp; </li></ul><ul><li>} </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>alpha aa; </li></ul><ul><li>beta bb; </li></ul><ul><li>cout<<“____”<<function1(aa,bb); </li></ul><ul><li>getche(); </li></ul><ul><li>} </li></ul><ul><li>Output of the Program: </li></ul>
8. 8. <ul><li>In the above program we want the </li></ul><ul><li>function1( alpha a, beta b); to have access both the private data members I.e. data1 of beta and alpha class. So we make it a friend function. It is declared with the friend keyword in both the classes. </li></ul><ul><li>This deceleration can be placed anywhere in the class. It does not matter if it goes in the public or private section. </li></ul><ul><li>An object of each class is passed as an argument to the function function1( alpha a, beta b); and it accesses the private data member of both classes through these arguments. </li></ul>
9. 9. <ul><li>An Observing Minor point: </li></ul><ul><ul><li>Remember that class cannot be referred to until it has been declared. Notice that class beta is referred to in the declaration of the function function1( alpha, beta ); in class alpha, so beta must be declared before alpha. </li></ul></ul><ul><ul><li>Hence the declaration </li></ul></ul><ul><ul><li>class beta; </li></ul></ul><ul><ul><li>is written at the beginning of the program. </li></ul></ul>
10. 10. Friend Classes
11. 11. <ul><li>The member functions of a class can all be made friends at the same time when you make the entire class friend . </li></ul><ul><ul><li>The following program shows this. </li></ul></ul>
12. 12. Example: <ul><li>#include <iostream.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>class alpha </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>int data1; </li></ul><ul><li>public: </li></ul><ul><li>alpha( ) </li></ul><ul><li> { </li></ul><ul><li> data1 = 99; </li></ul><ul><li> } </li></ul><ul><li>~alpha( ) </li></ul><ul><li> { } </li></ul><ul><li>friend class beta; // beta is Friend class </li></ul><ul><li>}; </li></ul>
13. 13. <ul><li>class beta </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>beta( ) { } </li></ul><ul><li>~beta( ) { } </li></ul><ul><li>// all member functions can access private alpha data </li></ul><ul><li>void function1( alpha a ) </li></ul><ul><li> { </li></ul><ul><li>cout<<“ data1 = “<<a.data1; </li></ul><ul><li> } </li></ul><ul><li>void function2( alpha a ) </li></ul><ul><li> { </li></ul><ul><li>cout<<“ data1 = “<<a.data1; </li></ul><ul><li> } </li></ul><ul><li>void function3( alpha a ) </li></ul><ul><li> { </li></ul><ul><li>cout<<“ data1 = “<<a.data1; </li></ul><ul><li> } </li></ul><ul><li>}; </li></ul>
14. 14. <ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>alpha a; </li></ul><ul><li>beta b; </li></ul><ul><li>b.function1( a ); </li></ul><ul><li>b.function2( a ); </li></ul><ul><li>b.function3( a ); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output of the Program: </li></ul>
15. 15. <ul><li>In class alpha the entire class is proclaimed(declared) a friend. Now all the member functions of beta can access the private data of alpha ( in this program the single data item data1 ). </li></ul><ul><li>Note that in the friend declaration we specify that beta is a class using the class keyword. </li></ul><ul><li>friend class beta ; </li></ul><ul><li>We could also have declared beta to be a class before the alpha class specifier, as in previous example </li></ul><ul><ul><li>class beta ; </li></ul></ul><ul><ul><li>And then within alpha, referred to beta without the class keyword: </li></ul></ul><ul><ul><li>friend beta ; </li></ul></ul>
16. 16. ThanX…….