PDA

Click to See Complete Forum and Search --> : Headers and CPP files


jmiller
Jan 7th, 2003, 09:16 AM
Can someone explain to me what code goes in cpp files, and what code is supposed to go in header files?
thanks

CornedBee
Jan 7th, 2003, 11:37 AM
The header files are for declarations. Declaration of all things you want shared among modules (cpp files) to be exact.

The cpp files are where the actual code goes.

Like this:

// Declarations, go into headers (.h)
void func(int i1, int i2);
int func2();
class cl
{
int m1, m2;
public:
cl();
void method(int i);
int inline_method() {
return m1 * m2;
}
};
extern int g_i;

// Definitions, go into modules (.cpp)
void func(int i1, int i2)
{
cl obj;
obj.method(i1);
cout << i2 << ':' << obj.inline_method() << endl;
}

int func2()
{
return g_i;
}

cl::cl()
{
m1 = m2 = 0;
}

void cl::method(int i)
{
m1 = i / 5;
m2 = i % 5;
}

// Even though this looks like a declaration
// it's actually a definition.
int g_i;


int inline_method() {
return m1 * m2;
}
This is actually a definition, but inline functions are an exception to the rule. They always go into the headers.

jmiller
Jan 7th, 2003, 05:25 PM
i'm still a bit confused. say i have three cpp files--main, classes, and functions. main needs to be able to recognize references to classes and functions, and functions to classes and classes to functions. Should I put all the prototypes in one header and then include that header in each cpp file? How should i work this?
thanks again

FunyBunyFartAHH
Jan 7th, 2003, 09:02 PM
You declare your functions in your header files, then call them in your .cpp files. Not too hard....

Ok if ur really lost, just follow along with me.

Make a header file and typ the following (this is VC++):

double Square (double x);

double Cube (double x);

ok now make a .cpp file and type this:

#include "Your Header File's Name.h"

double Square (double Value)
{
double SquareReturn;

SquareReturn = Value * Value;

return SquareReturn;

double Cube (double Value)
{
double CubeReturn;
CubeReturn = Value * Value * Value;

return CubeReturn;
}


Ok and make one more .cpp, make this the main:

#include "Your Header's Name.h"
#include <iostream>

main ()
{
double Number;
double SquaredNumber, CubedNumber;

Number = 5;

SquaredNumber = Square (Number);
CubedNumber = Cube (Number);

ard::cout << "Sqaure of 5 = " << SqauredNumber >> std::endl;
srd::cout << "Cube of 5 = " << CubedNumber << std::endl;

return 0;
}


Ok now you used a header and 2 cpp files to create a more efficient, easier to read program that tells you the square of 5 and the cube of 5. I hope this helped!!!!!!

This may have some spelling errors, the VC++ complier is very fussy about spelling, so this is just eye candy, not neceserally a thing to try out, its just to let u understand the concepts.

CornedBee
Jan 8th, 2003, 03:20 AM
But you can also have more than one header. It's common to have one header per class, one (or more in large projects) for functions and one for independent constants and global variables.
Global variables are to be avoided btw...

jmiller
Jan 9th, 2003, 04:10 AM
I tried moving my class declarations into a .h, and my class definitions into a .cpp, but i still have errors, and i'm not sure why. if could you could please look at the project and tell what is the best way to arrange to the code i have, i would much appreciate it.
thanks

CornedBee
Jan 9th, 2003, 10:35 AM
You had a few case errors (speed instead of Speed) and missed to include necessary header files.

And you used a strange string class that I can not approve of (besides I don't have it, so I had to remove it to test this for me).
I replaced the old headers with the new ones and your string class with the C++ standard string class.

This project compiles and runs very well, exept that the race itself runs too fast.

jmiller
Jan 10th, 2003, 12:26 PM
CornedBee,
thanks for the help.
the string class is the one that the AP College Board approves for the AP C++ exam in May, and since I'm taking it, i need to be familiar with using it. I just forgot to include it. The reason for the fast races is becase i was doing some probability testing, and i was tired of waiting. But thanks again for the help!
jmiller

CornedBee
Jan 11th, 2003, 07:47 AM
I wonder why AP approves of apstring, this strange String of yours but not the Standard string. It's the best of them and the only that has any application outside of programming courses.
Curse AP!