Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Class and Objects.
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.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I
Chapter 13: Overloading.
Chapter 15: Operator Overloading
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.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Review of C++ Programming Part II Sheng-Fang Huang.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CONSTRUCTOR AND DESTRUCTORS
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Chapter 3: User-Defined Functions I
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Operator Overloading.
TK1924 Program Design & Problem Solving Session 2011/2012
Chapter 13: Overloading and Templates
Chapter 7: User-Defined Functions II
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Object-Oriented Design (OOD) and C++
Chapter 15: Overloading and Templates
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 7: User-Defined Functions II
Chapter 11: Inheritance and Composition
More C++ Classes Systems Programming.
Presentation transcript:

Chapter 13: Overloading and Templates

Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator overloading – Examine the pointer this – Learn about friend functions – Learn how to overload operators as members and nonmembers of a class 2C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Objectives (cont’d.) In this chapter, you will (cont’d.) – Discover how to overload various operators – Become familiar with the requirements for classes with pointer member variables – Learn about templates – Explore how to construct function templates and class templates 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Introduction Templates: enable you to write generic code for related functions and classes Function templates: used to simplify function overloading 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Why Operator Overloading Is Needed Consider the following statements: Which of the following would you prefer? 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Why Operator Overloading Is Needed (cont’d.) Assignment and member selection are the only built-in operations on classes Other operators cannot be applied directly to class objects Operator overloading: extends definition of an operator to work with a user-defined data type C++ allows you to extend the definitions of most of the operators to work with classes 6C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Operator Overloading Most existing C++ operators can be overloaded to manipulate class objects Cannot create new operators Operator function: overloads an operator – Use reserved word operator as the function name 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Syntax for Operator Functions Syntax of an operator function heading: – It is a value-returning function – operator is a reserved word To overload an operator for a class: – Include operator function declaration in the class definition – Write the definition of the operator function 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading an Operator: Some Restrictions Cannot change precedence or associativity Default parameters cannot be used Cannot change number of parameters Cannot create new operators Cannot overload:..* :: ?: sizeof How the operator works with built-in types remains the same – Can overload for user-defined objects or for a combination of user-defined and built-in objects 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Pointer this Every object of a class maintains a (hidden) pointer to itself called this When an object invokes a member function – this is referenced by the member function 10C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Friend Functions of Classes Friend function (of a class): a nonmember function of the class that has access to all the members of the class Use the reserved word friend in the function prototype in the class definition 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Definition of a friend Function " friend " doesn’t appear in function definition When writing the friend function definition – The name of the class and the scope resolution operator are not used 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Operator Functions as Member and Nonmember Functions To overload (), [], ->, or = for a class, the function must be a member of the class If op is overloaded for opOverClass : – If the leftmost operand of op is an object of a different type, the overloading function must be a nonmember (friend) of the class – If the overloading function for op is a member of opOverClass, then when applying op on objects of type opOverClass, the leftmost operand must be of type opOverClass 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading Binary Operators If # represents a binary operator (e.g., + or == ) that is to be overloaded for rectangleType – It can be overloaded as either a member function of the class or as a friend function 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Binary Operators as Member Functions Function prototype (included in the class definition): Function definition: 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Arithmetic or Relational Operators as Nonmember Functions Function prototype (included in class definition): Function definition: 16C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Stream Insertion ( > ) Operators Consider the expression: cout << myRectangle; – Leftmost operand is an ostream object, not a rectangleType object Thus, the operator function that overloads << for rectangleType must be a nonmember function of the class – Same applies to the function that overloads >> 17C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Stream Insertion Operator ( << ) Function prototype: Function definition: 18C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Stream Extraction Operator ( >> ) Function prototype: Function definition: 19C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Assignment Operator ( = ) Function prototype: Function definition: 20C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading Unary Operators To overload a unary operator for a class: – If the operator function is a member of the class, it has no parameters – If the operator function is a nonmember (i.e., a friend function), it has one parameter 21C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Increment ( ++) and Decrement ( --) Operators General syntax to overload the pre-increment operator ++ as a member function – Function prototype: – Function definition: 22C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Increment ( ++ ) and Decrement ( -- ) Operators (cont’d.) General syntax to overload the pre-increment operator ++ as a nonmember function: – Function prototype: – Function definition: 23C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Increment ( ++ ) and Decrement ( -- ) Operators (cont’d.) General syntax to overload the post-increment operator ++ as a member function: – Function prototype: – Function definition: 24C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Increment ( ++ ) and Decrement ( -- ) Operators (cont’d.) Syntax to overload the post-increment operator ++ as a nonmember function: – Function prototype: – Function definition: 25C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Operator Overloading: Member Versus Nonmember Some operators must be overloaded as member functions and some must be overloaded as nonmember (friend) functions Binary arithmetic operator + can be overloaded either way – As a member function, operator + has direct access to data members of one of the objects – Need to pass only one object as a parameter 26C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Operator Overloading: Member Versus Nonmember (cont’d.) Overload + as a nonmember function – Must pass both objects as parameters – Code may be somewhat clearer this way 27C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Classes and Pointer Member Variables (Revisited) Recall that assignment operator copies member variables from one object to another of the same type – Does not work well with pointer member variables Classes with pointer member variables must: – Explicitly overload the assignment operator – Include the copy constructor – Include the destructor 28C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Operator Overloading: One Final Word If an operator op is overloaded for a class, e.g., rectangleType – When you use op on objects of type rectangleType, the body of the function that overloads the operator op for the class rectangleType executes – Therefore, whatever code you put in the body of the function executes 29C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Overloading the Array Index (Subscript) Operator ( [] ) Syntax to declare operator[] as a member of a class for nonconstant arrays: Syntax to declare operator[] as a member of a class for constant arrays: 30C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Function Overloading Overloading a function: several functions with the same name, but different parameters – Parameter list determines which function will execute – Must provide the definition of each function 31C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Templates Template: a single code body for a set of related functions (function template) and related classes (class template) Syntax: – Type is the data type – Declaration is either a function declaration or a class declaration 32C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Templates (cont’d.) class in the heading refers to any user- defined type or built-in type Type : a formal parameter to the template Just as variables are parameters to functions, data types are parameters to templates 33C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Function Templates Syntax of the function template: Type is a formal parameter of the template used to: – Specify type of parameters to the function – Specify return type of the function – Declare variables within the function 34C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Class Templates Class template: a single code segment for a set of related classes – Called parameterized types Syntax: A template instantiation can be created with either a built-in or user-defined type The function members of a class template are considered to be function templates 35C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Header File and Implementation File of a Class Template Passing a parameter to a function takes effect at run time Passing a parameter to a class template takes effect at compile time Cannot compile the implementation file independently of the client code – Can put class definition and definitions of the function templates directly in the client code – Can put class definition and the definitions of the function templates in the same header file 36C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Header File and Implementation File of a Class Template (cont’d.) Another alternative: put class definition and function definitions in separate files – Include directive to implementation file at the end of header file In either case, function definitions and client code are compiled together 37C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Summary An operator that has different meanings with different data types is said to be overloaded Operator function: a function that overloads an operator − operator is a reserved word − Operator functions are value-returning Operator overloading provides the same concise notation for user-defined data types as for built-in data types 38C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Summary (cont’d.) Only existing operators can be overloaded The pointer this refers to the object A friend function is a nonmember of a class If an operator function is a member of a class – The leftmost operand of the operator must be a class object (or a reference to a class object) of that operator’s class 39C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Summary (cont’d.) Classes with pointer variables must overload the assignment operator, and include both a copy constructor and deconstructor Templates: – Function template: a single code segment for a set of related functions – Class template: a single code segment for a set of related classes Are called parameterized types 40C++ Programming: From Problem Analysis to Program Design, Seventh Edition