473,386 Members | 1,815 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.

,h * .cpp + dependent class query

jk
Newbe Question ...

I have read "C++ the complete reference" by Herbert Schildt and
understood (most) of it :) but have a couple of queries.

(1) In real world applications the code is split into .h and .cpp
files not all just one file. The .h files having the class definitions
and prototypes and the .cpp having the methods. OK so when i #include
a file in my program I am including the headers or .h file. Why is
there this split - why not just have one file and include that ?

(2) In real world programs I have found the following construct in
class constructors to set a value to a variable:

in .h file ...

class test {
....
private:
int mHighestMenuId;
}

in .cpp file ...

test::test( )
:mHighestMenuId(6)
{
....

ie using the dependent classes initialise values section of the
constructor to initialise the variable to 6. Why do it this way ? Is
there any advantage over just
setting mHighestMenuId = 6; in the constructor ?

Dave

Apr 2 '07 #1
5 1866
On 2 Apr, 09:19, j...@pusspaws.net wrote:
(2) In real world programs I have found the following construct in
class constructors to set a value to a variable:

in .h file ...

class test {
...
private:
int mHighestMenuId;

}

in .cpp file ...

test::test( )
:mHighestMenuId(6)
{
...

ie using the dependent classes initialise values section of the
constructor to initialise the variable to 6. Why do it this way ? Is
there any advantage over just
setting mHighestMenuId = 6; in the constructor ?
I'll refer to the FAQ on this one: http://www.parashift.com/c++-faq-lit....html#faq-10.6

--
Erik Wikström

Apr 2 '07 #2
* jk@pusspaws.net:
Newbe Question ...

I have read "C++ the complete reference" by Herbert Schildt and
understood (most) of it :) but have a couple of queries.
First you should do a Google search on Herbert Schildt.

(1) In real world applications the code is split into .h and .cpp
files not all just one file. The .h files having the class definitions
and prototypes and the .cpp having the methods. OK so when i #include
a file in my program I am including the headers or .h file. Why is
there this split - why not just have one file and include that ?
Mainly for two reasons.

First, the split allows separate compilation, which means reduced build
times when only the implementation has been changed.

Second, it allows a better logical structure where implementation
dependencies are hidden down in the [.cpp] file and does not affect
client code. E.g. if the implementation uses <string>, but the
interface does not, then the client code is not forced to include
<string>. An extreme case is the PIMPL idiom -- look it up.

(2) In real world programs I have found the following construct in
class constructors to set a value to a variable:

in .h file ...

class test {
...
private:
int mHighestMenuId;
}

in .cpp file ...

test::test( )
:mHighestMenuId(6)
{
...

ie using the dependent classes initialise values section of the
constructor to initialise the variable to 6. Why do it this way ? Is
there any advantage over just
setting mHighestMenuId = 6; in the constructor ?
For an 'int' it doesn't matter. For a type that is a class with no
default constructor you couldn't do it any other way. There are also
efficiency arguments and those are discussed in the FAQ.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 2 '07 #3
jk
Thanks for both your replies - that makes sense

Dave

Apr 2 '07 #4
Alf P. Steinbach wrote:
* jk@pusspaws.net:
>Newbe Question ...

I have read "C++ the complete reference" by Herbert Schildt and
understood (most) of it :) but have a couple of queries.

First you should do a Google search on Herbert Schildt.
I second Alf's comment. I am an inveterate bookworm. I am
psychologically incapable of throwing away a book. Nevertheless, *EVERY
SINGLE COPY* of the abomination mentioned by the OP should be burned.
And the ashes blessed and sprinkled with holy water. And scattered.
Preferably by being launched into the Sun.
Apr 2 '07 #5
On Apr 2, 9:19 am, j...@pusspaws.net wrote:
I have read "C++ the complete reference" by Herbert Schildt and
understood (most) of it :) but have a couple of queries.
As others have already pointed out, Herbert Schildt is not
considered a reliable reference.
(1) In real world applications the code is split into .h and .cpp
files not all just one file.
This varies enormously. First, of course, the naming
conventions vary, and generally, .h is only used for headers
which are shared between C and C++; a pure C++ header will be
..hpp (or .hh, or in older times, .H or .hxx). In the Unix
world, at least .cc and .hh are at least as frequent as .cpp and
..hpp. And of course, the compiler doesn't care. (Up to a
point, at least; I'm not sure what would happen if you tried to
compile a file toto.exe, e.g.: cl /Tptoto.exe with VC++. OK,
just tried it. It worked like I expected: it compiled the file
as C++ code, and put the generated executable in a file
called... toto.exe. Overwriting the source.) Practically
speaking, of course, it's better to stick with .cpp/.hpp or
..cc/.hh.

Secondly, the breakdown varies. Many of the Boost components
contain only templates, and only use a .hpp file, for example,
whereas more classical libraries classes might have a .cpp per
member function. There's no one absolute rule.
The .h files having the class definitions
and prototypes and the .cpp having the methods. OK so when i #include
a file in my program I am including the headers or .h file. Why is
there this split - why not just have one file and include that ?
Because in a typical production environment, the two files are
written and maintained by two different people (with different
responsibilities). Because you don't want the smallest change
in the implementation of a member function to trigger the
recompilation of all of the user code.
(2) In real world programs I have found the following construct in
class constructors to set a value to a variable:
in .h file ...
class test {
...
private:
int mHighestMenuId;

}
in .cpp file ...
test::test( )
:mHighestMenuId(6)
{
...
ie using the dependent classes initialise values section of the
constructor to initialise the variable to 6. Why do it this way ? Is
there any advantage over just
setting mHighestMenuId = 6; in the constructor ?
There's never any moment when the variable is accessible but not
initialized. If you work with C++ programmers any amount of
time, you'll find that they don't like uninitialized variables.
For good reasons.

It's also a good habit to get into, since sometimes, it's the
only way you can initialize them. Try declaring mHighestMenuId
const, for example. Or using a reference.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 3 '07 #6

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

Similar topics

4
by: pervinder | last post by:
Hi, I get an error on line #3 while compiling the program segment on aix (xlc - va6) While this warning is not seen with gcc "test.cpp" line 200.18: 1540-0152 (W) A template dependent name that...
2
by: Andy | last post by:
Hi, I am trying to find a way to store objects and reload later so as to restore the preserved objects. But now I come to the problem that,if objects dependent on each other, how shall I...
0
by: Scott Morford | last post by:
This is a followup to a question I posed last summer. http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=utf-8&selm=3f451dd4%240%241095%247f8943f3%40newsreader.visi.com I'm developing a weed...
10
by: lorirobn | last post by:
Hi, I have a form with several combo boxes, continuous form format, with record source a query off an Item Table. The fields are Category, Subcategory, and Color. I am displaying descriptions,...
0
by: Jim M | last post by:
I have an appointment scheduling application with a number of queries that filter through a table with many thousand appointments and comes up with one days schedule. I need to start out by finding...
0
by: Jim Avera | last post by:
On AIX, a template class containing a nested class which needs to be a friend provokes a warning: (W) A template dependent name that is a type must be qualified with "typename". and method...
48
by: coool | last post by:
Hi I'm now trying to have a dependent lists in a form my form is a query based form i.e. I fill my MySQL query from this form I have around 30 fields/columns
2
by: puzzlecracker | last post by:
See it a lot but haven't learn the difference between this two in the context of template. Would someone explain it please? Thanks
26
by: jmartmem | last post by:
Greetings, I have an ASP page with two dynamic dependent list boxes written in JavaScript. My dependent lists work great, but my problem is that the values for "Program_Name" and "Project_Name"...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.