473,396 Members | 2,111 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,396 software developers and data experts.

How to define a class in more than two .h files?

I am creating a big class.
and I want to define in different files for this class.
How to do it?
Thanks.
Jul 22 '05 #1
13 1483
ronic wrote:
I am creating a big class.
Are you sure you can't split it up into several smaller classes?
and I want to define in different files for this class.


Define what? The class itself or its member functions? The latter is
easy. You can simply make several implementation files that contain the
functions. The former would be possible, but I'd advice against it,
since it makes the code less readable. It could go something like:

//foo.h
class Foo
{
public:

# include "foo_functions1.h"
# include "foo_functions2.h"
# include "foo_functions3.h"
};
//foo_functions1.h

void someFunc(int param);
void anotherFunc() const;
//...

and so on.
Jul 22 '05 #2
ronic wrote:
I am creating a big class.
Big classes are usually a sign of poor design. Are you sure you really
need such a big class? Think twice before answering.
and I want to define in different files for this class.
I am of the school "one component, one file" where "component" is one or
more strongly related classes. Therefore it sound blasphemous to me to
even think of spliting one single class in more than one file.
How to do it?


*Very reluctantly*, the _only_ way is to write this:

class BigBlasphemousClass
{
#include "BigBlasphemousClass1.h"
#include "BigBlasphemousClass2.h"
};

but merely looking at such thing gives me shivers.

Regards,

Alberto Barbati
Jul 22 '05 #3

"ronic" <ia***@sohu.com> wrote in message
news:35**************************@posting.google.c om...
I am creating a big class.
and I want to define in different files for this class.
How to do it?
Thanks.


A big class is probably bad design.
Not knowing how to put it in different files means you are an inexperienced
programmer who
hasn't read much which again implies that the class is probably a bad
design.

I know it's not directly helpful but I promise that in the long term
you'll be better off just stuffing it all one file and spending the time
saved reading C++ design books so you do it right next time.

Assuming that you ignore my advice:
If the problem is that the declarations (.h) file is too big then:
1. Don't use inline methods.
2. If you do use inline methods - define them in a sparate file included
from the main .h

If the problem is just the size of the .cpp/.cxx etc file then just split it
in two - there
is no (practical) reason not to.
Jul 22 '05 #4
"Alberto Barbati" <Al************@libero.it> wrote
I am of the school "one component, one file" where "component"
is one or more strongly related classes.


You've renewed my faith in humanity. :-)

Claudio Puviani
Jul 22 '05 #5
Nick Hounsome wrote:
"ronic" <ia***@sohu.com> wrote in message
news:35**************************@posting.google.c om...
I am creating a big class.
and I want to define in different files for this class.
How to do it?
Thanks.

If the problem is just the size of the .cpp/.cxx etc file then just split it
in two - there
is no (practical) reason not to.


The practical problem is that you then have more than one
place to look for the source. I like to have corresponding
file names:

include/somecomponent/myClass.h
source/somecomponent/myClass.cpp

having to ferret about looking in

source/somecomponent/myClass_part1.cpp
source/somecomponent/myClass_part2.cpp
source/somecomponent/myClass_part3.cpp

is a tad annoying.
Jul 22 '05 #6

"lilburne" <li******@godzilla.net> wrote in message
news:c1*************@ID-179504.news.uni-berlin.de...
Nick Hounsome wrote:
"ronic" <ia***@sohu.com> wrote in message
news:35**************************@posting.google.c om...
I am creating a big class.
and I want to define in different files for this class.
How to do it?
Thanks.

If the problem is just the size of the .cpp/.cxx etc file then just split it in two - there
is no (practical) reason not to.


The practical problem is that you then have more than one
place to look for the source. I like to have corresponding
file names:

include/somecomponent/myClass.h
source/somecomponent/myClass.cpp

having to ferret about looking in

source/somecomponent/myClass_part1.cpp
source/somecomponent/myClass_part2.cpp
source/somecomponent/myClass_part3.cpp

is a tad annoying.


By practical I only meant easy to create not easy to maintain
Jul 22 '05 #7
ronic wrote:
I am creating a big class.


Big classes are usually a sign of poor design. Are you sure you really
need such a big class? Think twice before answering.


I read what you and others say but what about cases where the splitting of
files is more for ease of use rather than design.

For example imaging a data handling class.

You could split the class to make it easier to handle.

class SomeDataHandelingClass
{
#include "SomeDataHandelingClass_READ.h";
#include "SomeDataHandelingClass_WRITE.h";
#include "SomeDataHandelingClass_MISC.h";
};

Would that not be a case where splitting the class would make it somewhat
easier to read eventhouh the class might not be that big?

Simon
Jul 22 '05 #8
Sims wrote:
ronic wrote:


I read what you and others say but what about cases where the splitting of
files is more for ease of use rather than design.

For example imaging a data handling class.

You could split the class to make it easier to handle.

class SomeDataHandelingClass
{
#include "SomeDataHandelingClass_READ.h";
#include "SomeDataHandelingClass_WRITE.h";
#include "SomeDataHandelingClass_MISC.h";
};

Would that not be a case where splitting the class would make it somewhat
easier to read eventhouh the class might not be that big?


No. Your SomeDataHandelingClass.h file may be easier to read, but the
three sub-include files are definitely not. Those files won't be
self-contained and their interpretation will be dependent on something
external to them. How could they be easier to read?

The only case in which I consider splitting a class definition into two
(not more) include files is if I define a lot of inline functions. In
that case I may write:

class ClassWithInlineFunctions
{
// declaration of all members
};

// definition of all inline member functions
#include "ClassWithInlineFunctions.inl"

Notice that the file "ClassWithInlineFunctions.inl" would still be
self-contained (at least at the syntactic level) as the #include occurs
at global scope. Except for some namespace-wrapping techniques used to
solve compatibility issues with legacy code, putting an #include
anywhere except at global scope is pure evil.

Alberto
Jul 22 '05 #9
"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message news:<Qk***********@news-binary.blueyonder.co.uk>...
"lilburne" <li******@godzilla.net> wrote in message
news:c1*************@ID-179504.news.uni-berlin.de...
Nick Hounsome wrote:
"ronic" <ia***@sohu.com> wrote in message
news:35**************************@posting.google.c om...

>I am creating a big class.
>and I want to define in different files for this class.
>How to do it?
>Thanks.
If the problem is just the size of the .cpp/.cxx etc file then just split it in two - there
is no (practical) reason not to.


The practical problem is that you then have more than one
place to look for the source. I like to have corresponding
file names:

include/somecomponent/myClass.h
source/somecomponent/myClass.cpp

having to ferret about looking in

source/somecomponent/myClass_part1.cpp
source/somecomponent/myClass_part2.cpp
source/somecomponent/myClass_part3.cpp

is a tad annoying.


By practical I only meant easy to create not easy to maintain


Thank for all your good help.
I am creating a class to simulate a micro controller. The micro
controller has
several hundreds instructions. So there are serveral hundreds private
instruction handlers. So I do like this

private:
#include "act.h"
#include "act_ABS.h"
#include "act_ADC_1.h"
#include "act_ADC_2.h"
#include "act_ADCF.h"
#include "act_ADD_1.h"
#include "act_ADD_2.h"
#include "act_ADD_3.h"
......
Jul 22 '05 #10
"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message news:<bp*************@news-binary.blueyonder.co.uk>...
"ronic" <ia***@sohu.com> wrote in message
news:35**************************@posting.google.c om...
A big class is probably bad design.
Hogwash.
Not knowing how to put it in different files means you are an inexperienced
programmer who hasn't read much
Hogwash.
which again implies that the class is probably a bad
design.
Hogwash.
I know it's not directly helpful but I promise that in the long term
you'll be better off just stuffing it all one file and spending the time
saved reading C++ design books so you do it right next time.


It is not only "not directly helpful", but it is insulting. I feel as
if I should apologize to the op on your behalf. A person need not be
familiar with every possible technique to design or produce quality
software. A series of assumptions and vacuous conclusions on your
part does not render the op ignorant.
Jul 22 '05 #11
> It is not only "not directly helpful", but it is insulting. I feel as
if I should apologize to the op on your behalf. A person need not be
familiar with every possible technique to design or produce quality
software. A series of assumptions and vacuous conclusions on your
part does not render the op ignorant.
Amen.

anon luker wrote:
"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message news:<bp*************@news-binary.blueyonder.co.uk>...
"ronic" <ia***@sohu.com> wrote in message
news:35**************************@posting.google.c om...
A big class is probably bad design.


Hogwash.
Not knowing how to put it in different files means you are an inexperienced
programmer who hasn't read much


Hogwash.
which again implies that the class is probably a bad
design.


Hogwash.
I know it's not directly helpful but I promise that in the long term
you'll be better off just stuffing it all one file and spending the time
saved reading C++ design books so you do it right next time.


It is not only "not directly helpful", but it is insulting. I feel as
if I should apologize to the op on your behalf. A person need not be
familiar with every possible technique to design or produce quality
software. A series of assumptions and vacuous conclusions on your
part does not render the op ignorant.

Jul 22 '05 #12
ronic wrote:
"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message news:<Qk***********@news-binary.blueyonder.co.uk>...
"lilburne" <li******@godzilla.net> wrote in message
news:c1*************@ID-179504.news.uni-berlin.de...
Nick Hounsome wrote:
"ronic" <ia***@sohu.com> wrote in message
news:35**************************@posting.goog le.com...
>I am creating a big class.
>and I want to define in different files for this class.
>How to do it?
>Thanks.
If the problem is just the size of the .cpp/.cxx etc file then just


split it
in two - there
is no (practical) reason not to.
The practical problem is that you then have more than one
place to look for the source. I like to have corresponding
file names:

include/somecomponent/myClass.h
source/somecomponent/myClass.cpp

having to ferret about looking in

source/somecomponent/myClass_part1.cpp
source/somecomponent/myClass_part2.cpp
source/somecomponent/myClass_part3.cpp

is a tad annoying.


By practical I only meant easy to create not easy to maintain

Thank for all your good help.
I am creating a class to simulate a micro controller. The micro
controller has
several hundreds instructions. So there are serveral hundreds private
instruction handlers. So I do like this

private:
#include "act.h"
#include "act_ABS.h"
#include "act_ADC_1.h"
#include "act_ADC_2.h"
#include "act_ADCF.h"
#include "act_ADD_1.h"
#include "act_ADD_2.h"
#include "act_ADD_3.h"
......


It appears to me that you are going to do this anyway, then why post here?

You heard all sorts of reasons why not to do it, but you insist on doing
it...

Anyway, you could divide your microcontroller instructions by type
(math, comparison, etc) into different smaller classes. That you way
you have manageable classes, while splitting the functions into smaller
modules.

Just a thought,

Jorge L.
Jul 22 '05 #13
ronic wrote:
I am creating a class to simulate a micro controller. The micro
controller has
several hundreds instructions. So there are serveral hundreds private
instruction handlers. So I do like this

private:
#include "act.h"
#include "act_ABS.h"
#include "act_ADC_1.h"
#include "act_ADC_2.h"
#include "act_ADCF.h"
#include "act_ADD_1.h"
#include "act_ADD_2.h"
#include "act_ADD_3.h"
......


If all those are private, why would you want to put them in the class
declaration? You are creating a dependency hell! If you change or add
one single instruction handler, you have to recompile the whole
program... All that is, technically, unnecessary. The client of you
class does not need to know all that stuff.

If that's the case, I may suggest using free-functions defined into an
implementation namespace and move all your #includes inside the .cpp
file of your class.

Alberto Barbati
Jul 22 '05 #14

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

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
14
by: Carl Ribbegaardh | last post by:
What other c++ constructs can I use instead of #define for executing a couple of functions? Example: #define DO_STUFF doThis(); doThat(); I'd guess that I can either use a template function,...
14
by: Blue Ocean | last post by:
My c++ text tells me that I should define methods this way: class Stack { int method(double t); Stack(int s); ... } int Stack::method(double t)
6
by: David Young | last post by:
Hello all, I'm quite new to C# (< 6 months) but really love it and is my language of choice ..... but I have one question I've not been able to find out ..... In C++ a #define label in one...
7
by: Don Wash | last post by:
Hi There! I'm trying to define constants so that I can refer those constants from any page of my ASP.NET website. I know I can use <appSettings> in web.config XML file but I don't want to parse...
9
by: C. J. Clegg | last post by:
When you say "const int FOO = 0" (as the commonly-recommended C++ alternative to "#define FOO 0"), isn't that declaration globally visible? I have "const int FOO = 0;" in one source file and...
7
by: aaragon | last post by:
Hi everyone, I have a simple question. I'm trying to make a macro in one file so I can use it in main.cpp. The idea is that I the user of my code has simple to type the macro definition to replace...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
11
by: Sachin Garg | last post by:
I need to build two executables from my code, one having all the code (and thus application features) and other not having it all. How to best manage the code that shouldn't go in one of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.