C++ Header File Interaction -


ok, pretty new c++, have couple of questions header files...

1.) variables should declare, , should not in header file?

2.) when declare variable in header file, should use extern?

here header file:

#ifndef main_h #define main_h  class main { public:         int main(); //constructor         virtual ~main(); //destructor          double initialvelocity;         double initialangle;  private:         double degtorad(double angle);         void simulate(double angle, double velocity); }; #endif 

and here main.cpp

/*******************************************************************  * program take input initial velocity (fps), , launch angle  * based on information, current posotion of object thrown  * calculated until hits ground.  *  *   * date: 30 august 2013  * version 1.0  * **/  # include "main.h" # include <iostream> # include <fstream> # include <cmath> using namespace std;  /******************************************************************  * general variables **/ const int gravity_factor = -16; const int ground = 0; const double pi = atan(1.0)*4; double initialvelocity; double initialangle;  /******************************************************************  * degtorad function.  *  * function takes in angle in degrees, , converts  * radians.  * **/ double degtorad(double angle){     return angle * (pi/180);     }  /******************************************************************  * simulate function.  *  * takes in angle in radians, , velocity. calculates  * path of projectile, , displays in terminal.  *  **/ void simulate(double angle, double velocity){     cout << "entering simulation" << endl;      double time = 0;     double x = 1;     double y = 1;     double veloup = 0;     double velofo = 0;      veloup = (velocity*sin(angle));     velofo = (velocity*cos(angle));     cout << "angle in radians: " << angle << endl;     cout << "initial velocity upwards (fps): " << veloup << endl;     cout << "initial velocity forward (fps): " << velofo << endl;      while(y >= ground){         x = velofo * time;         y = gravity_factor*(time*time) + (veloup * time);         cout << "(x, y) @ time " << time << " (" << x << ", " << y << ")" <<  endl;         ++time;     } //while     cout << "leaving simulation" << endl; } //simulate  /***************************************************************************  * main function.  *  * produces output console in order coach user on input. **/ int main() {     cout << "execution beginning" << endl;     cout << "enter inital velocity (feet per second):" << endl;     cin >> initialvelocity;      if(initialvelocity > 0){         cout << "good. " << initialvelocity << " valid value initial velocity." << endl;     }     else{         cout << "error: " << initialvelocity << " not valid value initial velocity." <<endl;         return 0;     }      cout << "enter initial angle in degrees (from horizontal):" << endl;     cin >> initialangle;      if(initialangle >= 0 && initialangle <= 90){         cout << "good. " << initialangle << " valid value initial angle." << endl;     }     else{         cout << "error: " << initialangle << " not valid value initial angle." << endl;         return 0;     }      simulate(degtorad(initialangle), initialvelocity);      cout << "ending execution" << endl; return 0; } 

like said, new c++, can please explain how these 2 interact, or should make them interact more efficiently. program compiles, , runs correctly, unclear on protocol , uses of header files .cpp file. also, functions , variables should in private section of header, , should go in public? thank you.

i think need know 2 things:

  • whats difference between declaration , definition.
    • you need declaration use class or function (for compiler know there)
    • the definition code gets compiled (function body, initialization)
  • what #include do.
    • #include copies content of header source file including it.

that means, don’t want definitions in header files, because if multiple files include header, violate one-definition-rule, causing multiple-defintion-errors during linking stage.

which variables should declare, , should not in header file?

the header-file interface other source-files want call functions/use classes.
put in declarations things needed other source files work.

when declare variable in header file, should use extern?

the extern keyword specifies declaration variable rather definition. means variable-definition somewhere else (e.g. source file).

if want use gravity_factor in source file, includes header:

  • in source: const int gravity_factor = -16;
  • in hour header: extern const int gravity_factor;

remarks:

  • what have in header file class definition
    • public , private accessors class’ members
    • you may rather want namespace here or plain functions
    • the main function has nothing inside class definition

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -