好的,所以我对 C++ 很陌生,我有几个关于头文件的问题......
1.) 您应该声明哪些变量,哪些不应该在头文件中声明?
2.) 当你在头文件中声明一个变量时,你应该使用 extern 吗?
这是我的头文件:
#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
这是我的 Main.cpp
/*******************************************************************
* This program will take input for initial velocity (fps), and a launch angle
* based on this information, the current posotion of the object thrown will be
* calculated until it hits the 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.
*
* This function takes in an angle in degrees, and converts it to
* radians.
*
**/
double degToRad(double angle){
return angle * (PI/180);
}
/******************************************************************
* simulate function.
*
* Takes in the angle in radians, and the velocity. Calculates the
* path of the projectile, and displays it in the 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) at time " << time << " is (" << x << ", " << y << ")" << endl;
++time;
} //while
cout << "Leaving Simulation" << endl;
} //simulate
/***************************************************************************
* The main function.
*
* Produces output to the console in order to coach the user on what to input.
**/
int main()
{
cout << "Execution Beginning" << endl;
cout << "Enter the inital velocity (feet per second):" << endl;
cin >> initialVelocity;
if(initialVelocity > 0){
cout << "Good. " << initialVelocity << " is a valid value for the initial velocity." << endl;
}
else{
cout << "ERROR: " << initialVelocity << " is not a valid value for the initial velocity." <<endl;
return 0;
}
cout << "Enter the initial angle in degrees (from the horizontal):" << endl;
cin >> initialAngle;
if(initialAngle >= 0 && initialAngle <= 90){
cout << "Good. " << initialAngle << " is a valid value for the initial angle." << endl;
}
else{
cout << "ERROR: " << initialAngle << " is not a valid value for the initial angle." << endl;
return 0;
}
simulate(degToRad(initialAngle), initialVelocity);
cout << "Ending Execution" << endl;
return 0;
}
就像我说的,我是 C++ 新手,谁能解释一下这两者是如何交互的,或者我应该怎么做才能让它们更有效地交互。该程序编译并正确运行,但我不清楚协议和头文件与 .cpp 文件的使用。此外,哪些函数和变量应该在标题的私有部分,哪些应该在公共部分?谢谢你。