Object-Oriented Programming (OOP) Lecture No. 38

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Final and Abstract Classes
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
OBJECT ORIENTED FEATURES AND IMPLEMENTATION OF UNIVERSAL QUANTIFICATION IN C++ Karoly Bosa RISC SS2000.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
EC-241 Object-Oriented Programming
C/c++ 4 Yeting Ge.
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Abstract classes and Interfaces. Abstract classes.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Computing IV Singleton Pattern Xinwen Fu.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
CS212: Object Oriented Analysis and Design
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department.
 2006 Pearson Education, Inc. All rights reserved Templates.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Motivation for Generic Programming in C++
Java Unit 11: Inheritance I
Objects as a programming concept
Today’s Objectives 5-Jul-2006 Announcements
How to be generic Lecture 10
Ch 10- Advanced Object-Oriented Programming Features
Object-Oriented Programming (OOP) Lecture No. 45
Final and Abstract Classes
Generic programming – Function template
Creating Your OwnClasses
CS212: Object Oriented Analysis and Design
Introduction to Object-oriented Program Design
Templates.
Object-Oriented Programming (OOP) Lecture No. 32
Object-Oriented Programming (OOP) Lecture No. 36
CS212: Object Oriented Analysis and Design
Object-Oriented Programming (OOP) Lecture No. 39
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Templates Class Templates
Object-Oriented Programming Paradigm
Object-Oriented Programming (OOP) Lecture No. 40
Introduction to Programming
Reference Parameters.
Multidimensional Arrays
Object-Oriented Programming (OOP) Lecture No. 37
Template Functions Lecture 9 Fri, Feb 9, 2007.
Object-Oriented Programming (OOP) Lecture No. 33
Object oriented programming (OOP) Lecture No. 7
Java Programming Language
Lab4 problems More about templates Some STL
Final and Abstract Classes
Templates Generic Programming.
Object-Oriented Programming (OOP) Lecture No. 34
Object-Oriented Programming (OOP) Lecture No. 23
Object-Oriented Programming (OOP) Lecture No. 27
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Parse Analysis Introduction
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 38

Templates and Friends Like inheritance, templates or their specializations are compatible with friendship feature of C++

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

…Templates and Friends – Rule 1 void doSomething( B< char >& ); class A { … }; template< class T > class B { int data; friend void doSomething( B<char>& ); friend A; … };

…Templates and Friends – Rule 1 void doSomething( B< char >& cb ) { B< int > ib; ib.data = 5; // OK cb.data = 6; // OK }

…Templates and Friends – Rule 1 class A { void method() { B< int > ib; B< char > cb ib.data = 5; // OK cb.data = 6; // OK } };

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

…Templates and Friends – Rule 2 template< class U > void doSomething( U ); template< class V > class A { … }; template< class T > class B { int data; friend void doSomething( T ); friend A< T >; };

…Templates and Friends – Rule 2 template< class U > void doSomething( U u ) { B< U > ib; ib.data = 78; }

…Templates and Friends – Rule 2 int main() { int i = 5; char c = ‘x’; doSomething( i ); // OK doSomething( c ); // OK return 0; }

…Templates and Friends – Rule 2 template< class U > void doSomething( U u ) { B< int > ib; ib.data = 78; }

…Templates and Friends – Rule 2 int main() { int i = 5; char c = ‘x’; doSomething( i ); // OK doSomething( c ); // Error! return 0; }

…Templates and Friends – Rule 2 Because doSomething() always instantiates B< int > class B< int > { int data; friend void doSomething( int ); friend A< int >; };

…Templates and Friends – Rule 2 template< class T > class A { void method() { // Error! B< char > cb; cb.data = 8; B< int > ib; ib.data = 9; } };

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

…Templates and Friends – Rule 3 template< class U > void doSomething( U ); template< class V > class A { … }; template< class T > class B { int data; template< class W > friend void doSomething( W ); template< class S > friend class A; }; VC++ and BC does not support friendship of general templates However standard includes it

…Templates and Friends – Rule 3 template< class U > void doSomething( U u ) { B< int > ib; ib.data = 78; }

…Templates and Friends – Rule 3 int main() { int i = 5; char c = ‘x’; doSomething( i ); // OK doSomething( c ); // OK return 0; }

…Templates and Friends – Rule 3 template< class T > class A { void method() { // OK! B< char > cb; cb.data = 8; B< int > ib; ib.data = 9; } };

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

…Templates and Friends – Rule 4 template< class T > class B { T data; template< class U > friend class A; };

…Templates and Friends – Rule 4 template< class U > class A { A() { B< int > ib; ib.data = 10; // OK } };

…Templates and Friends – Rule 4 template< class U > class A< U* > { A() { B< int > ib; ib.data = 10; // OK } };

…Templates and Friends – Rule 4 template< class T > class B { T data; template< class U > friend void doSomething( U ); };

…Templates and Friends – Rule 4 template< class U > void doSomething( U u ) { B< int > ib; ib.data = 56; // OK }

…Templates and Friends – Rule 4 void doSomething< char >( char u ) { B< int > ib; ib.data = 56; // OK }