1 More Operator Overloading Chapter 11. 2 Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
1 Abstract Data Types Chapter 1. 2 Objectives You will be able to: 1. Say what an abstract data type is. 2. Implement a simple abstract data type in C++
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Shape.h Shape Point Circle Cylinder // SHAPE.H
Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
Friend. Revisit the Class Money class Money { public: Money(int D, int C); int getDollars(); int getCents(); private: int dollars; int cents; }; Overload.
Computer Science 1620 Function Scope & Global Variables.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Binary Search Trees II Morse Code.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
1 3. More About Data Structures and ADTs: C++ Data types (Read §3.1 & §3.2) C++ types an be classified as: Fundamental (or simple or scalar): A data object.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: User-Defined Classes Problem Solving, Abstraction, and Design.
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.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Original Source : and Problem and Problem Solving.ppt.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
#include guards Practical session #2 Software Engineering
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 Introduction to Object Oriented Programming Chapter 10.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
1 Compiler directive: #define, usage 1 #include using namespace std; #define TAX //double TAX=0.08; #define LAST_NAME "Li" #define FIRST_NAME "Dennis"
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Overloading C++ supports the concept of overloading Two main types
#define #include<iostream> using namespace std; #define GO
Object-Oriented Design (OOD) and C++
A First C++ Class – a Circle
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Overloading the << operator
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Screen output // Definition and use of variables
Overview of C++ Overloading
Classes.
C++ Compilation Model C++ is a compiled language
CS 144 Advanced C++ Programming February 21 Class Meeting
Overloading the << operator
Presentation transcript:

1 More Operator Overloading Chapter 11

2 Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes. Understand and use friend functions.

3 Overloading the Insertion Operator << works with all native C++ types. Overloaded definitions for all native types are included in What about our own classes? Would like to be able to write cout << my_circle << endl; If want it to work as expected, we have to provide a new overload of the << operator for that class: void operator<<(ostream& os, const Circle& c); Cannot be a member function. Why?

Friend Methods A class can declare a non-member function as a friend. Function has the same access to class members as a member method. The function is normally defined in the same cpp file as the member functions. Effectively part of the interface published by the class. Read about this in Chapter 11. 4

Circle.h #ifndef CIRCLE_H #define CIRCLE_H #include using namespace std; class Circle { … public: Circle(const char* Name, double Radius); … bool operator>(const Circle& other) const; friend void operator<<(ostream& os, const Circle& c); }; #endif 5

Add to Circle.cpp void operator<<(ostream& os, const Circle& c) { os << c.name << " Radius " << c.radius; } Note: NOT a member of class Circle. No Circle:: 6

Main Program void Display_Circles(Circle** circles, int nr_circles) { cout << endl; for (int i = 0; i < nr_circles; ++i) { //circles[i]->Display(); cout << *circles[i]; cout << endl; } cout << endl; } 7

Program Running 8

But there is a flaw Suppose we want to put more output after c1 void Display_Circles(Circle** circles, int nr_circles) { cout << endl; for (int i = 0; i < nr_circles; ++i) { cout << *circles[i] << endl; } cout << endl; } This doesn’t work. Why? 9

Compile Time Errors 10

The error line 11

12 Correction in Circle.h friend ostream& operator<<(ostream& os, const Circle& c);

13 Correction in Circle.cpp ostream& operator<<(ostream& os, const Circle& c) { os << c.name << " Radius " << c.radius; return os; }

Now It Works 14

An Alternative It didn't have to be a friend. If the << operator used accessor functions it would not need to be a friend. Move declaration outside class definition and remove “friend”. 15

Circle.h class Circle {... double Radius() const {return radius;}; const char* Name() const {return name;};... }; ostream& operator<<(ostream& os, const Circle& c); 16

Circle.cpp Now uses accessor methods rather than directly accessing member variables of class Circle. ostream& operator<<(ostream& os, const Circle& c) { os << c.Name() << " Radius " << c.Radius(); return os; } 17

Works the Same 18

Summary Overloaded insertion operator, operator<<, should be defined with the class, but cannot be a member. Could be a friend. Could use accessor methods. 19