Computer Notes - Templates and Friends - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Notes - Templates and Friends

Description:

- Computer Notes - Templates and Friends in Object oriented Programming what is Templates and Friends Explain about it in detail .explain it with example – PowerPoint PPT presentation

Number of Views:188

less

Transcript and Presenter's Notes

Title: Computer Notes - Templates and Friends


1
Templates and Friends
http//ecomputernotes.com
2
Templates and Friends
  • Like inheritance, templates or their
    specializations are compatible with friendship
    feature of C

http//ecomputernotes.com
3
Templates and Friends Rule 1
  • When an ordinary function or class is declared as
    friend of a class template then it becomes friend
    of each instantiation of that template

http//ecomputernotes.com
4
Templates and Friends Rule 1
  • void doSomething( Blt char gt )
  • class A
  • templatelt class T gt class B
  • int data
  • friend void doSomething( Bltchargt )
  • friend A

http//ecomputernotes.com
5
Templates and Friends Rule 1
  • void doSomething( Blt char gt cb )
  • Blt int gt ib
  • ib.data 5 // OK
  • cb.data 6 // OK

http//ecomputernotes.com
6
Templates and Friends Rule 1
  • class A
  • void method()
  • Blt int gt ib
  • Blt char gt cb
  • ib.data 5 // OK
  • cb.data 6 // OK

http//ecomputernotes.com
7
Templates and Friends Rule 2
  • When a friend function / class template is
    instantiated with the type parameters of class
    template granting friendship then its
    instantiation for a specific type is a friend of
    that class template instantiation for that
    particular type

http//ecomputernotes.com
8
Templates and Friends Rule 2
  • templatelt class U gt
  • void doSomething( U )
  • templatelt class V gt
  • class A
  • templatelt class T gt class B
  • int data
  • friend void doSomething( T )
  • friend Alt T gt

http//ecomputernotes.com
9
Templates and Friends Rule 2
  • templatelt class U gt
  • void doSomething( U u )
  • Blt U gt ib
  • ib.data 78

http//ecomputernotes.com
10
Templates and Friends Rule 2
  • int main()
  • int i 5
  • char c x
  • doSomething( i ) // OK
  • doSomething( c ) // OK
  • return 0

http//ecomputernotes.com
11
Templates and Friends Rule 2
  • templatelt class U gt
  • void doSomething( U u )
  • Blt int gt ib
  • ib.data 78

http//ecomputernotes.com
12
Templates and Friends Rule 2
  • int main()
  • int i 5
  • char c x
  • doSomething( i ) // OK
  • doSomething( c ) // Error!
  • return 0

http//ecomputernotes.com
13
Templates and Friends Rule 2
  • Because doSomething() always instantiates Blt int
    gt
  • class Blt int gt
  • int data
  • friend void doSomething( int )
  • friend Alt int gt

http//ecomputernotes.com
14
Templates and Friends Rule 2
  • templatelt class T gt
  • class A
  • void method() // Error!
  • Blt char gt cb
  • cb.data 8
  • Blt int gt ib
  • ib.data 9

http//ecomputernotes.com
15
Templates and Friends Rule 3
  • When a friend function / class template takes
    different type parameters from the class template
    granting friendship then its each instantiation
    is a friend of each instantiation of the class
    template granting friendship

http//ecomputernotes.com
16
Templates and Friends Rule 3
  • templatelt class U gt
  • void doSomething( U )
  • templatelt class V gt
  • class A
  • templatelt class T gt class B
  • int data
  • templatelt class W gt
  • friend void doSomething( W )
  • templatelt class S gt
  • friend class A

http//ecomputernotes.com
17
Templates and Friends Rule 3
  • templatelt class U gt
  • void doSomething( U u )
  • Blt int gt ib
  • ib.data 78

http//ecomputernotes.com
18
Templates and Friends Rule 3
  • int main()
  • int i 5
  • char c x
  • doSomething( i ) // OK
  • doSomething( c ) // OK
  • return 0

http//ecomputernotes.com
19
Templates and Friends Rule 3
  • templatelt class T gt
  • class A
  • void method() // OK!
  • Blt char gt cb
  • cb.data 8
  • Blt int gt ib
  • ib.data 9

http//ecomputernotes.com
20
Templates and Friends Rule 4
  • Declaring a template as friend implies that all
    kinds of its specializations explicit, implicit
    and partial, are also friends of the class
    granting friendship

http//ecomputernotes.com
21
Templates and Friends Rule 4
  • templatelt class T gt
  • class B
  • T data
  • templatelt class U gt
  • friend class A

http//ecomputernotes.com
22
Templates and Friends Rule 4
  • templatelt class U gt
  • class A
  • A()
  • Blt int gt ib
  • ib.data 10 // OK

23
Templates and Friends Rule 4
  • templatelt class U gt
  • class Alt U gt
  • A()
  • Blt int gt ib
  • ib.data 10 // OK

http//ecomputernotes.com
24
Templates and Friends Rule 4
  • templatelt class T gt
  • class B
  • T data
  • templatelt class U gt
  • friend void doSomething( U )

25
Templates and Friends Rule 4
  • templatelt class U gt
  • void doSomething( U u )
  • Blt int gt ib
  • ib.data 56 // OK

http//ecomputernotes.com
26
Templates and Friends Rule 4
  • templatelt gt
  • void doSomethinglt char gt( char u )
  • Blt int gt ib
  • ib.data 56 // OK

http//ecomputernotes.com
Write a Comment
User Comments (0)
About PowerShow.com