C++ 子类中的重载运算符

C++ 子类中的重载运算符,c++,operator-overloading,C++,Operator Overloading,导游和导游。导游延伸了旅游课程。我在tour类中重载>操作符 我的旅游课看起来像 #include <iostream> #include <vector> #include "Customer.h" using namespace std; class Tour { protected: string id; string description; double fee; vector<string> customerL

导游和导游。导游延伸了旅游课程。我在tour类中重载>操作符

我的旅游课看起来像

#include <iostream>
#include <vector>
#include "Customer.h"

using namespace std;

class Tour {

protected:
    string id;
    string description;
    double fee;
    vector<string> customerList;

public:
    Tour();
    Tour(string idVal, string descriptionVal, double feeVal);
    string getId();
    string getDescription();
    double getFee();
    double getTotalForTour();
    virtual void addCustomer(string cust);
    vector<string> getCustomers();
    virtual void display();

    friend ostream& operator<< (ostream &out, Tour &cust);
    friend istream& operator>> (istream &in, Tour &cust);
};
#包括
#包括
#包括“Customer.h”
使用名称空间std;
集体游{
受保护的:
字符串id;
字符串描述;
双倍收费;
矢量客户列表;
公众:
旅游();
巡视(字符串idVal、字符串描述VAL、双feeVal);
字符串getId();
字符串getDescription();
双倍费用();
双getTotalForTour();
虚拟客户(字符串cust);
向量getCustomers();
虚拟虚空显示();
friend ostream&operator>(istream&in、Tour&cust);
};
那么我的导游是这样的,

#include <iostream>
#include "Tour.h"
#include "SimpleDate.h"

using namespace std;

class GuidedTour : public Tour {

private:
    SimpleDate* date;
    string guideName;
    int maxNumTourists;

public:
    GuidedTour();
    GuidedTour(string idVal, string descriptionVal, double feeVal, SimpleDate* dateVal, string guideNameVal, int maxNumTouristsVal);
    virtual void addCustomer(string cust);
    SimpleDate* getDate();
    void display();

    friend ostream& operator<< (ostream &out, GuidedTour &cust);
    friend istream& operator>> (istream &in, GuidedTour &cust);
};
#包括
#包括“Tour.h”
#包括“SimpleDate.h”
使用名称空间std;
课程指南主题:公共旅游{
私人:
简化日期*日期;
字符串名称;
国际最大旅游者;
公众:
引导图();
GuidedTour(字符串idVal、字符串descriptionVal、double feeVal、SimpleDate*dateVal、字符串guideNameVal、int maxNumTouristsVal);
虚拟客户(字符串cust);
SimpleDate*getDate();
void display();
friend ostream&operator>(istream&in、GuidedTour&cust);
};
我想在子类上以不同的方式重载这些运算符,以执行其他操作

我有一个包含旅游和导游的向量

当我循环遍历向量并执行以下操作时

for (unsigned int i = 0; i < tourListVector.size(); i++) {

    cout << *tourListVector[i];
}
for(unsigned int i=0;iCUT< P>如果我理解正确,你就有指针在你的向量中。所以你应该使用关键词Virtual。在C++中阅读虚拟方法。

你几乎做对了,但不是很好。让我们先来看看输出案例——输入案例的工作原理是一样的。

首先,你应该申报

virtual void write(std::ostream&) const;
基类中的成员函数。实现可能类似于:

void Tour::write(std::ostream& os) const
{
    os << "ID: " << id << std::endl;
    os << "Description: " << description << std::endl;
    // etc
}
比如说


解释:忘记您当前的
操作员可能帮助虚拟
读取(std::stream&)
写入(std::ostream&)
从友好的基类运算符调用将消除对派生类中运算符重写的需要。派生重写
read
write
(如果需要,甚至可以直接调用基类作为其派生功能的一部分)。你能再解释一下吗?对不起,我对C++很陌生。@Archie Any将解释这一点和更多。@Archie.Loki在这个问题上的回答是一个很好的总结,正如你将在有限的SO答案空间中看到的那样。如果我使用虚拟,我会得到错误教程。h:36:错误:虚拟函数不能是friends@Archie:您覆盖了一个
打印
虚拟函数并具有
运算符
void GuidedTour::write(std::ostream& os) const
{
    Tour::write(os); // Write out base Tour info first
    os << "Guide Name: " << guideName << std::endl;
    // etc
}
std::ostream& operator<<(std::ostream& os, const Tour& tour)
{
    tour.write(os);
    return os;
}
void do_something(Tour& t); // (a)
void do_something(GuidedTour& gt); // (b)