C++ Operator Overloading Gordon College CPS212 Gordon College CPS212.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

Operator overloading redefine the operations of operators
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
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.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
Chapter 11: Classes and Data Abstraction
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
Plab – Exercise 5 C++: references, operator overloading, friends.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
IT PUTS THE ++ IN C++ Object Oriented Programming.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Chapter 12: Adding Functionality to Your Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Defining New Types Lecture 21 Hartmut Kaiser
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Function Overloading and References
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Procedural and Object-Oriented Programming
Programming with ANSI C ++
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
A First C++ Class – a Circle
Review: Two Programming Paradigms
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 15: Overloading and Templates
Introduction to Classes
Classes and Objects.
Handout-4b More on Classes
Lecture 8 Object Oriented Programming (OOP)
SPL – PS3 C++ Classes.
Presentation transcript:

C++ Operator Overloading Gordon College CPS212 Gordon College CPS212

Function overloading Function overloading enables writing more then one function with the same name but different signature. int my_func(int a, int b) { return(a * b); } int my_func(int a, int b, int c) { return( a * b * c); } my_func(2,4); my_func(2,4,6);

Function overloading int Add(int nX, int nY) { return nX + nY; } double Add(double dX, double dY) { return dX + dY; } int Add(int nX, int nY, int nZ) { return nX + nY + nZ; } ERROR (signature must be unique in some way) int GetRandomValue(); double GetRandomValue();

Operator overloading  Some languages allow only function (method) overloading - such as Java.  However, on a primitive level all languages use operator overloading to some degree.  Consider “ Cows “ + “ can jump ”  Some languages allow only function (method) overloading - such as Java.  However, on a primitive level all languages use operator overloading to some degree.  Consider “ Cows “ + “ can jump ”

Operator overloading  Operator overloading doesn ’ t add power to code - it essentially enhances the coding experience (efficiency for the programmer)  Adds logical depth to ADTs:  If there is a new type called “ BirthdayList ”, shouldn ’ t it possible to add, substract, compare lists and display using intuitive operators? listA == listB listA + Jim listA - Marvin cout << listA  Operator overloading doesn ’ t add power to code - it essentially enhances the coding experience (efficiency for the programmer)  Adds logical depth to ADTs:  If there is a new type called “ BirthdayList ”, shouldn ’ t it possible to add, substract, compare lists and display using intuitive operators? listA == listB listA + Jim listA - Marvin cout << listA

Operator overloading  The only operators that cannot be overloaded are :: (scope resolution),. (member selection), and.* (member selection through pointer to function).

Operator overloading  FRIEND (free – yet is associated with class – inline or external function) friend const Point operator+ (const Point&, const int&); friend ostream & operator<<( ostream&, const Point &);  CLASS const Point& operator= (const Point&);  FREE const Point operator- (const Point&, const Point&);  FRIEND (free – yet is associated with class – inline or external function) friend const Point operator+ (const Point&, const int&); friend ostream & operator<<( ostream&, const Point &);  CLASS const Point& operator= (const Point&);  FREE const Point operator- (const Point&, const Point&); Within Class Definition

Operator overloading  FRIEND (free that is allowed access to private variables of class – Friend not used here) const Point operator+ (const Point& lhs, const int& v) { return Point(lhs._x + v, lhs._y + v); } ostream& operator<<( ostream& o, const Point &p ) { o << "(" << p.x() << "," << p.y() << ")"; return o; }  FRIEND (free that is allowed access to private variables of class – Friend not used here) const Point operator+ (const Point& lhs, const int& v) { return Point(lhs._x + v, lhs._y + v); } ostream& operator<<( ostream& o, const Point &p ) { o << "(" << p.x() << "," << p.y() << ")"; return o; } Within Source File (not scoped for class)

Operator overloading  CLASS (use when necessary or more convenient) const Point& Point::operator= (const Point& rhs) { this->_x = rhs.x(); this->_y = rhs.y(); return *this; }  CLASS (use when necessary or more convenient) const Point& Point::operator= (const Point& rhs) { this->_x = rhs.x(); this->_y = rhs.y(); return *this; } Within Source File (scoped for class)

Operator overloading  FREE (efficiency issues – must use accessor and mutator functions) const Point operator- (const Point& lhs, const Point& rhs) { return Point(lhs.x() - rhs.x(), lhs.y() - rhs.y()); }  FREE (efficiency issues – must use accessor and mutator functions) const Point operator- (const Point& lhs, const Point& rhs) { return Point(lhs.x() - rhs.x(), lhs.y() - rhs.y()); } Within Source File (not scoped for class)

Const  The five types of const prevent the following from modification:  const variable: the variable (local and global variables)  const argument: the argument  const return type: this only only applied to references to members of a class. Then, const can prevent the original member from being modified.  const method: all non-mutable class members  const member: the member (class member - once object is constructed - this value can’t change)  The five types of const prevent the following from modification:  const variable: the variable (local and global variables)  const argument: the argument  const return type: this only only applied to references to members of a class. Then, const can prevent the original member from being modified.  const method: all non-mutable class members  const member: the member (class member - once object is constructed - this value can’t change)

Const  For parameters to member function: the parameter value can not be changed.  Particularly useful when using the reference qualifier: void addCow(const Cow& heifer) {list_ = list_+heifer;} Otherwise - the function would have the ability to change the value of the argument being passed in.  For parameters to member function: the parameter value can not be changed.  Particularly useful when using the reference qualifier: void addCow(const Cow& heifer) {list_ = list_+heifer;} Otherwise - the function would have the ability to change the value of the argument being passed in.

Const  For class usage of a member function - the function can not change the values of any class variables void displayBrands() const;  For class usage of a member function - the function can not change the values of any class variables void displayBrands() const;

Const  The value of a return type that is declared const cannot be changed. This is especially useful when giving a reference to a class's internals. struct Values { const std::vector & GetValues() const { return mV; } private: std::vector mV; }; C++ Structure and C++ class are exactly same except default access specifier of their members - in C++ Structure all members are public by default while in Class all are private. NOTE: Use const whenever possible.  The value of a return type that is declared const cannot be changed. This is especially useful when giving a reference to a class's internals. struct Values { const std::vector & GetValues() const { return mV; } private: std::vector mV; }; C++ Structure and C++ class are exactly same except default access specifier of their members - in C++ Structure all members are public by default while in Class all are private. NOTE: Use const whenever possible.