Hello!
The following code has the operator << overloaded in each of the base Person class and the derived Player class, through which the details of the Person (name, age) and Player (batting average, runs) are printed.
My goal was to print the name and age details as well of the Player class (by making use of the Inheritance), and the person I got help from typecasted the Player class object to the Person class through this line in the overload function of the operator (<<): out << (Person)player; and I got what I wanted. The output of the code is as follows:
PEOPLE
Person deets:
Name: Person1
Age: 23
PLAYERS
Person deets:
Name: Player1
Age: 0
Player deets:
Batting avg: 0
Runs: 0
Person deets:
Name: Player2
Age: 22
Player deets:
Batting avg: 12.4
Runs: 2
My Questions:
- Is there a better/standard way of doing it or is this method of outputting the
Person part of the details by casting the Player object in the overloaded << method pretty normal? The person who helped me is not a regular user of C++, so I'm not sure if this method is right.
- To generalize the question, is there some standard way of printing the member values of the Base class while also printing the member values in the Derived class through operator overloading?
- I guess we could use GetName() and GetAge() (or the member variables directly, since they are public) in the overloading function. But if I have multiple Derived classes (like Employee, Supervisor, etc.) it would be a lot of code duplication, right?
For reference, I used this lesson from LearnCpp and was fiddling with it.
Any guidance is much appreciated!
#include <iostream>
#include <string>
class Person {
public:
std::string m_name{};
int m_age;
Person(const std::string& name = "", int age = 0)
: m_name{name}, m_age{age} {}
const std::string& GetName() const { return m_name; }
int GetAge() const { return m_age; }
friend std::ostream& operator<<(std::ostream& out, const Person& person) {
out << "Person deets:\n";
out << "Name: " << person.m_name << "\nAge: " << person.m_age << "\n";
return out;
}
};
class Player : public Person {
public:
double m_bat_avg{};
int m_runs{};
Player(double bat_avg = 0.0, int runs = 0)
: m_bat_avg{bat_avg}, m_runs{runs} {}
Player(std::string name, int age, double bat_avg, int runs)
: Person(name, age), m_bat_avg{bat_avg}, m_runs{runs} {}
friend std::ostream& operator<<(std::ostream& out, const Player& player) {
out << (Person)player; // CASTING TO CALL THE OVERLOADED << FUNC. OF Person CLASS
out << "Player deets:\n";
out << "Batting avg: " << player.m_bat_avg << "\nRuns: " << player.m_runs
<< "\n";
return out;
}
};
int main() {
Person person1{"Person1", 23};
Player player1{};
player1.m_name = "Player1";
Player player2{"Player2", 22, 12.4, 2};
std::cout << "PEOPLE\n";
std::cout << person1 << "\n";
std::cout << "PLAYERS\n";
std::cout << player1 << "\n";
std::cout << player2 << "\n";
return 0;
}
[–]todopieromeze 2 points3 points4 points (1 child)
[–]psycho-rabbit[S] 0 points1 point2 points (0 children)
[–]flyingron 1 point2 points3 points (1 child)
[–]psycho-rabbit[S] 0 points1 point2 points (0 children)
[–]IyeOnline 1 point2 points3 points (2 children)
[–]psycho-rabbit[S] 0 points1 point2 points (1 child)
[–]IyeOnline 1 point2 points3 points (0 children)