CMSC 341 Lecture 2. Announcements Tutors wanted Office hrs Project 1.

Slides:



Advertisements
Similar presentations
1 CSC241: Object Oriented Programming Lecture No 21.
Advertisements

Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
EC-241 Object-Oriented Programming
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
1 CMSC 202 More C++ Classes. 2 Announcements Project 1 due midnight Sunday 2/25/01 Quiz #2 this week Exam 1 Wednesday/Thursday Project 2 out Monday March.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.
Templates CS-341 Dick Steflik. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply certain attributes.
1Genericity  C++ Templates. 2 Parametric Polymorphism Allows definitions to be parametrized at compile-time Such a definition is actually a “function”
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
Run-Time Type Identification Jim Fawcett CSE687 – Object Oriented Design Spring 2007.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
1 CSC241: Object Oriented Programming Lecture No 25.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Overview of C++ Templates
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 5 1.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism Reference: R.Sebesta, Chapter 12 Lafore, Chapter.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Chapter 2 Objects and Classes
Topics: Templates Exceptions
Submission Example May 14, 2018
Template Classes and Functions
Templates.
Chapter 2 Objects and Classes
CMSC 202 Templates.
Introduction to Programming
CMSC 202 Lesson 22 Templates I.
Classes.
Function Overloading.
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Templates I CMSC 202.
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
C++ Templates CSE 333 Winter 2019
Templates An introduction.
CMSC 341 C++ and OOP.
Templates CMSC 202, Version 4/02.
COP 3330 Object-oriented Programming in C++
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
Presentation transcript:

CMSC 341 Lecture 2

Announcements Tutors wanted Office hrs Project 1

Templates Provide “parametric polymorphism” Polymorphism: ability of variable/function/class to take more than one form –ad-hoc polymorphism: function overloading –true polymorphism: virtual member functions; dynamic binding –parametric polymorphism: controlled through a parameter works at compile time Two types –function templates –class templates

Why Function Templates? Suppose you want to write a function of two args of same type which returns the largest. Could write each: int maxInt (int a, int b); int maxFloat (float a, float b); … const Foo &maxFoo(const Foo &a, const Foo &b); With each code: int maxInt (int a, int b) { if (a < b) return b; else return a; }

Why Function Templates? (cont) Using templates, write one function to handle all: template T max (T a, T b){ if ( a < b) return b; else return a; } For instance: template Comparable max(Comparable a, Comparable b) { if (a < b) return b; else return a; }

Compiling Templates When compiler sees calls such as: int i1 = 1, i2 = 2, i3; float f1 = 1.1, f2 = 2.2, f3; const Foo &foo1 = initval1, &foo2 = initval2; i3 = max (i1, i2); f3 = max (f1, f2); foo1 = max (foo1, foo2); Compiler creates different functions for each type of args that are used, just like having, but all called “max” int maxInt(int a, int b) float maxFloat(float a, float b) const Foo maxFoo(const Foo &a, const Foo&b)

But what about this? Suppose you have: template Comparable max(Comparable a, Comparable b) { if (a < b) return b; else return a; } And you write: char *str1 = “abc”; char *str2 = “def”; max(str1, str2); What gets compared?

What does happen. Addresses compared, not contents. Can work around by overloading max function and provide an explicit function for char * char* max(char *a, char *b) { if (strcmp(a,b) < 0) return b; else return a; }

MemCell.h #ifndef _MEMCELL_H_ #define _MEMCELL_H_ template class MemCell { public: explicit MemCell(const Object &initVal = Object()); ~MemCell(); MemCell(const MemCell &mc); const MemCell &operator=(const MemCell &rhs); const Object &read() const;// accessor void write (const Object &x); // mutator private: Object _storedVal;}; #endif

MemCell.C #include “MemCell.H” template MemCell ::MemCell(const Object &initVal): _storedVal(initVal) {} template MemCell ::MemCell(const MemCell &mc) {write(mc.read()); } template MemCell ::~MemCell() {}

MemCell.C (cont) template const MemCell &MemCell ::operator=(const MemCell &rhs) { if (this != &rhs) write(rhs.read()); return *this; } template const Object &MemCell ::read() const { return _storedVal; } template void MemCell ::write(const Object &x) { _storedVal = x; }

TestMemCell.C #include “MemCell.H” #include // for cout #include “mystring.h” #include // for EXIT_SUCCESS int main() { MemCell m1; MemCell m2(“hello”); m1.write(37); string str = m2.read(); str += “ world”; m2.write(str); cout << m1.read() << endl; cout << m2.read() << endl; return (EXIT_SUCCESS); }

Compiling Templates (revisited) Each instantiated template object is considered to be a different type by C++ Array != Array Suppose: #include “Array.H” #include “Foo.H” int main() { Array ai; Foo ff; … }