C++头文件交互

C++ Header File Interaction

本文关键字:交互 文件 C++      更新时间:2023-10-16

好吧,所以我对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文件的使用。此外,哪些函数和变量应该在标头的私有部分,哪些应该在公共部分?非常感谢。

我想你需要知道两件事:

  • 声明和定义有什么区别。
    • 你需要一个声明来使用一个类或函数(让编译器知道它在那里)
    • 定义是要编译的代码(函数体、初始化)
  • #include做什么。
    • #include只是将头的内容复制到包含它的源文件中

这意味着,您可能不希望在头文件中包含定义,因为如果多个文件包含您的头,则会违反"一个定义规则",从而在链接阶段导致多个定义错误。

哪些变量应该声明,哪些不应该在头文件中声明?

头文件是您与其他源文件的接口,这些源文件希望调用您的函数/使用您的类
因此,您为其他源文件工作所需的内容放入声明。

在头文件中声明变量时,是否应该使用extern

extern关键字指定变量的声明,而不是定义。这意味着您的变量定义在其他地方(例如源文件)。

如果你想在另一个源文件中使用GRAVITY_FACTOR,其中包括你的头:

  • 来源:const int GRAVITY_FACTOR = -16;
  • 小时标题:extern const int GRAVITY_FACTOR;

备注:

  • 当前头文件中有一个类定义
    • public和private是类成员的访问器
    • 您可能更希望这里有一个名称空间或纯函数
    • main函数与类定义内部无关

在头文件中,您可以声明所有类字段和函数,这些字段和函数将在cpp文件中实现。你不应该声明将在类函数中使用的变量(因为有类似的临时变量),但你可以声明(也应该声明)将在程序中使用的常量变量。

你不应该使用extern,extern its用于你没有定义它们的变量,编译器也不会为此打扰你,因为当它是extern时,这意味着变量是在你的程序中的其他地方定义的。extern还用于处理c++代码的函数,就像在c中一样。类似:

外部"C"{//code.}

extern "C" int func(int a);

总结-.h文件中的delcare常量变量,以及cpp中的classes.else-(除非是内联的)。在您的情况下:

const int GRAVITY_FACTOR = -16;
const int GROUND = 0;
const double PI = atan(1.0)*4;
are better to be in the .h file

+你不应该再申报:

double initialVelocity;
double initialAngle;

它已经在.h文件中了。

希望它有用。。