| 1 |
#include <iostream> |
| 2 |
|
| 3 |
using namespace std; |
| 4 |
|
| 5 |
template <class Derived> |
| 6 |
class Vehicle |
| 7 |
{ |
| 8 |
public: |
| 9 |
void mass() |
| 10 |
{ |
| 11 |
static_cast<Derived *>(this)->vmass(); |
| 12 |
} |
| 13 |
|
| 14 |
// no need to implement vmass here, as it should be implemented |
| 15 |
// by derived classes. |
| 16 |
}; |
| 17 |
|
| 18 |
class Car: public Vehicle<Car> |
| 19 |
{ |
| 20 |
friend void Vehicle<Car>::mass(); |
| 21 |
|
| 22 |
private: |
| 23 |
void vmass() |
| 24 |
{ |
| 25 |
cout << "Car's vmass called\n"; |
| 26 |
} |
| 27 |
}; |
| 28 |
|
| 29 |
class Truck: public Car |
| 30 |
{ |
| 31 |
private: |
| 32 |
void vmass() |
| 33 |
{ |
| 34 |
cout << "Car's vmass called\n"; |
| 35 |
} |
| 36 |
}; |
| 37 |
|
| 38 |
int main() |
| 39 |
{ |
| 40 |
Truck{}.mass(); |
| 41 |
} |