Add tests with stateless/static function
This commit is contained in:
BIN
CM2005 Object Oriented Programming/Topic 5/5.2.1/a.exe
Normal file
BIN
CM2005 Object Oriented Programming/Topic 5/5.2.1/a.exe
Normal file
Binary file not shown.
40
CM2005 Object Oriented Programming/Topic 5/5.2.1/test.cpp
Normal file
40
CM2005 Object Oriented Programming/Topic 5/5.2.1/test.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
class Car
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Car();
|
||||||
|
void accelerate();
|
||||||
|
static int whichIsFaster(Car carA, Car carB);
|
||||||
|
private:
|
||||||
|
float speed;
|
||||||
|
};
|
||||||
|
|
||||||
|
Car::Car() : speed(0.0f)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Car::accelerate()
|
||||||
|
{
|
||||||
|
speed += 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Car::whichIsFaster(Car carA, Car carB)
|
||||||
|
{
|
||||||
|
if(carA.speed > carB.speed)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Car car1{};
|
||||||
|
car1.accelerate();
|
||||||
|
// state of car1 - speed is 1.5
|
||||||
|
Car car2;
|
||||||
|
car2.accelerate();
|
||||||
|
car2.accelerate();
|
||||||
|
// static functions don't need to remember or access the state of the function
|
||||||
|
Car::whichIsFaster(car1, car2);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user