Overloading the << operator

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Functions Prototypes, parameter passing, return values, activation frams.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
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.
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.
Const Parameters & In Line Functions 04/15/11. Next Time  Quiz, Monday, 04/18/11  Over 5.2 and 5.3 void functions pass-by-reference  Read 7.1 about.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
C++ data types. Structs vs. Classes C++ Classes.
CMSC 2021 Stream I/O Operators and Friend Functions.
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH ; p197 – 226 Week 4 lesson 2Operators 1H ; p237 – 254 Week 5 lesson.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
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.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
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,
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Operator Overloading.
CS410 – Software Engineering Lecture #12: C++ Basics V
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Learning Objectives Pointers as dada members
Function Overloading Can enables several function Of same name
Two or more functions can have the same name but different parameters
Overloading Operator MySting Example
Operator Overloading Part 2
Class: Special Topics Copy Constructors Static members Friends this
Jim Fawcett Copyright ©
Chapter 15: Overloading and Templates
CS212: Object Oriented Analysis and Design
The dirty secrets of objects
CISC/CMPE320 - Prof. McLeod
Const in Classes CSCE 121 J. Michael Moore.
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.
Advanced Program Design with C++
Anatomy of a Function Part 3
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Copy Assignment CSCE 121 J. Michael Moore.
Introduction to Programming
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Operator Overloading I
Eighth step for Learning C++ Programming
Lecture 7.
CS 144 Advanced C++ Programming February 21 Class Meeting
Review of Function Overloading
const A& B::blah (const C& c) const {...} Passed in a reference to a constant object ‘c’  ‘c’ cannot be modified in the function const A& B::blah.
Class rational part2.
Copy Assignment CSCE 121.
Copy Constructor CSCE 121.
C++ data types.
Operator overloading Friend Function This Operator Inline Function
Jim Fawcett Copyright ©
Presentation transcript:

Overloading the << operator CSCE 121

Output Custom classes No problem we can overload Class helper function Want to use the output operator (<<) for them! No problem we can overload Class helper function

Class (Version 1) class MyClass { int data1; int data2; public: MyClass() : data1(0), data2(0) {} MyClass (int data1 = 0; int data2 = 0) : data1(data1), data2(data2) {} int getData1() { return data1; } int getData2() { return data 2; } }

Can’t be a method of the class being output! Create Function ostream& operator<<(ostream& os, const MyClass& mc) { // Do streaming output here return os; } Can’t be a method of the class being output! Always returns a reference to an ostream. First parameter is always an ostream passed by reference. (NOT const!) Second parameter is always the class being output passed by const reference. Always returns the ostream parameter that was passed in.

PREFERRED!!! Function (Version 1) ostream& operator<<(ostream& os, const MyClass& mc) { // Do streaming output here os << mc.getData1() << “ “ << mc.getData2(); return os; } Uses public accessor methods to get data to print. PREFERRED!!!

Dilemma Sometimes we have private / protected data we want to include in output from the << operator, but we don’t want public accessors for that data. We could just make the private / protected data public… But we really don’t want to do that. Solution: Declare class / function as a friend.

Friend allows access to private data members. Class (Version 2) class MyClass { int data1; // don’t want a public accessor int data2; // don’t want a public accessor friend ostream& operator<<(ostream& so, const MyClass& mc); public: MyClass() : data1(0), data2(0) {} MyClass (int data1 = 0; int data2 = 0) : data1(data1), data2(data2) {} } Friend allows access to private data members. Avoid unless you have to!!!

Avoid unless you have to!!! Function (Version 2) ostream& operator<<(ostream& os, const MyClass& mc) { // Do streaming output here os << mc.data1 << “ “ << mc.data2; return os; } Directly accesses private data members since public accessors are not available. Avoid unless you have to!!!

References http://www.cplusplus.com/doc/tutorial/inheritance/ https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx