Ok файл, так что я довольно новыми для C++, у меня есть несколько вопросов о заголовочных файлах ...Заголовок 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-файлом. Кроме того, какие функции и переменные должны находиться в закрытом разделе заголовка и которые должны публиковаться? Спасибо.
Интересное имя «конструктора» у вас есть. (это не конструктор btw) – WhozCraig
@WhozCraig это пугает меня ** Обратите внимание: вы никогда не создаете объект класса 'main'. в чем смысл этого класса? вы также не выполняете какие-либо функции в вашем 'class main' –
@Trevor. Пожалуйста, измените это 'm' в 'int main(); // Конструктор 'и избавиться от int –