By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

header files

P: 12
Hi,

I have a bad habit including .cpp files in other .cpp files. Up to a point its possible. However, now i have met the wall, and i am a newbie in the c++ world.
My question is : what do i put in the .h file ?

First, the inclusion guard, right ? Looks like this when the filename is polygonstruct?


#ifndef POLYGONSTRUCT_H_INCLUDED
#define POLYGONSTRUCT_H_INCLUDED

#endif

Prototypes of functions and global variables , right ?
What about structs ?
The class definition , right ? and the class implementation in .cpp ?

Where do namespaces go ?

Please anybody claify this.

If someone find this question silly, then i can tell that i have googled the theme but really not found the answer to all my questions.


Tommy,
Sep 27 '06 #1
Share this Question
Share on Google+
3 Replies


P: 12
One more thing:

can i leave the constructor with implementation in the headerfile ?

Tommy,
Sep 27 '06 #2

P: 12
he he, now i feel irritating but i am only trying to learn:

Here i have made a .cpp file to a .cpp and a .h file

.h file

Expand|Select|Wrap|Line Numbers
  1. #ifndef VECTOR3_H_INCLUDED
  2. #define VECTOR3_H_INCLUDED
  3.  
  4. #include <string>
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <vector>
  9. #include <iostream>
  10. #include <fstream>
  11.  
  12. class vector3{
  13.  
  14. public:
  15.     float x,y,z;
  16.  
  17.     vector3(float x1,float y1,float z1);
  18.  
  19.     vector3(){};
  20.  
  21.     vector3 add(vector3 v1, vector3 v2);
  22.  
  23.     vector3 set(float x1,float y1,float z1);
  24.  
  25.     vector3 sub(vector3 v1, vector3 v2);
  26.  
  27.     vector3 mul(vector3 v1, vector3 v2);
  28.  
  29.     vector3 mulSkalar(vector3 v1, float n);
  30.  
  31.     vector3 cross(vector3 v1, vector3 v2);
  32.  
  33.     vector3 normalize(vector3 v1);
  34.  
  35.     float length(vector3 v1);
  36.  
  37.  
  38.  
  39. };
  40.  
and this is the .cpp file

Expand|Select|Wrap|Line Numbers
  1. #include "vector3.h"
  2.  
  3. using namespace std;
  4.  
  5.  
  6. vector3::vector3(float x1,float y1,float z1){
  7.  
  8.         x=x1;
  9.         y=y1;
  10.         z=z1;
  11.  
  12.  
  13. }
  14.  
  15. vector3    vector3:: add(vector3 v1, vector3 v2){
  16.         vector3 temp;
  17.         temp.x=v1.x+v2.x;
  18.         temp.y=v1.y+v2.y;
  19.         temp.z=v1.z+v2.z;
  20.  
  21.         return temp;
  22.     }
  23.  
  24. vector3    vector3:: set(float x1,float y1,float z1){
  25.         vector3 temp;
  26.         temp.x=x1;
  27.         temp.y=y1;
  28.         temp.z=z1;
  29.  
  30.         return temp;
  31.     }
  32.  
  33.  
  34. vector3    vector3:: sub(vector3 v1, vector3 v2){
  35.         vector3 temp;
  36.         temp.x=v1.x-v2.x;
  37.         temp.y=v1.y-v2.y;
  38.         temp.z=v1.z-v2.z;
  39.  
  40.         return temp;
  41.     }
  42.  
  43. vector3    vector3::mul(vector3 v1, vector3 v2){
  44.         vector3 temp;
  45.         temp.x=v1.x*v2.x;
  46.         temp.y=v1.y*v2.y;
  47.         temp.z=v1.z*v2.z;
  48.  
  49.         return temp;
  50.     }
  51.  
  52. vector3    vector3:: mulSkalar(vector3 v1, float n){
  53.         vector3 temp;
  54.         temp.x=v1.x*n;
  55.         temp.y=v1.y*n;
  56.         temp.z=v1.z*n;
  57.  
  58.         return temp;
  59.     }
  60.  
  61.  
  62.  
  63. vector3    vector3:: normalize(vector3 v1){
  64.         vector3 temp;
  65.         float l=length(v1);    
  66.         temp.x=v1.x/l;
  67.         temp.y=v1.y/l;
  68.         temp.z=v1.z/l;
  69.         return temp;
  70.     }
  71.  
  72. float    vector3:: length(vector3 v1){
  73.         return sqrt(v1.x*v1.x+v1.y*v1.y+v1.z+v1.z);
  74.     }
  75.  
  76. vector3 vector3::cross(vector3 v1, vector3 v2){
  77.         vector3 temp;
  78.         temp.x=v1.y*v2.z-v1.z*v2.y;
  79.         temp.y=v1.z*v2.x-v1.x*v2.z;
  80.         temp.z=v1.x*v2.y-v1.y*v2.x;
  81.         return temp;
  82.  
  83.         }
  84.  
any comments ?
Sep 27 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Headers should/can contain

structure definitions
class definitions
data declarations
function declarations

it is also OK to included the implementation of any small class member functions, particularly those you wish to be inline

I header file should NOT contain

data definitions
function definitions

There are 2 schools of thought on including header files into header files, they are

1. It is OK to include header files into header files
2. You should not included header files into header files

Choose which ever one you want, 1 makes it easier to get the code compiling (as you don't have to worry about have the correct headers in the correct order in you CPP files so much) but can slow down compilation fractionally (because of including more stuff).

What you wrote looked OK to me.
Sep 28 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.