Which is better place to define functions

R

Ravi

I can define functions within the class body as well as outside by
using the scope operator. Which is the better place to do so? In Java
we do it it within the class body so the former method seems more
logical to me.

Another related question is that experienced C++ programmers use
separate header files for class definition and method definitions.
What will be the loss if I make class definitions and function
definition in the same file and include it in other files?

I am an experienced C programmer who has a little experience of Java
and started learning C++, so many concepts are new to me.
 
M

maverik

I can define functions within the class body as well as outside by
using the scope operator. Which is the better place to do so?

It depends. You can define it within the class body it function body
is small or trivial. If function body is big it's better to define
outside of the class.

Also, there are many techniques requiring one of this method. If, for
example, you decide to separate definition from implementation, you
probably should define functions outside.

So there is no right answer at all.
In Java
we do it it within the class body so the former method seems more
logical to me.

Another related question is that experienced C++ programmers use
separate header files for class definition and method definitions.
What will be the loss if I make class definitions and function
definition in the same file and include it in other files?

The first thing I can think of is that you need to rebuild all modules
that depends of this file. E.g. yo have

BaseModule.h:

class MyBaseClass {
public:
int ReturnNumber() { return 42; }
};

MyModule001.h:
#include <BaseModule.h>

....

MyModule999.h:
#include <BaseModule.h>
....

And some time later you decide to change ReturnNumber function:

int ReturnNumber() { return 0x0501; }

In this case you have to rebuild all modules [MyModule001 ...
MyModule999]

Of course, there are many other issues.
 
H

Hel

I can define functions within the class body as well as outside by
using the scope operator. Which is the better place to do so? In Java
we do it it within the class body so the former method seems more
logical to me.

If you think of it in terms of separating the interface from the
implementation, it's more logical to do it in separate files. If, for
instance, all you want is to find the declaration of a member
function, cluttering the interface with hundreds of lines of
implementation is just annoying.
Another related question is that experienced C++ programmers use
separate header files for class definition and method definitions.
What will be the loss if I make class definitions and function
definition in the same file and include it in other files?

If the functions are not declared inline (which they are, as Saeed
pointed out, if they are defined /inside/ the class definition), you
will probably get a linker error about the functions being multiply
defined if you include the same class definition more than once.

Greets, Hel
 
M

Matthias Buelow

Ravi said:
I can define functions within the class body as well as outside by
using the scope operator. Which is the better place to do so? In Java
we do it it within the class body so the former method seems more
logical to me.

Since C++ is lacking a proper module system, you'd better not make
header files too fat or you'll have to cope with astronomical compile
times. Another thing is that you can split your code over several files
as you like it, it gives a little more flexibility. Of course, in Java
you can't not use classes and everything has to be in a class, and these
restrictions allow for a straightforward class-file based module system.
It doesn't really make sense to compare Java and C++ here because
they're very much different in that regard.
Another related question is that experienced C++ programmers use
separate header files for class definition and method definitions.

What do you mean by that?
What will be the loss if I make class definitions and function
definition in the same file and include it in other files?

Isn't that exactly the same question as above?
I am an experienced C programmer

Aha.
 
G

George Kettleborough

I can define functions within the class body as well as outside by
using the scope operator. Which is the better place to do so? In Java
we do it it within the class body so the former method seems more
logical to me.

Another related question is that experienced C++ programmers use
separate header files for class definition and method definitions.
What will be the loss if I make class definitions and function
definition in the same file and include it in other files?

I am an experienced C programmer who has a little experience of Java
and started learning C++, so many concepts are new to me.

Well a concept which you should be used to from C is keeping interface
separate from implementation. Usually you put the interface in a header
file (.h) and include this in all .cc files that you need it in. All
implementation stays in .cc files. What this means is you declare
classes and functions in .h files and then implement them in .cc files.

One advantage of this is that anyone using your class does not need to
know the implementation of your class, they only need to know the
interface. The implementation can change freely but the interface
remains the same.

Another advantage is that things can be built separately. If you include
entire implementations in every file then you will have to rebuild your
entire project for each small change that you make. The UNIX tool make
helps you with this. If you have main.cc which uses a class defined in
class.h and implemented in class.cc, main.cc will include class.h so
that you can use the class. Then main.cc and class.cc can be built
separately. If you make a change to main.cc there is no need to rebuild
class.cc.

This is a bit different if you use templates though because you need to
include the entire template implementation whenever you use it (because
the compiler needs to see the whole code in order to instantiate it).
There are other ways to include template code, but this is the simplest
and most popular.
 
J

James Kanze

I can define functions within the class body as well as
outside by using the scope operator. Which is the better place
to do so? In Java we do it it within the class body so the
former method seems more logical to me.

In general, you don't want the function definitions (an
implementation detail) in the same file as the class definition
(which client code has to see); this is a well-known (and
serious) defect of Java (and C++ templates).
Another related question is that experienced C++ programmers
use separate header files for class definition and method
definitions.

Not separate header files: they put the class definition in a
header file, because it is needed by the client code. Function
definitions are just an implementation detail; the client code
never sees them.
What will be the loss if I make class definitions and function
definition in the same file and include it in other files?

You'll get the same unmaintainable mess you get in Java. It can
be made to work for smaller projects, but as soon as several
people start working on a project, it becomes a real hassle.
(The classical work-around in Java is to define an interface for
everything, and only furnish it. This doesn't work in C++
because it requires dynamic allocation, and most of the time,
you don't want to dynamically allocate yor objects in C++.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top