C++ 多态打印

C++ Polymorphic printing

假设我们有 class 个具有两个字段的人 - 姓名和号码。 Class Student 继承 Person 并添加另一个名为 averageGrade 的字段。

我已经为 Person 和 Student 定义了运算符“<<”,并希望能够拥有一个 Person 数组,该数组也将包含 Student 对象。当我想打印数组中恰好是 Student 的元素时,我希望调用特定于 Student 的运算符“<<”的定义,而不是 Person 的定义。

如何做到这一点?

person.h:

#pragma once
#include <iostream>
#include <string>
using namespace std;


class Person
{
    private:
        string name;
        int number;
    public:
        Person();
        Person(string,int);

        friend ostream& operator<<(ostream& os, const Person& person);
};

person.cpp:

#include "person.h"

Person::Person() : Person("defaultName", 0)
{
}

Person::Person(string name, int number)
{
  this->name = name;
  this->number = number;
}

ostream& operator<<(ostream& os, const Person& person)
{
  os << "Name: " << person.name << endl;
  os << "Number: " << person.number;

  return os;
}

student.h:

#pragma once
#include "person.h"

class Student : public Person
{
  private:
    double averageGrade;
  public:
    Student();
    Student(string, int, double);

    friend ostream& operator<<(ostream& os, const Student& student);
};

student.cpp:

#include "student.h"
Student::Student() : Person()
{
  this->averageGrade = 5.0;
}

Student::Student(string name, int number, double avgGrade) : Person(name, number)
{
  this->averageGrade = avgGrade;
}

ostream& operator<<(ostream& os, const Student& student)
{
  os << (Person) student << endl;
  os << "Average grade: " << student.averageGrade;

  return os;
}

main.cpp:

#include "student.h"

int main()
{

  Person people[10];
  people[0] = Person("Tom", 1);
  people[1] = Student("Greg", 6, 5.74);
  cout << people[0] << endl;
  cout << people[1] << endl; // prints only the name and number, without the grade


  return 0;
}

一种方法可以看起来很简单。

在每个 class 中定义一个 public 或受保护的虚函数,例如

virtual std::ostream & out( std::ostream & );

然后把输出操作符写成

friend std::ostream & operator <<( std::ostream &os, const Person &person )
{
    return person.out( os );
}

friend std::ostream & operator <<( std::ostream &os, const Student &student )
{
    return student.out( os );
}

或者只有第一个运算符。

请注意,您不能声明一个将 Person 和 Students 类型的对象一起存储的数组。