Top Banner
1 More Operator Overloading Chapter 11
19

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.

Jan 18, 2016

Download

Documents

Kenneth Hart
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

1

More Operator Overloading

Chapter 11

Page 2: 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.

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.

Page 3: 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.

3

Overloading the Insertion Operator

<< works with all native C++ types.

Overloaded definitions for all native types are included in <iostream>

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?

Page 4: 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.

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

Page 5: 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.

Circle.h

#ifndef CIRCLE_H

#define CIRCLE_H

#include <iostream>

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

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

Program Running

8

Page 9: 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.

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

Page 10: 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.

Compile Time Errors

10

Page 11: 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.

The error line

11

Page 12: 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.

12

Correction in Circle.h

friend ostream& operator<<(ostream& os, const Circle& c);

Page 13: 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.

13

Correction in Circle.cpp

ostream& operator<<(ostream& os, const Circle& c)

{

os << c.name << " Radius " << c.radius;

return os;

}

Page 14: 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.

Now It Works

14

Page 15: 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.

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

Page 16: 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.

Circle.h

class Circle

{

...

double Radius() const {return radius;};

const char* Name() const {return name;};

...

};

ostream& operator<<(ostream& os, const Circle& c);

16

Page 17: 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.

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

Page 18: 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.

Works the Same

18

Page 19: 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.

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