Is it possible to create all of you classes in a header file (i think I saw somebody do that) instead of doing it in the .cpp file? If so, could you please demonstrate?
Thanks
-Chris
Printable View
Is it possible to create all of you classes in a header file (i think I saw somebody do that) instead of doing it in the .cpp file? If so, could you please demonstrate?
Thanks
-Chris
It's not only possible, but extremely recommendable. The class declaration is only a model for a compiler. It doesn't have any code as you can't put any code in header, except inlined code, but that's an another case. So:
You can use the class in any .cpp file you #include it as it'd be defined there. There is a short example, I hope it helped.Code:// In Myclass.h:
class MyClass
{
public:
MyClass();
~MyClass();
// function declarations and whatever...
private:
// variables and functions and whatever...
};
// In any .cpp file:
#include "myclass.h"
int WhatEverFunc(void)
{
MyClass MyObject;
// yada yada...
return whatever;
}
Thanks heaps, that should prove useful. But I just noticed that in your constructor and destructor, you didn't actually writing them you just called them...i'll explain:
you wrote:
public:
MyClass();
~MyClass();
I thought it was meant to be:
public:
MyClass() // constructor
{
//stuff like num1 = 0; etc...
}
~MyClass
{
}
What's going on here...?
Oh yeah, I forgot the major point, sorry :D
You must have a MyClass.cpp file where you write your classes code.
And you must compile this .cpp file also. Usually compilers do it automatically and link it to your project.Code:// in Myclass.cpp
#include "myclass.h"
// constructors and destructor
MyClass::MyClass()
{
// code here
}
MyClass::~MyClass()
{
// code here
}
// function bodies
void MyClass::AnyFunc(int Variable)
{
// code here
}
so what you wrote in your previous post needs this extra cpp file in order to work?
Basicly... yes.
You could write all the code in the header, but then it'd be all inline code. If you have the code in .cpp file the code will be generated once. The code you have in header will be generated every time you call it, so if you call a function which code is in header you'll have it's code multiple times in .exe file. This can increase the size of your .exe file very much.
It must not be the MyClass.cpp file. The name doesn't make difference, or you could put the code in .cpp file you have other code, but it wouldn't make the code easy to read and debug. So it's recommendable to have a .cpp file same name as the .h/.hpp file.
you have created the constructors in the cpp file, can this be done in the .h file like I have done?
You can write all of the code for one class in the header. You don't need a .cpp file if you don't want to, but it's the normal way.
Code:// clas.h
#include <stdio.h>
class clas
{
public:
clas( int x )
{
i = x;
}
print()
{
printf( "%d",i );
}
private:
int i;
};
// main cpp
#include "clas.h"
int main()
{
clas c(5);
c.print();
return 0;
}
Yes, but then the constructor will be inlined code. If the constructor is long, then it'll be better to declare it in .cpp file.
yeah, doing it all the the cpp file seems like it may prevent some problems along the line. Thanks for explaining it all to me :)