Operator Overloading

Overloading the Stream Insertion Operator

How can I overload the << operator to print objects of my custom type?

Abstract art representing computer programming

To overload the << operator for printing objects of your custom type, you need to define it as a non-member function that takes an ostream reference and a const reference to your custom type as parameters. The function should return the ostream reference to allow for chaining multiple insertions.

Here's an example of overloading the << operator for a custom Player class:

#include <iostream>
#include <string>

class Player {
private:
  std::string name;
  int score;

public:
  Player(const std::string& n, int s)
    : name(n), score(s) {}

  friend std::ostream& operator<<(
    std::ostream& os, const Player& player) {
    os << "Player: " << player.name
      << ", Score: " << player.score;
    return os;
  }
};

int main() {
  Player p1("Alice", 100);
  Player p2("Bob", 200);

  std::cout << p1 << "\n";
  std::cout << p2 << "\n";
}
Player: Alice, Score: 100
Player: Bob, Score: 200

In this example, we define the << operator as a non-member function that takes an ostream reference (os) and a const reference to a Player object. The function accesses the private members of the Player class, so we declare it as a friend function inside the class.

Inside the function, we use the << operator to insert the desired output format into the ostream. We return the ostream reference to allow for chaining multiple insertions.

Now we can use the << operator with Player objects just like we would with built-in types, printing them to the console or any other output stream.

By overloading the << operator, we provide a convenient way to print objects of our custom type, making the code more readable and avoiding the need for explicit printing functions.

Remember to include any necessary headers (e.g., <iostream> for std::ostream) and to adjust the output format according to your specific requirements.

This Question is from the Lesson:

Operator Overloading

Discover operator overloading, allowing us to define custom behavior for operators when used with our custom types

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Operator Overloading

Discover operator overloading, allowing us to define custom behavior for operators when used with our custom types

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved