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

header file help

P: n/a
Hello

I'm learning c++ and it is clear to me how to add information to header
file about functions. But what about class and class functuons?

If i have:
1. main.cpp
#include "myHeader.h"

int main(){
myClass test;
test.nr=1;
test.printnr();

return 0;
}

2. myClass-source.cpp
#include other stuff...
#include "myHeader.h"

using namespace std;

class myClass {
Public:
int nr;

void printnr(){
cout<<this.nr<<endl;
}
}
3. myHeader.h
?????????
myClass::void printnr(); --I guess ...
but what about class file?
So my questions are:
a) How myHeader.h file should look like that it worked?
b) Because i come from Java - is it correct way to make instance of
class, or i should call a "new" operator like that:
myClass test=new myClass();
c) What if myClass has a constructor - what should be included to header
file?
Thanks,
Juhan.
Aug 1 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Juhan Voolaid wrote:
Hello

I'm learning c++ and it is clear to me how to add information to header
file about functions. But what about class and class functuons?

If i have:
1. main.cpp
#include "myHeader.h"

int main(){
myClass test;
test.nr=1;
test.printnr();

return 0;
}
This is ok.

2. myClass-source.cpp
#include other stuff...
#include "myHeader.h"

using namespace std;

class myClass {
Public:
int nr;

void printnr(){
cout<<this.nr<<endl;
}
}
3. myHeader.h
?????????
myClass::void printnr(); --I guess ...
but what about class file?
You've got this the other way around. The header file should contain
the definition of the class. The source file should have the defintion
of the methods.

(You can have the definition of the methods inside the header file as
well, but it means something else in terms of linkage.)
b) Because i come from Java - is it correct way to make instance of
class, or i should call a "new" operator like that:
myClass test=new myClass();

Nope, the way you had it in "main" is fine.
c) What if myClass has a constructor - what should be included to header
file?


You need to declare the constructor in the header file. You can define
the body of the constructor in the header file (inline) or in the CPP
file.

Hope this helps,
-shez-

Aug 1 '05 #2

P: n/a
Juhan Voolaid wrote:
Hello

I'm learning c++ and it is clear to me how to add information to header
file about functions. But what about class and class functuons?

If i have:
1. main.cpp
#include "myHeader.h"

int main(){
myClass test;
test.nr=1;
test.printnr();

return 0;
}

2. myClass-source.cpp
#include other stuff...
#include "myHeader.h"

using namespace std;

class myClass {
Public:
Seems your word processor made the p caps.
int nr;

void printnr(){
cout<<this.nr<<endl;
}
}
3. myHeader.h
?????????
myClass::void printnr(); --I guess ...
but what about class file?
So my questions are:
a) How myHeader.h file should look like that it worked?
I would rather have my class decl. inside the myHeader.h file.
b) Because i come from Java - is it correct way to make instance of
class, or i should call a "new" operator like that:
myClass test=new myClass();
You can create objects using the following 2 methods:

1. myClass obj1;
2. myClass *obj2;
obj2 = new myClass;

One of them allocates memory from the stack and the other one from the
heap.
Ofcourse for case 2, also make a call to delete at the end of main().
c) What if myClass has a constructor - what should be included to header
file?
Just have the constructor decl. inside the class in myHeader.h and have
its definition in .cpp file.


Thanks,
Juhan.


Aug 1 '05 #3

P: n/a
Juhan Voolaid wrote:
Hello

I'm learning c++ and it is clear to me how to add information to header
file about functions. But what about class and class functuons?
// c.h

class C
{
public:
C();
void f();
};

// c.cpp

# include "c.h"
# include <iostream>

C::C()
{
std::cout << "ctor";
}

void C::f()
{
std::cout << "f()";
}

// main.cpp
# include "c.h"

int main()
{
C c; // outputs "ctor"
c.f(); // outpus "f()"
}
So my questions are:
a) How myHeader.h file should look like that it worked?
See above.
b) Because i come from Java - is it correct way to make instance of
class, or i should call a "new" operator like that:
myClass test=new myClass();


There is no "correct" way, both are valid from a language point of
view, as long as you delete what you create:

int main()
{
C *c = new C;
} // ouch! c is not deleted

int main()
{
C *c = new C;

delete c// ah!
}

However, C++ objects are usually on the stack:

int main()
{
C c;
}

which is quite simpler and more natural, unless you need to use dynamic
memory. There are various reasons for that, read it in your favorite
textbook.
Jonathan

Aug 1 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.