Introduction to Programming

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
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.
Introduction to Programming Lecture 31. Operator Overloading.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 Lecture 8: Introduction to C++ Templates and Exceptions  C++ Function Templates  C++ Class Templates.
1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Learners Support Publications Functions in C++
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Instructor - C. BoyleFall Semester
Introduction to Programming Lecture 40. Class Class is a user defined data type.
CS 11 C++ track: lecture 6 Today: Default function arguments friend classes Introduction to exception handling.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
Strings Jarret Raim C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
C:\Temp\Templates 4 5 Use This Main Program 6.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
CMSC 341 Lecture 2. Announcements Tutors wanted Office hrs Project 1.
 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
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.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
C++ : Operator overloading example
Introduction to Programming
Topics: Templates Exceptions
Introduction to Programming
Introduction to Programming
Function Basics.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Extra.
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
CMSC 202 Lesson 22 Templates I.
Introduction to Programming
Default Arguments.
Pointers Lecture 2 Tue, Jan 24, 2006.
Introduction to Programming
Function Overloading.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Continued from last class
CS150 Introduction to Computer Science 1
Templates I CMSC 202.
CS150 Introduction to Computer Science 1
Template.
Pointers & Functions.
Object-Oriented Programming (OOP) Lecture No. 34
Introduction to Programming
Functions Divide and Conquer
Introduction to Programming
Presentation transcript:

Introduction to Programming Lecture 41

Templates

Types of Templates Function Templates Class Templates

Swap Function { int temp ; temp = i ; i = j ; j = temp ; } void swap ( int & i , int & j ) { int temp ; temp = i ; i = j ; j = temp ; }

Function Overloading

Function Templates

template < class T >

return_type function_name ( argument_list )

Example int reverse ( int x ) { return ( - x ) ; } double reverse ( double x )

Example template < class T > T reverse ( T x ) { return (- x ) ; }

Example main ( ) { int i ; ……. reverse ( i ) ; }

Example main ( ) { int i ; … reverse ( i ) ; double y ; reverse ( y ) ; }

Code Reuse

Example template < class T > void swap ( T & x , T & y ) { T temp ; temp = x ; x = y ; y = temp ; }

int a , b ; char a , b ; swap ( a , b ) ;

Example template < class T > void swap ( T & x , T & y ) { T temp ; temp = x ; x = y ; y = temp ; }

template < class T , class U >

Example template <class T> T larger ( T x, T y ) { T big ; if ( x > y ) big = x ; else big = y ; return ( big ) ; }

Example main ( ) { int i = 5 , j = 7 ; double x = 10.0 , y = 15.0 ; cout << larger ( i , j ) << endl ; cout << larger ( x , y ) << endl ; // cout << larger ( i , y ) ; Error }

Example template <class T> void inverse ( T & x , T & y ) { T temp ; temp = x ; x = y ; y = temp ; }

Example template <class T> T inverse ( T x ) { return ( - x ) ; }

Example main ( ) { int i = 4 , j = 8 ; inverse ( i , j ) ; }

Example template <class T> T reverse ( T x ) { return ( - x ) ; } void main ( ) double a = 10.75 ; reverse ( a ) ; reverse <int> ( a ) ;

Example template <class T , class U> T reverse ( U x ) { return ( - x ) ; }

Example main ( ) { double a = 8.8 ; reverse ( a ) ; reverse <int> ( a ) ; reverse <int , double> ( a ) ; reverse<double , double> ( a ) ; reverse<double , int> ( a ) ; }

Example class PhoneCall { private : int lengthOfCall ; char billCode ; public : PhoneCall ( const int i , char b ) ; PhoneCall ( PoneCall & p ) ; PhoneCall PhoneCall :: operator - ( void ) ; void display ( void ) ; } ;

Example template <class T> T reverse ( T x ) { return ( - x ) ; }

Example PhoneCall reverse ( PhoneCall x ) { return (- x ) ; }

Example PhoneCall PhoneCall :: operator - ( void ) { PhoneCall temp ( * this ) ; temp.billCode = 'C' ; return ( temp ) ; }

Example main ( ) { PhoneCall a ( 10 , ‘S’ ) ; a.display ( ) ; a = reverse ( a ) ; }