class Vehicle{
long Number;
int Year;
char *Make,*Model,*BodyStyle,*Color;
float Cost;
friend ostream & operator<<(ostream& stream,const Vehicle& v);
class TruckVehicle:public Vehicle{
int Passengers;
long Mileage,GrossWeight,TempGross;
char *PoweredBy;
friend ostream & operator<<(ostream& stream,const TruckVehicle& t )
在上面的代码..如何调用重载&lt;&lt; &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;派生类的功能?? 回复将受到高度赞赏。
答案 0 :(得分:0)
最简单,最干净的方法是: -
class Base
{
public:
virtual ostream& put(ostream& s) const = 0;
};
ostream& operator<<(ostream& s, const Base& r)
{
return r.put(s);
}
class Derived : public Base
{
public:
ostream& put(ostream& s) const;
};
void f(const Base b, Derived d)
{
cout << b << d;
}
即put()是一个虚函数,可确保在&lt;&lt;。
中使用正确的输出操作