• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
656
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
33
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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