[英]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 fuelAmt
和int 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.