Computer Engineering Rabie A. Ramadan Lecture 5.

Slides:



Advertisements
Similar presentations
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Advertisements

Macro Processor.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Lecture 2 Introduction to C Programming
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
 2006 Pearson Education, Inc. All rights reserved Templates.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Templates CS-240 Dick Steflik & DJ. Foreman. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply.
Chapter 15: Operator Overloading
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Lecture #5 Introduction to C++
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
C Hints and Tips The preprocessor and other fun toys.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Overview of Previous Lesson(s) Over View .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows.  It.
Engineering Computing I Chapter 4 Functions and Program Structure.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
1 CSC241: Object Oriented Programming Lecture No 25.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Programming Languages Meeting 3 September 9/10, 2014.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
 2006 Pearson Education, Inc. All rights reserved Templates.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Lecture 9 Symbol Table and Attributed Grammars
TK1924 Program Design & Problem Solving Session 2011/2012
User-Written Functions
Java Primer 1: Types, Classes and Operators
FUNCTIONS In C++.
Programming Fundamentals Lecture #7 Functions
Chapter 10: Pointers 1.
Chapter 15 Pointers, Dynamic Data, and Reference Types
C Preprocessor(CPP).
Classes, Objects and Methods
What Is? function predefined, programmer-defined
ENERGY 211 / CME 211 Lecture 23 November 12, 2008.
Presentation transcript:

Computer Engineering Rabie A. Ramadan Lecture 5

Templates 2

3 A feature of the C++ programming language that allow functions and classes to operate with generic types Allows a function or class to work on many different data types without being rewritten for each one. A mold from which the compiler generates a family of classes or functions.

Function Templates 4 Behaves like a function that can accept arguments of many different types. A function template represents a family of functions.

Function Templates declaration 5 template Indicates that T is a template parameter Equivalent to template Variables declared with ‘const’ added become constants and cannot be altered by the program. Helps in error messages Which one is constant, the pointer or the variable ? const int * Constant2; int const * Constant2; int * const Constant3; int const * const Constant4;

const 6 const int * Constant2 ; Declares that Constant2 is variable pointer to a constant integer int const * Constant2; An alternative syntax which does the same, int * const Constant3 Declares that Constant3 is constant pointer to a variable integer int const * const Constant4 Declares that Constant4 is constant pointer to a constant integer.

Class Template 7

Members of Class Templates 8 Members of templated classes are defined differently than those of nontemplated classes.

Templates for Constructors and Destructors 9

Class Template Instantiation 10 B

Templates vs. Macros 11 #define SquareOf(x) x*x It defines a kind of function which, used in an actual piece of code, Looks exactly like any other function call: Double yout, xin=3; yout = SquareOf(xin); The formal syntax of a macro is: #define name(dummy1[,dummy2][,...]) tokenstring

How does a compiler handle a macro? 12 It gets handled and done with at compilation time rather than at run time. When the compiler encounters a previously defined macro, it first isolates its actual arguments, handling them as plain text strings separated by commas. It then parses the tokenstring, isolates all occurrences of each dummy-argument symbol and replaces it by the actual argument string. The whole process consists entirely of mechanical string substitutions with almost no semantic testing!

Why should that be a problem? 13 The following code compiles without any problem: you probably expect the output of this program to be:

Why should that be a problem? 14 What you actually get, however, is this: What happened? When the compiler met the string "SquareOf(xin+4)", it replaced it with the string "x*x" and then replaced each of the dummy- argument-strings "x" by the actual-argument-string "xin+4", obtaining the final string "xin+4*xin+4" which, in fact, evaluates to 19 and not to the expected 49. ?

Another vision 15 compilers ignore macros until they are invoked. If macro A(...) contains a call to macro B(...) there is no reason for the definitions of the two macros to appear in any particular order. If the definitions are in a header file, that of macro A may precede the one of macro B. It is only important that both macros be defined when macro A is actually called. On the other hand, when a macro is never invoked, its definition is completely irrelevant. Your header file may therefore contain nonsensical definitions of a number of unused macros and you will not find out until, at some later revision, you actually invoke one of them! Other problems could be found here: