Rossella Lau Lecture 11, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template.

Slides:



Advertisements
Similar presentations
Template Classes. A class that depends on an underlying data type(s) likewise can be implemented as a template class. We can have a ‘NewClass’ of ints,
Advertisements

Chapter 7: User-Defined Functions II
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 23 : Generics King Fahd University of Petroleum & Minerals College of Computer Science.
Rossella Lau Lecture 7, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 7: OOP: Inheritance  Inheritance and composition.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Chapter 14: Overloading and Templates
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Rossella Lau Lecture 6, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 6: More on class construction UML and an.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
C++ data types. Structs vs. Classes C++ Classes.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
Rossella Lau Lecture 4, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 4: Basic containers  Array: fundamental.
Rossella Lau Lecture 2, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure.
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Chapter 15: Operator Overloading
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Intro to Generic Programming Templates and Vectors.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
1 CSC241: Object Oriented Programming Lecture No 25.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
ECE 264 Object-Oriented Software Development
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Constructors and Destructors
How to be generic Lecture 10
Templates.
Inheritance and Polymorphism
Java Programming Language
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
Lab4 problems More about templates Some STL
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
11.1 The Concept of Abstraction
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Rossella Lau Lecture 11, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template  Template and operator overload  More on operator overload -- By Rossella Lau

Rossella Lau Lecture 11, DCO10105, Semester B, A reason for template  Consider the sample class IntArray  If the objects stored on the array are not integers, but strings, rational numbers, or student records, we may need to create many classes. class IntArray{ int *array; size_t size; size_t capacity; …… }; class StringArray{ string *array; size_t size; size_t capacity; …… }; class StudentArray{ Student *array; size_t size; size_t capacity; …… };   Absolutely terrible for maintenance

Rossella Lau Lecture 11, DCO10105, Semester B, Array intArray; Templates in C++  In C++, type parameter supports a template class / function to be written as it can be of any data type  Rewrite class IntArray to a template class Array  Whenever instantiating an Array object, a type must be specified class IntArray{ int *array; size_t size; size_t capacity; …… }; template <class T> T Array strArray; Array stdArray; Array

Rossella Lau Lecture 11, DCO10105, Semester B,  Sample program sets:  IntArray.h, IntArray.cpp, testArray.cpp in lab6  Array.h and test testArray.cpp  Implementation of Array.h cannot be pre-compiled since the type cannot be determined before the class is used  implementation codes will be placed inside.h file to allow for complete user program compilation  When declaring a template class, a type should be specified whether in a user program, e.g., Array in testArray.cpp, or is implemented outside the class definition Array :: Example: IntArray  Array

Rossella Lau Lecture 11, DCO10105, Semester B, Template and operator overload  For type class used in a template, the specified type must have operator overload functions used in the template class in order to use the respective functions  Inside copy constructor, push_back(), resize() : assignment operator must be properly overloaded  Inside fillData() :  Inside printData() :  Inside searchData() :  Inside sum(), average(), min(), max() :  As an example in testArray.cpp, the instance “ names ” does not call aggregate functions since there are not respective operator overload functions defined in

Rossella Lau Lecture 11, DCO10105, Semester B, More notes on friend function  Usually, we can avoid friend function declared in a class  However, if, for example, a class overloads << (output), then it should be used inside the class: void aFunction(…) { … cout << thisClassObject; … }  The operator overload function must be declared before that statement can be used in order to get through the compiler  However, if this statement must be declared inside the class definition, friend function of << should be declared since operator<<(ostream&, TheClass const &) cannot be simply moved up before the class declaration since the second parameter is defined after it!  In this case, we must declare operator<<() as a friend function inside the class  Again, this can be avoided if the implementation codes of the class can be placed after the definition of operator<<()

Rossella Lau Lecture 11, DCO10105, Semester B, More notes on virtual operator overload  To allow for operator overload override, it can be declared with “virtual”  However, the non-member operator overload function cannot be achieved  Solution: Define a member function which calls the non-member operator function with “virtual”

Rossella Lau Lecture 11, DCO10105, Semester B, Advantages of template over inheritance  A template class can serve for different types – no new class is created – maintenance is simpler  Inheritance must create new classes  Former Java must use inheritance, creating many classes, to achieve similar results of Array  Together with operator overload, many functions can become generic – a user type further define its detailed operation in order to apply for the same process

Rossella Lau Lecture 11, DCO10105, Semester B, Summary  Template makes rewriting codes and maintenance to be minimized  Together with operator overload, it makes the potential to define generic algorithms in the STL

Rossella Lau Lecture 11, DCO10105, Semester B, Reference  Malik: END --