COP 3330 Object-oriented Programming in C++

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
EC-241 Object-Oriented Programming
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
C++ is Fun – Part Eight at Turbine/Warner Bros.! Russell Hanson.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Templates ~ their instantiation and specialization.
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 =
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.
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 Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Object Oriented Programming COP3330 / CGS5409.  Class Templates  Bitwise Operators.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
MAITRAYEE MUKERJI Object Oriented Programming in C++
TK1924 Program Design & Problem Solving Session 2011/2012
Basic concepts of C++ Presented by Prof. Satyajit De
Templates.
Template Classes and Functions
Introduction to C++ Systems Programming.
Exceptions, Templates, and the Standard Template Library (STL)
Chapter 14 Templates C++ How to Program, 8/e
Chapter 2.1 Control Structures (Selection)
C++ Arrays.
Starting Out with C++ Early Objects Eighth Edition
Templates in C++.
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Object-Oriented Programming (OOP) Lecture No. 32
Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.
Value returning Functions
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Name: Rubaisha Rajpoot
Counting Loops.
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Exception Handling.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Engineering Problem Solving with C++, Etter
Exceptions, Templates, and the Standard Template Library (STL)
Parasol Lab, Texas A&M University
Templates I CMSC 202.
Fundamental Programming
Really reusable software
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Class: Special Topics Overloading (methods) Copy Constructors
Templates An introduction.
Chapter 1 c++ structure C++ Input / Output
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

COP 3330 Object-oriented Programming in C++ Template Spring 2019

Function Template A function template can perform the same algorithm for different types without specifying the types in advance Example: a function that computes the maximum for three different integers int maximum(int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; }

Function Template However, this function only works for int variables (or types that can be automatically converted to int) What if we wanted to compare variables with the type double? Do we have to write a new function? A function template allows this problem to be solved easily, with only one function

Function Template template<class T> T maximum(T value1, T value2, T value3) { T maximumValue = value1; if (value2 > maximumValue) maximumValue = value2; if (value3 > maximumvalue) maximumValue = value3; return maximumValue; }

How to Use? int main() { int int1, int2, int3; cout << “Input three integer values: “; cin >> int1 >> int2 >> int3; cout << “The maximum integer value is: “; << maximum(int1, int2, int3); double double1, double2, double3; cout << “\n\nInput three double values: “; cin >> double1 >> double2 >> double3; cout << “The maximum double value is: “; << maximum(double1, double2, double3); char char1, char2, char3; cout << “\n\nInput three characters: “; cin >> char1 >> char2 >> char3; cout << “The maximum character value is: “ << maximum(char1, char2, char3); return 0; }

Another Example This is a function template that prints the contents of the array template<class T> void printArray(const T*array, const int count) { for (int j = 0; j < count; j++) cout << array[j] << “ “; cout << endl; } This function will work on arrays of ANY class type when an appropriate insertion operator << has been overloaded

How to Use? int main() { const int aCount = 5; const int bCount = 7; const int cCount = 6; int a[aCount] = {1, 2, 3, 4, 5}; double b[bCount] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; char c[cCount] = “HELLO”; cout << “Array a contains: “ << endl; printArray(a, aCount); cout << “Array b contains: “ << endl; printArray(b, bCount); cout << “Array c contains: “ << endl; printArray(c, cCount); return 0; }

Class Template Suppose we want to design a class that uses array-bases storage to maintain a list of integers With class templates, we can make this class work with different types without altering and recompiling code To make a class into a template, prefix the class definition with the syntax template<class T> T is a type parameter, and it can have ANY name When the class is instantiated, we fill in an appropriate type Use the same prefix on the definitions of the member functions

Class Template For member functions, the name of the class will be className<T>::functionName Also, in the main program, we must #include the actual definition file, in addition to the class declaration The compiler creates a different version of the class for each type that is used Thus, either the entire class should be written in the header file, OR the .cpp file should be included #include “templatelist.cpp”

Code Review

Questions