Change the member functions to inline. - C++ Class

C++ examples for Class:Member Function

Description

Change the member functions to inline.

Demo Code

                                 
#include <iostream> 
                                  /*  w  w  w  .jav  a 2  s .c  o m*/
class Bike 
{ 
public: 
    Bike(int initialSpeed); 
    ~Bike(); 
    int getSpeed() const { return speed; } 
    void setSpeed(int speed); 
    void pedal() 
    { 
          setSpeed(speed + 1); 
          std::cout << "Pedaling " << getSpeed() << " mph\n\n"; 
    } 
    void brake() 
    { 
          setSpeed(speed - 1); 
           std::cout << "Pedaling " << getSpeed() << " mph\n"; 
    } 
private: 
    int speed; 
};  
                                 
// constructor for the object 
Bike::Bike(int initialSpeed) 
{ 
    setSpeed(initialSpeed); 
} 
                                  
// destructor for the object 
Bike::~Bike() 
{ 
    // do nothing 
} 
                                  
// set the trike's speed 
void Bike::setSpeed(int newSpeed) 
{ 
    if (newSpeed >= 0) 
        speed = newSpeed; 
} 
                                  
// create a trike and ride it 
int main() 
{ 
    Bike my(5); 
    my.pedal(); 
    my.pedal(); 
    my.brake(); 
    my.brake(); 
    my.brake(); 
    return 0; 
}

Result


Related Tutorials