Why wont my code run I want to inherit everything from sailb

Why won\'t my code run? I want to inherit everything from sailb.h class (base) into raceb.h class (derived) and in main I want to set a raceb member variable equal to one of sailb member variables but it does not work. language is c++

////////////////////////////////////Base Class////////////////////////////////////////

#ifndef sailb_h
#define sailb_h
#include <iostream>

using namespace std;

class sailb {
public:
sailb();
double setWindSpeed(); //modifier
double getWindSpeed(); //accessor
private:
double windSpeed;
};
#endif

////////////////////////////////derived class/////////////////////////////////////////////////////////////////

#ifndef raceb_h
#define raceb_h
#include \"sailb.h\"

class raceb: public sailb {
public:
raceb();
void setDownWind();
double getDownWind();
  
p rivate:
double downWindSpeed;
};
#endif

///////////////main.cpp/////////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <cmath>
#include \"sailb.h\"
#include \"raceb.h\"

using namespace std;

sailb::sailb() {windSpeed = 0;}
raceb::raceb() {}

void sailb::setWindSpeed() {
cout << \"What is the speed of the wind in knots.\ \";
cin >> windSpeed;
}

double sailb::getWindSpeed() {
return windSpeed;
}

void raceb::setDownWind() {
downWindSpeed = getWindSpeed();
}

double raceb::getDownWind() {
return downWindSpeed;
}

int main() {
sailb s;

s.setWindSpeed();

raceb r;
r.setDownWind();
cout << r.getDownWind();

///////// Output = 0. So the windSpeed is being set to the default windspeed, but i want it to be set to the speed that the user inputs.

Solution

You are creating two different objects. Both of the objects share different memory locations. To make it work what you are trying to achieve just use one object of derived class. Main function will look like:

int main() {
raceb r;
  
   r.setWindSpeed();
  
r.setDownWind();
cout << r.getDownWind();

return 0;   
}

-----------

One more error I saw is you have defined the prototype of \"setWindSpeed();\" as \"double\". Make it void. For example:

class sailb {
public:
sailb();
void setWindSpeed(); / <------------------ it was double in your code
double getWindSpeed(); //accessor
private:
double windSpeed;
};

Why won\'t my code run? I want to inherit everything from sailb.h class (base) into raceb.h class (derived) and in main I want to set a raceb member variable eq
Why won\'t my code run? I want to inherit everything from sailb.h class (base) into raceb.h class (derived) and in main I want to set a raceb member variable eq

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site