473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

header file help

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
3 2147

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Kemmerich | last post by:
hi, i'm looking for a possibility to start a download. here is my current code: header("Content-Type: $_contenttype"); header("Content-Disposition: attachment; filename=\"$_filename\"");...
11
by: DaRemedy | last post by:
Hiya, just need help with PHP headers. I have an index php page which has the following code within a header redirect: <?php if ( empty($_GET) ) if (empty($_GET) ) { $month = date(n);
0
by: Luke Airig | last post by:
I am using the Saxon engine and I have an xml file that contains batches of records. Each batch starts with a header record and the associated detail records immediately follow each header. There...
16
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...
16
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. ...
6
by: alan | last post by:
Dear all, I have written my own function by C. And my development platform is W2k with VC6.0. Then I also defined a header file to extern declare this function. After that, I include this...
3
by: bill | last post by:
I firmly believe that it is always a bad idea to put code in a header file. Nothing pisses me off more than seeing function definitions in a ..h, and I recently was truly blessed :) to witness...
16
by: wdh3rd | last post by:
Hi everyone. I'm new to C and I have a few questions: I am making files for permutations and combinations. Files to be made are perm.c, perm.h, combo.c, and combo.h. Since both combinations...
1
by: Shalako | last post by:
I check my error log and see these entries: malformed header from script. Bad header= Missing gauge reports are ind: padata.pl /perl/pema/padata.pl did not send an HTTP header malformed...
1
by: Proogeren | last post by:
I have a problem with a httpwebrequest that I am creating. The request in itself looks correct but using fiddler I see that a www-authentication header is sent along as well. The code is pasted...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.