Content deleted Content added
m Reverted 1 edit by 185.20.29.54 (talk) to last revision by Frap (TW) |
m →C++: Improve C++ example |
||
Line 218:
=== C++ ===
<source lang=Cpp>
#include <iostream>
#include <memory>
class ICar {
public:
virtual ~ICar() { std::cout << "ICar destructor!" << std::endl; }▼
virtual void DriveCar() = 0;
▲ std::cout << "ICar destructor!" << std::endl;
}▼
};
class Car : public ICar {
public:▼
void DriveCar() override {▼
void DriveCar() override { std::cout << "Car has been driven!" << std::endl; }
};
class ProxyCar : public ICar {
public:
private:▼
▲public:
▲ ProxyCar (int driver_age) : realCar(new Car()), _driver_age(driver_age) {}
void DriveCar() {
if (
} else {
std::cout << "Sorry, the driver is too young to drive." << std::endl;
▲ }
}
▲ private:
std::unique_ptr<ICar> real_car_ = std::make_unique<Car>();
int driver_age_;
};
int main() {▼
▲int main()
▲ ICar* car = new ProxyCar(16);
}
</source>
|