I am having a problem with accessor. How can i get it to read height?
They are 3 files .h(CLASS) , .cpp(IMPLEMENTATION) , and another .cpp(PROGRAM).
i tried putting a getHeight in the class and describing it in the implementation, but that did not work.
I want the getHeight to return height_ (Its in the private section).
Here is the code in this order. .h . cpp . cpp(Program)
#ifndef SHIP_STATE
#define SHIP_STATE
#include <iostream>
class ship_state
{
public:
ship_state();
ship_state(double h, double v, double f);
double height();
double getHeight();
double velocity();
double fuel();
int landed();
ship_state& update(double rate);
private:
double height_;
double velocity_;
double fuel_;
};
#endif
IMPLEMENTATION FILE
#include "w.h"
#include <iostream>
using namespace std;
//int print(ostream& os);
static const double dt=1.0;
static const double gravity=0.5;
static const double engine_strength=1.0;
static const double safe_velocity = -0.5;
static const char burn_key='b';
ship_state::ship_state(double h, double v, double f)
{
height_ = h;
velocity_ = v;
fuel_ = f;
}
double ship_state::getHeight()
{
return height_;
}
double ship_state::velocity()
{
return velocity_;
}
double ship_state::fuel()
{
return fuel_;
}
int ship_state::landed()
{
if (height_<=0)
return 1;
return 0;
}
ship_state& ship_state::update(double rate)
{
double dh;
double dv;
if (fuel_<=0) {
fuel_=0;
rate=0;
}
dh=velocity_*dt;
dv=engine_strength*rate-gravity;
fuel_ -= (rate*dt);
height_ += dh;
velocity_+=dv;
return *this;
}
void end_game(ship_state&s);
double get_burn_rate();
void lander_loop(ship_state& s)
{
if (s.landed())
end_game(s);
else
lander_loop(s.update(get_burn_rate()));
}
void end_game(ship_state& s)
{
double v=s.velocity();
cout<<"Final velocity: "<<v;
if (v >= safe_velocity)
cout<<"...good landing!\n";
else
cout<<"...you crashed!\n";
}
double get_burn_rate()
{
cout<<"Enter "<< burn_key <<" to burn fuel, any other to float :";
char ch;
cin>>ch;
if (ch==burn_key)
return 1.0;
else
return 0.0;
}
THE PROGRAM
#include "w.h"
#include "w.cpp"
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to Lunar Lander. The object is to land the ship with a"
<< " final" << endl << "velocity greater than " << safe_velocity
<< endl << endl;
ship_state s(50.0, 0.0, 20.0);
lander_loop(s);
//cout <<"Velocity :"<< velcoity_ <<endl;
cout <<"Height :"<< getHeight()<<endl;
}