473,513 Members | 2,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

header files

12 New Member
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
3 1661
stromhau
12 New Member
One more thing:

can i leave the constructor with implementation in the headerfile ?

Tommy,
Sep 27 '06 #2
stromhau
12 New Member
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
9,065 Recognized Expert Moderator Expert
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

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

Similar topics

3
2207
by: Chris Mantoulidis | last post by:
Seperate compilation (that's what it's called, right?) seems to be quite popular, so I decided to get some info about it, and (d'oh) use it... But it's whole structure seems weird to me... ...
21
2571
by: Hattuari | last post by:
I'm learning C++ after having spent several years in the computer industry doing both system administration and engineering. I've written code in Perl, Bash, Pascal, Ada, C, Mathematica (hundreds...
16
12504
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it...
11
2732
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
3
3078
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by...
11
5550
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how...
18
2723
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
8
2752
by: ginnisharma1 | last post by:
Hi All, I am very new to C language and I got really big assignment in my work.I am wondering if anyone can help me.........I need to port compiler from unix to windows and compiler is written...
9
4029
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
36
3786
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.