C++ examples for Class:final
Preventing Method Overrides, Using the final Keyword
#include <cinttypes> #include <iostream> using namespace std; class Vehicle//from w w w . ja va 2s . c o m { public: Vehicle() = default; virtual int GetNumberOfWheels() const final { return 2; } }; class Car : public Vehicle { public: Car() = default; // int GetNumberOfWheels() const override // { // return 4; //} }; class Motorcycle : public Vehicle { public: Motorcycle() = default; }; int main(int argc, char* argv[]) { Vehicle* pVehicle{}; Vehicle myVehicle{}; pVehicle = &myVehicle; cout << "A vehicle has " << pVehicle->GetNumberOfWheels() << " wheels." << endl; Car myCar{}; pVehicle = &myCar; cout << "A car has " << pVehicle->GetNumberOfWheels() << " wheels." << endl; Motorcycle myMotorcycle; pVehicle = &myMotorcycle; cout << "A motorcycle has " << pVehicle->GetNumberOfWheels() << " wheels." << endl; return 0; }