-2

This code is from "Sams Teach Yourself C++".It might be something simple but I'm having a hard time trying to figure this out. I get the same output if I don't use the getSpeed() method. So do I need this? If not, why does this book use it?

#include <iostream>

class Tricycle 
{
public :
    int getSpeed();
    void setSpeed(int speed);
    void pedal();
    void brake();
private :
int speed;
};

int Tricycle::getSpeed()   //<-- Why do I need this method
{
    return speed;
}

void Tricycle::setSpeed(int newSpeed) 
{
    if (newSpeed >= 0)
    {
        speed = newSpeed;
    }
}

void Tricycle::pedal() 
{
    setSpeed(speed + 1);
    std::cout << "\nPedaling; tricycle speed " << speed << " mph\n";
}

void Tricycle::brake() 
{
    setSpeed(speed - 1);
    std::cout << "\nBraking ; tricycle speed " << speed << " mph\n";
}

int main()
{
    Tricycle wichita;

    wichita.setSpeed(0);
    wichita.pedal();
    wichita.pedal();
    wichita.brake();
    wichita.brake();
    wichita.brake();
    return 0;
}
  • 2
    Read up on getters and setters. There is an ongoing debate whether you need them in c++ or YAGNI. Here is a nice list of [C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). –  Jan 03 '17 at 06:47
  • 2
    It's not used in the code you show. presumably it will come up later. It's an accessor function. – john elemans Jan 03 '17 at 06:47
  • For sake of completeness and most importantly encapsulation the method `getSpeed()` is defined. So that you can have/access the current speed of `Tricycle` at any point of time. – sameerkn Jan 03 '17 at 06:54
  • It's all a bad abstraction really. On my real bike, I must use `pedal()` to get some speed (and even to maintain it). There is no `setSpeed` unless you have a powered bike. See [Are getters and setters poor design?](http://stackoverflow.com/questions/565095/are-getters-and-setters-poor-design-contradictory-advice-seen) – Bo Persson Jan 03 '17 at 07:27

3 Answers3

0

This Method return the Value from Speed.

So if you call setSpeed with value greater as 0 then the Speed Value are set to the new Value. Declared as private int variable.

As example

int main()
{
Tricycle wichita;
wichita.setSpeed(10);
int mySpeed= wichita.getSpeed();
}

The Value of mySpeed is now 10.

ivorysmoker
  • 118
  • 10
0

Since Speed is a private variable, you cannot retrive/set its value outside the class scope. So here we have used setSpeed and getSpeed public functions through which we can retrive/set Speed to tricycle object outside the class scope.

For example,

Tricycle myTricycle = new Tricycle();

// to set speed of tricycle use,

myTricycle.setSpeed(100);

// to retrive speed of tricycle object use,

myTricycle.getSpeed(); // returns 100;
skull_king
  • 70
  • 8
0

Because you cannot access the private members directly from the main() function or somewhere else. But you can use a public function to access the private elements of an object of any class. In spite of this, you cannot access those private elements. In your code, speed is a private member and to get the value of this, a public function namely getSpeed is being used.

Najat
  • 167
  • 14