我正在尝试使用继承运行程序,但遇到了与所需输出匹配的问题。我将在下面发布所有内容。
HEADER FILES:
class Vehicle {
public:
virtual void accelerate() = 0;
virtual void brake() = 0;
virtual std::ostream& display(std::ostream& ostr) const =0;
};
class Car: public Vehicle {
int speed_;
int maxSpeed_;
protected:
void speed(int value);
int maxSpeed() const;
public:
int speed() const;
Car(int m = 100);
};
class SportCar: public Car{
int noOfPassengers_;
public:
SportCar();
SportCar(int speed, int pass);
// implementations of Vehicle's pure virtual methods
void accelerate();
void brake();
std::ostream& display(std::ostream& os) const;
};
class PickupTruck: public Car {
int loadAmount_;
char loadedMaterial_[31];
public:
PickupTruck();
void load(const char* loadedMaterial, int loadAmount);
void unload();
bool isEmpty()const;
// implementations of Vehicle's pure virtual methods
void accelerate();
void brake();
std::ostream& display(std::ostream& os) const;
};
CPP文件:
void Car::speed(int value) {
speed_ = value;
if (value > maxSpeed_ ) {
maxSpeed_ = value;
}
else if (value < 0) {
speed_ = 0;
}
}
int Car::maxSpeed()const {
return maxSpeed_;
}
int Car::speed() const {
return speed_;
}
Car::Car(int m) {
maxSpeed_ = m;
speed_ = 0;
}
SportCar::SportCar() {
noOfPassengers_ = 1;
}
SportCar::SportCar(int speed, int pass): Car(speed) {
noOfPassengers_ = pass;
}
void SportCar::accelerate() {
speed(speed() + 40);
}
void SportCar::brake() {
speed(speed() - 10);
}
std::ostream& SportCar::display(std::ostream& os) const {
if (speed() > 0) {
os << "This sport car is carrying" << noOfPassengers_ << "passengers and is traveling at a speed of " << speed() << "km/h";
}
else {
os << "This sport car is carrying" << noOfPassengers_ << "passengers and is parked";
}
return os;
}
PickupTruck::PickupTruck() {
loadAmount_ = 0;
loadedMaterial_[0] = char(0);
}
bool PickupTruck::isEmpty()const {
return loadAmount_ == 0;
}
void PickupTruck::load(const char* loadedMaterial, int loadAmount) {
std::strcpy(loadedMaterial_, loadedMaterial);
loadAmount_ = loadAmount;
}
void PickupTruck::accelerate() {
speed(speed() + 20);
}
void PickupTruck::brake()
{
speed(speed() - 5);
}
void PickupTruck::unload() {
loadAmount_ = 0;
}
std::ostream& PickupTruck::display(std::ostream& os) const
{
os << "This pickup truck is ";
if (isEmpty()) {
os << "not carrying any load";
}
else {
os << "carrying " << loadAmount_ << " kgs of " << loadedMaterial_;
}
if (speed() > 0) {
os << ", traveling at the speed of " << speed() << " km/h.";
}
else {
os << " and is parked.";
}
return os;
}
主要(下面有所需的输出):
void drive(Car& cDrive) {
cDrive.accelerate();
cDrive.brake();
cDrive.display(cout);
}
void stop(Car& cStop) {
if (cStop.speed() > 0) {
cStop.brake();
}
cStop.display(cout);
}
int main()
{
SportCar Tesla(240, 2);
PickupTruck Ford;
Tesla.display(cout) << endl;
Ford.display(cout) << endl;
Ford.load("Bricks", 3500);
drive(Tesla);
drive(Ford);
stop(Tesla);
stop(Ford);
Ford.unload();
Tesla.display(cout) << endl;
Ford.display(cout) << endl;
return 0;
}
/* output
This sport car is carrying 2 passengers and is parked.
This pickup truck is not carrying any load and is parked.
This sport car is carrying 2 passengers and is traveling at a speed of 30 km/h.
This pickup truck is carrying 3500 kgs of Bricks, traveling at the speed of 15 km/h.
This sport car is carrying 2 passengers and is parked.
This pickup truck is carrying 3500 kgs of Bricks and is parked.
This sport car is carrying 2 passengers and is parked.
This pickup truck is not carrying any load and is parked.
*/
我得到的错误:
输出的第5行: 输出应该是: 这辆跑车载有2名乘客并停放。 但你的输出是: 这辆跑车载有2名乘客,行驶速度为20公里/小时。
我现在已经坚持了一段时间,任何朝着正确方向的帮助都会很棒。谢谢!
答案 0 :(得分:0)
这些功能未输出新行:
void drive(Car& cDrive)
{
cDrive.accelerate();
cDrive.brake();
cDrive.display(cout); //< Should also write endl
}
void stop(Car& cStop)
{
if (cStop.speed() > 0) {
cStop.brake();
}
cStop.display(cout); //< Should also write endl
}
您可以看到working code here。