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,
3 1661
One more thing:
can i leave the constructor with implementation in the headerfile ?
Tommy,
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 -
#ifndef VECTOR3_H_INCLUDED
-
#define VECTOR3_H_INCLUDED
-
-
#include <string>
-
#include <math.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <vector>
-
#include <iostream>
-
#include <fstream>
-
-
class vector3{
-
-
public:
-
float x,y,z;
-
-
vector3(float x1,float y1,float z1);
-
-
vector3(){};
-
-
vector3 add(vector3 v1, vector3 v2);
-
-
vector3 set(float x1,float y1,float z1);
-
-
vector3 sub(vector3 v1, vector3 v2);
-
-
vector3 mul(vector3 v1, vector3 v2);
-
-
vector3 mulSkalar(vector3 v1, float n);
-
-
vector3 cross(vector3 v1, vector3 v2);
-
-
vector3 normalize(vector3 v1);
-
-
float length(vector3 v1);
-
-
-
-
};
-
and this is the .cpp file -
#include "vector3.h"
-
-
using namespace std;
-
-
-
vector3::vector3(float x1,float y1,float z1){
-
-
x=x1;
-
y=y1;
-
z=z1;
-
-
-
}
-
-
vector3 vector3:: add(vector3 v1, vector3 v2){
-
vector3 temp;
-
temp.x=v1.x+v2.x;
-
temp.y=v1.y+v2.y;
-
temp.z=v1.z+v2.z;
-
-
return temp;
-
}
-
-
vector3 vector3:: set(float x1,float y1,float z1){
-
vector3 temp;
-
temp.x=x1;
-
temp.y=y1;
-
temp.z=z1;
-
-
return temp;
-
}
-
-
-
vector3 vector3:: sub(vector3 v1, vector3 v2){
-
vector3 temp;
-
temp.x=v1.x-v2.x;
-
temp.y=v1.y-v2.y;
-
temp.z=v1.z-v2.z;
-
-
return temp;
-
}
-
-
vector3 vector3::mul(vector3 v1, vector3 v2){
-
vector3 temp;
-
temp.x=v1.x*v2.x;
-
temp.y=v1.y*v2.y;
-
temp.z=v1.z*v2.z;
-
-
return temp;
-
}
-
-
vector3 vector3:: mulSkalar(vector3 v1, float n){
-
vector3 temp;
-
temp.x=v1.x*n;
-
temp.y=v1.y*n;
-
temp.z=v1.z*n;
-
-
return temp;
-
}
-
-
-
-
vector3 vector3:: normalize(vector3 v1){
-
vector3 temp;
-
float l=length(v1);
-
temp.x=v1.x/l;
-
temp.y=v1.y/l;
-
temp.z=v1.z/l;
-
return temp;
-
}
-
-
float vector3:: length(vector3 v1){
-
return sqrt(v1.x*v1.x+v1.y*v1.y+v1.z+v1.z);
-
}
-
-
vector3 vector3::cross(vector3 v1, vector3 v2){
-
vector3 temp;
-
temp.x=v1.y*v2.z-v1.z*v2.y;
-
temp.y=v1.z*v2.x-v1.x*v2.z;
-
temp.z=v1.x*v2.y-v1.y*v2.x;
-
return temp;
-
-
}
-
any comments ?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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;
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |