简体   繁体   English

C ++中具有继承类的流

[英]stream with inherited class in C++

I have two class: The parent class: Vehicle 我有两节课:家长课:车辆

class Vehicle {
    private:
        string manufacturer;
        int cylinder;
        Person owner;
    public:
        Vehicle();
        Vehicle(const Vehicle& theObject);
        friend istream& operator >>(istream& inStream, Vehicle& object);
        friend ostream& operator <<(ostream& outStream, const Vehicle& object);
};

and the child class: Truck I overload operator<< and operator>> so that I can use cout and cin with my class Truck. 子类:Truck I重载operator<<operator>>以便我可以在我的Truck类中使用coutcin Here is my definition with these two operators: 这是我对这两个运算符的定义:

class Truck : public Vehicle {
    private:
        double loadCapacity;
        int towingCapacity;
    public:
        Truck();
        Truck(const Truck& object);
        friend istream& operator >>(istream& inStream, Truck& object);
        friend ostream& operator <<(ostream& outStream, const Truck& object);
};

istream& operator >>(istream& inStream, Truck& object) {
    cout << endl << "Insert truck: "; inStream >> object;
    cout << "Insert load capacity: "; inStream >> object.loadCapacity;
    cout << "Insert towing capacity: "; inStream >> object.towingCapacity;
    return inStream;
}

ostream& operator <<(ostream& outStream, const Truck& object) {
    outStream << object;
    outStream << endl << "Load capacity: " << object.loadCapacity;
    outStream << endl << "Towing capacity: " << object.towingCapacity;
    return outStream;
}

When I try to use 当我尝试使用

Truck object;
cin >> object;
cout << object

It does not work out as I think. 我认为这行不通。 Can anybody explain why? 有人可以解释为什么吗? Thank you 谢谢

inStream >> object and outStream << object are both recursive calls because the static type of object is Truck , not Vehicle . inStream >> objectoutStream << object都是递归调用,因为object的静态类型是Truck ,而不是Vehicle Use a virtual print and get method that Truck overrides. 使用Truck覆盖的虚拟printget方法。 Call object.print(outStream) in the inserter and object.get(inStream) in the extractor in order to achieve polymorphic I/O through inheritance: 调用插入器中的object.print(outStream)和提取器中的object.get(inStream) ,以通过继承实现多态I / O:

class Vehicle {
private:
    string manufacturer;
    int cylinder;
    Person owner;
public:
    Vehicle();
    Vehicle(const Vehicle& theObject);
    virtual void print(std::ostream& os) const {
        os << manufacturer << cylinder << owner;
    }
    virtual void get(std::istream& is) {
        is >> manufacturer >> cylinder >> owner;
    }
};

class Truck : public Vehicle {
private:
    double loadCapacity;
    int towingCapacity;
public:
    Truck();
    Truck(const Truck& object);
    void print(std::ostream& os) const {
        Vehicle::print(os);
        os << loadCapacity << towingCapacity;
    }
    void get(std::istream& is) {
        Vehicle::get(is);
        is >> loadCapacity >> towingCapacity;
    }
};

std::istream& operator>>(std::istream& inStream, Vehicle& object) {
    object.get(inStream);
    return inStream;
}

std::ostream& operator<<(std::ostream& outStream, const Vehicle& object) {
    object.print(outStream);
    return outStream;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM