473,545 Members | 666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2159

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
3192
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\""); // flush();
11
2286
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
1546
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 can be multiple batches in a single file, each batch indicated by a new header. I need to create a tab-delimited output file that appends all of...
16
12508
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 because it is, as the authors love to say, "implementation specific". If that's the case, how does the compiler commonly handle them? I use Linux and...
16
12583
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. I have tried and tried but just cant seem to get this right, I am using Queries to created my export files with specifications which works fine, I...
6
7052
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 header file. The function is stored in C:\temp\myfun.c int func(){ return 1;
3
1757
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 code that actually contained #include "foo.c" in a header. However, I am eliciting responses to the thought of putting assertions in a header file. I...
16
2065
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 and permutations use factorial to solve their problems, factorial is supposed to be in both perm.c and combo.c, and declared private. (1) You...
1
24114
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 header from script. Bad header= : padata.pl /perl/pema/padata.pl did not send an HTTP header malformed header from script. Bad header= Missing...
1
7993
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 below. I do not add any www-authentication header here so I was wondering if anyone knows how to remove it. I have used almost 2 days trying to figure...
0
7467
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5326
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
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 we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.