user1964520
user1964520

Reputation: 11

Please explain the output of this class constructor

#include <iostream>

using namespace std;

class tricie
{
public:
    tricie(int);
    ~tricie();
    void acc();
    void deacc();
private:
    int df_speed=8;
};

tricie::tricie(int speed_input)                //Shouldn't this make the speed=8?
{
    int speed;
    if (speed_input > df_speed)
    {
        speed=speed_input;
        cout<<"speed now is "<<speed<<" km/hr \n";
    }
    else
    {
        speed=df_speed;
        cout<<"speed now is "<<speed<<" km/hr \n";
    }
}
tricie::~tricie()
{

}
void tricie::acc()
{
    int speed=(speed+1);
    cout<<"speed now is "<<speed<<" km/hr \n";
}
void tricie::deacc()
{
    int speed=(speed-1);
    cout<<"speed now is "<<speed<<" km/hr \n";
}
int main(int argc, const char * argv[])  //Xcode but these stuff in the bracket (ignore)
{
    tricie biker1(4);          //If i put any number > 8 the program work correctly
    biker1.acc();              //output from this line is "speed now is 5 km/hr "    instead of 9
    biker1.deacc();            //output from this line is "speed now is 4 km/hr "    instead of 8
    biker1.deacc();            //output from this line is "speed now is 3 km/hr "    instead of 7
    return 0;
}

I want to know if I am missing a concept which tells me why the output 8 5 4 3 not 8 9 8 7?

Thanks in advance and sorry if the question is stupid i am teaching myself using sams teach yourself c++ in 24 hrs book.

Thanks everyone for the quick response i will search if there is a way to prevent compiling in these cases put i have a follow up question: When I put int speed in the private part of the class and it is working fine but i want to know is there another error i am not getting since i put it in the private part of the class and the main function can see it?

Upvotes: 0

Views: 106

Answers (2)

Veger
Veger

Reputation: 37906

You have an error in your code. You redeclare the speed variable in all of your functions (constructor, acc() and deacc()), for example in deacc():

int speed=(speed+1);

Initially speed in undeclared (in this scope), but you use it to subtract 1 from it. Which is (totally) undefined behavior.

To fix this, remove the variable declarations (int) in these 3 situations and add a class variable to the class to store the current speed:

class tricie {
  //...
  int speed;
  //...
}

void tricie::deacc()
{
    speed=(speed-1);
    cout<<"speed now is "<<speed<<" km/hr \n";
}

Same for acc() and in the constructor remove int speed; completely.

Note: I do not know what df_speed is, I assume(d) 'default speed'. If it is the class variable that should hold the actual speed, you need to rename all speed variable usages to df_speed. But this depending on the meaning of df_speed (which is probably described in your book..?)

Upvotes: 2

Mario
Mario

Reputation: 36487

There's something wrong with your basic concept, because you're using local variables (int speed) that will lose their value once you leave functions. What you'll most likely want is a member variable (just like df_speed).

The output in your constructor is correct, because you set the value either way.

However, the output of tricie::acc() and tricie::deacc() is undefined, as you're using uninitialized variables (speed) in assignments/calculations.

Upvotes: 2

Related Questions