Clean Coding C++
Hi all,
I''ve been learning C++ for some time now, but i haven''t
understood how to correctly store code.
I know that in Pascal, we have units, diferent files
with code in them, that the main code can call.
How do I, in C++, create a diferent file with source code,
that I can call from withing my main code file?
I think this should be pretty easy,
thanx all,
Hugo Ferreira
lotsjunk@hotmail.com
http://unitek3000.tripod.com
Hello there. I''ll explain this with a simple example using gcc. It is even simpler to do using Microsoft Visual C++. Okay, so lets pretend we have three files:
[File Player.h]
#include
struct Player
{
public:
int iX;
int iY;
char *szTexture;
char *szName;
...
};
[File Player.cpp]
#include "Player.h"
Player:
layer(void)
{}
...
[File Main.cpp]
#include "Player.h"
int main(void)
{
Player Instance;
return 0;
}
At the command line just type gcc Main.cpp Player.cpp. This will combine the files and compile them to produce an executable.
[File Player.h]
#include
struct Player
{
public:
int iX;
int iY;
char *szTexture;
char *szName;
...
};
[File Player.cpp]
#include "Player.h"
Player:
![](tongue.gif)
{}
...
[File Main.cpp]
#include "Player.h"
int main(void)
{
Player Instance;
return 0;
}
At the command line just type gcc Main.cpp Player.cpp. This will combine the files and compile them to produce an executable.
"The time has come", the Walrus said, "To speak of many things."
Oh, and if you want to share functions and code between things you should put the declarations for functions and structures in a header file, then define them in a source code file. This seperates the interface from the implementation, and that way you could just ship the header file with object code instead of the source file.
"The time has come", the Walrus said, "To speak of many things."
May 26, 2001 12:07 PM
put your class declarations in header files (.h) and use #include "headerfile.h" in any other files which need access.
if you''re not using classes but using c style code, you can use the extern keyword to declare any functions or globals which are external to the current source file.
the anonymous guy
if you''re not using classes but using c style code, you can use the extern keyword to declare any functions or globals which are external to the current source file.
the anonymous guy
The quick-and-dirty paradigm is to have every .cpp file contain the implementation of a single class, and every .h file have the definition for that class.
A more involved definition comes from Lakos "Large-Scale C++ Software Design", where a project is broken down into "components". A component is a single .h and single .cpp file, but they can contain multiple classes _if appropriate_. For instance, if you''re writing a linked list class List with a helper class Node, they could both go into the same component. He alternatively defines a component as "the smallest individually testable module".
A more involved definition comes from Lakos "Large-Scale C++ Software Design", where a project is broken down into "components". A component is a single .h and single .cpp file, but they can contain multiple classes _if appropriate_. For instance, if you''re writing a linked list class List with a helper class Node, they could both go into the same component. He alternatively defines a component as "the smallest individually testable module".
its quite simple ..
yup yup .. C++ is easy once u get the hang of it.
btw, VC++ implementation of struct is quite similar to classes. some may say its even the same..
{ Stating the obvious never helped any situation !! }
Edited by - jwalker on May 26, 2001 7:55:15 PM
|
yup yup .. C++ is easy once u get the hang of it.
btw, VC++ implementation of struct is quite similar to classes. some may say its even the same..
{ Stating the obvious never helped any situation !! }
Edited by - jwalker on May 26, 2001 7:55:15 PM
May 26, 2001 07:17 PM
quote:
jwalker wrote:
VC++ implementation of struct is quite similar to classes. some may say its even the same.
In C++ (not just VC++) a struct is identical to a class, with one small exception: In a struct the default access-modifier is "public", while in a class the default is "private".
But other than that they are exactly alike.
I actually wish that this wasn''t the case. It would be much clearer if structs could only contain data members.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement