pexea12
pexea12

Reputation: 1209

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. 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

Upvotes: 0

Views: 928

Answers (1)

David G
David G

Reputation: 96810

inStream >> object and outStream << object are both recursive calls because the static type of object is Truck, not Vehicle. Use a virtual print and get method that Truck overrides. Call object.print(outStream) in the inserter and object.get(inStream) in the extractor in order to achieve polymorphic I/O through inheritance:

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;
}

Upvotes: 3

Related Questions