简体   繁体   English

C ++,如何在派生类中调用Base类的重载提取运算符?

[英]C++, How do I call a Base classes' overloaded extraction operator in a derived class?

So this is a small part of a large assignment I have, I'm just unsure of the syntax for this. 所以这只是我所拥有的大型作业的一小部分,我只是不确定这个的语法。

I have a base class named Vehicle , which has these members: int fuelAmt and int fuelUsage ) 我有一个名为Vehicle的基类,它有以下成员: int fuelAmtint fuelUsage

(I am using namespace std ) (我正在使用命名空间std

I overloaded the << operator this way: 我以这种方式重载<<运算符:

ostream& operator<<(ostream& osObject, const Vehicle& myVehicle)
{
    cout << "Fuel Usage Rate: " << myVehicle.fuelUsage << endl
         << "Fuel Amount:     " << myVehicle.fuelAmt << endl;

    return osObject;
}

I then call it this way: 然后我这样称呼它:

cout << Vehicle;

The result is (example): 结果是(示例):

Fuel Usage Rate: 10;
Fuel Amount: 50;

I also have an Airplane class which derives from the Vehicle class, it introduces a new member: int numEngines . 我还有一个派生自Vehicle类的Airplane类,它引入了一个新成员: int numEngines

How can I overload the << operator in the Airplane class, so that it will first call the "Vehicle overloaded operator results", and then the results of whatever I tell the << operator to print from the derived class... So, here's what I mean: 如何重载Airplane类中的<<运算符,以便它首先调用“Vehicle overloaded operator results”,然后我告诉<<运算符从派生类打印的结果...所以,这就是我的意思:

I need it to function like this in the Airplane class: 我需要它在Airplane类中这样运行:

ostream& operator<<(ostream& osObject, const Airplane& myAirplane)
{
        //First print the Fuel Usage rate and Fuel amount by calling
        //the Base class overloaded << function

         //then
        cout << "Number of Engines: " << myAirplane.numEngines << endl;

    return osObject;
}

How do I trigger the base class execution of outputting its members' values, in this derived class? 如何在此派生类中触发输出其成员值的基类执行?

Is it something like changing the header? 这是改变标题吗? Like this: 像这样:

ostream& operator<<(ostream& osObject, const Airplane& myAirplane): operator<<Vehicle

How about the following: 以下内容如何:

ostream& operator<<(ostream& osObject, const Airplane& myAirplane)
{
    osObject << static_cast<const Vehicle &>(myAirplane);
    osObject << "Number of Engines: " << myAirplane.numEngines << endl;

    return osObject;
}

Since the operator << is a nonmember function, you can't declare it virtual, which is ideally what you want. 由于运算符<<是非成员函数,因此您无法将其声明为虚拟,这理想情况下是您想要的。 So you do the following 所以你做了以下几点

class Base
{
public:
    virtual std::ostream& output(std::ostream& out) const
    {
        return out << "Base";
    }
    virtual ~Base() {} //Let's not forget to have destructor virtual
};

class Derived : public Base
{
public:
    virtual std::ostream& output(std::ostream& out) const
    {
        Base::output(out); //<------------------------
        return out << "DerivedPart";
    }
    virtual ~Derived() {} //Let's not forget to have destructor virtual
};

and finally, have operator << for the base class only and the virtual dispatch will work its magic 最后,只有operator <<用于基类,虚拟调度将发挥其魔力

std::ostream& operator << (std::ostream& out, const Base& b) 
{
    return b.output(out);
}
ostream& operator<<(ostream& osObject, const Airplane& myAirplane)
{
     //First print the Fuel Usage rate and Fuel amount by calling
     //the Base class overloaded << function
     cout << (Vehicle& ) myAirplane;

     //then
     cout << "Number of Engines: " << myAirplane.numEngines << endl;

     return osObject;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将基类的重载运算符函数调用为派生类的函数 - how do i call the overloaded operator function of a base class into that of a derived class C ++重载运算符返回派生类对象而不是基类 - C++ overloaded operator to return the derived class object not the base class 如何在派生类中调用&lt;&lt;的基类运算符 - How do I call << operator of base class in the derived class 如何从派生类中调用在派生类中重载的模板类函数? - c++ How do you call a template class function that is overloaded in the derived class, from the derived class? 从基对象上的 C++ 类调用重载的派生函数不会调用重载函数 - Call overloaded, derived function from C++ class on base object does not call the overloaded function 如何使用基本 class 作为 C++ 中派生类的占位符? - How do I use a base class as a placeholder for derived classes in C++? 指向包含派生类对象和重载&lt;&lt;运算符的基类的指针数组 - Array of pointers to the base class that contains objects of derived classes and overloaded << operator C ++ - 使派生类从基类“继承”重载赋值运算符的安全/标准方法 - C++ - Safe/standard approach to having a derived class “inherit” the overloaded assignment operator from a base class c++ inheritance:基础 class 中的 2 次重载运算符 + 在派生的 ZA2F2ED4F8EBC2CBB14C21A29DC4 中无法正常工作 - c++ inheritance: 2 times overloaded operator+ in base class are not working proper in derived class 如何在基本抽象类中调用重载运算符? - How should I call an overloaded operator in a base abstract class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM