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;
   
}

>but that did not work.
You should describe exactly how it did not work, so we can stop guessing and actually figure out what the problem is.

>cout <<"Height :"<< getHeight()<<endl;
How in the world is the compiler supposed to know which object to call getHeight from? There's something special that needs to go before a member function...

By the way, you don't need to #include CPP files -- just header files; all that's necessary for a implementation file is that it's added to the project (assuming you're using an IDE).

>but that did not work.
You should describe exactly how it did not work, so we can stop guessing and actually figure out what the problem is.

>cout <<"Height :"<< getHeight()<<endl;
How in the world is the compiler supposed to know which object to call getHeight from? There's something special that needs to go before a member function...

By the way, you don't need to #include CPP files -- just header files; all that's necessary for a implementation file is that it's added to the project (assuming you're using an IDE).

Also I'd like to add, if I may, that when you use includes that you use conditional perprocessor directives to prevent inclusions. Basically in the preprocessor phase of the compiling process if you somehow indirectly or directly include multiple instances of the same header file, it will cause inclusions - the compiler won't know which function a function call is referring too. The mechanism used to prevent this from happening is really easy to implement and is really good programming practice, in my opinion. Here's an example:

Header file header.h

#ifndef HEADER_H
#define HEADER_H

.... Where you put the normal prototypes, declarations and/or etc.

#endif

Now when the preprocessor scans the above, it will essentially reckon this "If 1 header.h isn't defined (meaning isn't included already) then define it.

Good luck, LamaBot

>when you use includes that you use conditional perprocessor directives to prevent inclusions.
But the OP did this. Reread the code; there's nothing wrong with the way he/she implemented preprocessor directives.

But the OP did this. Reread the code; there's nothing wrong with the way he/she implemented preprocessor directives.

....albeit, in a wrong way.

When you write guards like that to prevent recursive inclusion, you need to specify the Header file name in the preprocessor directives and not the class name. The OP has used the class name (Ship_State) instead of the header file name (w.h).

>when you use includes that you use conditional perprocessor directives to prevent inclusions.
But the OP did this. Reread the code; there's nothing wrong with the way he/she implemented preprocessor directives.

Hey Joe, as ~S.O.S~ pointed out, she or he did use the class name. I provided a brief explaination because I figured she or he didn't know why to put the header file instead of the class name. I thought it might help him or her remember better if she or he knew what they're used for. It make me feel better if I know the OP understands thoroughly. I just wanted to make sure.

LamaBot

>you need to specify the Header file name in the preprocessor directives and not the class name.
Actually you don't. The compiler's preprocessor doesn't care what you use for your header sentinals, as long as it's something unique that hasn't been used elsewhere in your program.

It's a common convention to use the header file as the name, and although it's a good one, it's OK to use something else (as an example, Visual C++ 6.0 had a feature when it generated class files it would make a sentinal consisting of random digits to ensure its uniqueness).

Depending on your compiler, you can often use #pragma once to replace a header sentinal.

commented: Good catch, I actually missed on that one - ~s.o.s~ +14

>you need to specify the Header file name in the preprocessor directives and not the class name.
Actually you don't. The compiler's preprocessor doesn't care what you use for your header sentinals, as long as it's something unique that hasn't been used elsewhere in your program.

It's a common convention to use the header file as the name, and although it's a good one, it's OK to use something else (as an example, Visual C++ 6.0 had a feature when it generated class files it would make a sentinal consisting of random digits to ensure its uniqueness).

Depending on your compiler, you can often use #pragma once to replace a header sentinal.

Very true and I know. I guess I should rephrased that statement.

LamaBot

Very true and I know. I guess I should rephrased that statement.

LamaBot

Whoa those where a lot of responses. You guys respond quick. I have taken all your input and i am working on it right now. Just to clarify my teacher wants us to practice using .h and .cpp files like that. I know a little bit about recursion but have not really learnt much about it.
Thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.