Download presentation
Presentation is loading. Please wait.
Published byKenneth Hart Modified over 9 years ago
1
1 More Operator Overloading Chapter 11
2
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
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?
4
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
5
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
6
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
7
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
8
Program Running 8
9
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
10
Compile Time Errors 10
11
The error line 11
12
12 Correction in Circle.h friend ostream& operator<<(ostream& os, const Circle& c);
13
13 Correction in Circle.cpp ostream& operator<<(ostream& os, const Circle& c) { os << c.name << " Radius " << c.radius; return os; }
14
Now It Works 14
15
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
16
Circle.h class Circle {... double Radius() const {return radius;}; const char* Name() const {return name;};... }; ostream& operator<<(ostream& os, const Circle& c); 16
17
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
18
Works the Same 18
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.