473,770 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help me about Header Files

Hi All,

i am new to header files , and have a basic question ,
i have made my program in three files namely,

idr_bpl.cpp( contains class definitions and function definitions)
bpl.cpp( contains main() function)
bpl.h (contains variable definition of a variable that is shared in
the above two files )

right now i have included "idr_bpl.cp p", "idr.h" and other standard
files (like iostream etc........) in bpl.cpp and

"idr.h" and other standard files in idr_bpl.cpp

now, as suggested by one google friend that it is not a good practice
to include a cpp file in another cpp file , i want to make header
files for separating the definitions and implementation,
the point to be noted is that there are 2 classes(namely body and
header) defined in idr_bpl.cpp and the second class's(header' s) one
function uses the object of the other class(body).

can anyone help me arranging my code in proper header files, since i
will have to make several such programs in future.

Thanks
Dharmesh
Jul 19 '05 #1
3 5133

"dharmesh Gupta" <dh************ @esteltelecom.c om> wrote in message
news:45******** *************** ***@posting.goo gle.com...
Hi All,

i am new to header files , and have a basic question ,
i have made my program in three files namely,

idr_bpl.cpp( contains class definitions and function definitions)
bpl.cpp( contains main() function)
bpl.h (contains variable definition of a variable that is shared in
the above two files )

right now i have included "idr_bpl.cp p", "idr.h" and other standard
files (like iostream etc........) in bpl.cpp and

"idr.h" and other standard files in idr_bpl.cpp

now, as suggested by one google friend that it is not a good practice
to include a cpp file in another cpp file ,
Right.
i want to make header
files for separating the definitions and implementation,
the point to be noted is that there are 2 classes(namely body and
header) defined in idr_bpl.cpp and the second class's(header' s) one
function uses the object of the other class(body).

can anyone help me arranging my code in proper header files, since i
will have to make several such programs in future.

Thanks
Dharmesh


Just put the classes in the header file, but put the class functions in the
cpp file.

// idr.h

class X
{
public:
void a_func();
};

class Y
{
public:
void b_func();
};

// idr_bpl.cpp

void X::a_func()
{
Y y;
y.b_func();
}

void Y::b_func()
{
}

john
Jul 19 '05 #2
>right now i have included "idr_bpl.cp p", "idr.h" and other standard
files (like iostream etc........) in bpl.cpp and


Bad idea. cpp files are implementation files, which are not meant to
be included.

The whole idea behind this is quite simple. You must understand the
diference between a compiler and a linker. There are very good books
out there, check out www.accu.org.

But let's put a simple example :

int main()
{

}

Quite simple. Now you have function which you want to use :

void f()
{
std::cout << "this is my function";
}

So you put it before main and you call it :

void f()
{
std::cout << "this is my function";
}

int main()
{
f();
}

Now, your project is getting larger and you want to seperate the
main() from other functions. You keep main() in your original file
(let's call it main.cpp) and you put your function in a new one (let's
call it function.cpp). Your main.cpp now won't even compile, since
you put f() in another file. You have to tell your compiler that f()
exists, but somewhere else :

// main.cpp

void f();

int main()
{
f();
}

And that's it. The compiler parses both files and finds no errors.
Then the linker kicks in, puts the two files together and the call to
f() in main() is 'linked' to the f() in function.cpp.

But now, let's get on with a new project :

// myclass.h

class MyClass
{
private:
int some_stuff;

public:
MyClass(int s) : some_stuff(s)
{
}

int stuff()
{
return some_stuff;
}
};

This is your class and it is in a header file ("myclass.h" ). This
header is meant to be included by any file which uses this class :

// main.cpp

# include "myclass.h"

int main()
{
MyClass my_object(5);
}

But now, imagine you have some twenty different files which are all
including "myclass.h" . Everything goes pretty well until you decide
the constructor must validate the int before putting it in
'some_stuff'. You change some code and you recompile. Since the
header changed, all of your twenty files must be recompiled too.

This is the reason for which you have to separate the class definition
and the class' implementation. In your header file, you will only put
the function's declarations :

// myclass.h

class MyClass
{
private:
int some_stuff;

public:
MyClass(int s);

int stuff();
};

That's it. Now your twenty files are quite happy with that since they
only need declarations, not definitions of member functions.

And since you have to put the implementation somewhere, you get
another file which you call "myclass.cp p" which will contain the
implementations :

// myclass.cpp

# include "myclass.h"

MyClass::MyClas s(int s) : some_stuff(s)
{
}

int MyClass::stuff( )
{
return some_stuff;
}
So if you have to change something in the code, the only file which
will have to get recompiled is "myclass.cp p". Of course, if you want
to add another member function (such as

void stuff (int new_stuff);

), you will change the header file and the whole thing will recompile.
But that is normal.
So that is the deal : class definitions go in .h files and class
implementation go in .cpp files. You include the .h file when you
need that class, but you never include the .cpp file.

And usually, you will have no more than a couple of classes in a
header file.
Jonathan

Jul 19 '05 #3
dh************@ esteltelecom.co m (dharmesh Gupta) wrote in message news:<45******* *************** ****@posting.go ogle.com>...

can anyone help me arranging my code in proper header files, since i
will have to make several such programs in future.


You can try Lazy C++. Write your component in one file, define
entities at their declaration (Java-style), then run lzz, a C++
preparser. Many onerous coding tasks go away, like writing header
include guards and removing default args from definitions. Specially
comes in handy when you want your template definitions in a separate
file (if you want to explicitly instantiate your templates) or
inline function definitions in a separate file (for conditional inline
compilation).

You can try it out online at

http://www.lazycplusplus.net/cgi-bin/lzzcgi

It's also at

http://www.lazycplusplus.com

Hope this helps,

Mike
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4349
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program) //-------------------------------------------------------------------------- - #pragma hdrstop #pragma argsused
8
5481
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
13
1561
by: tsoukase | last post by:
Hello, two questions: 1) Why is a library a collection of compiled source-files while each header-file is a single source? Would it be more efficient if they were both either compiled or not? Could a "header-library" exist? 2) Why do libraries have extensions .a and .so and modules .o, which should be reserved for cc -c output? Would it be better: module.m, lib.sl, lib.dl or something
22
2136
by: macAWM | last post by:
Hi list, First let me explain that my background is in Java and I am quite spoiled to its niceties (read "less ambiguous nature"). Anyway to my problems. 1. I want to write my own library for what I consider to be some holes in the standard language. How do I go about writing and compiling this without this stupid error about not having a 'main' function. I don't want a stupid 'main' function. (I'm compiling using gcc 4.0.) I would...
31
4604
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there are more objects that match the first object that i grabbed. If they match then I put them in an array. I would like to remove each match from the arraylist as I find them to speed things up and so that they don't get checked again. If I try...
4
2044
by: Christoph Scholtes | last post by:
Hi, I have some questions about header files: Say I have a file functions.c which contains a couple of functions. I have declared some structs in this file too. The structs are defined in main.c. Now I create a header file which represents the interface of functions.c to my main program file main.c. I put in the header file: all function prototypes with keyword extern and the declarations of the structs, which are defined in the main...
0
6547
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file /new_file/prob1.rec started. Please wait.... In while loop of request searching *** glibc detected *** ./a.out: free(): invalid next size (normal): 0x099da890 *** ======= Backtrace: ========= /lib/libc.so.6
1
2140
by: Francesco | last post by:
Hi guys, I wrote a little script to authorize the users to download a file without showing the actual location of the file. I wrote something like if(authorized) { $file_path = "$site_path" . "/anAbsolute/pathTo/MyFile.zip"; $file_mime = "application/x-zip-compressed";
1
1380
by: johnnash | last post by:
ok so i was just reading about #ifndef and header files. I have a few doubts. It would be great help if someone can clear my doubts.. #ifndef A_H #define A_H .... code ..... #endif
0
9618
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.