tags:

views:

115

answers:

6

Learning C++ and see the class laid out like this:

class CRectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area () {return (x*y);}
};

void CRectangle::set_values (int a, int b) {
  x = a;
  y = b;
}

I know Java and methods(functions) in Java are written within the class. The class looks like a Java interface. I know I can write the class like this:

class CRectangle {
    int x, y;
  public:
    void set_values (int a, int b) {
      x = a;
      y = b;
    };
    int area () {return (x*y);}
};

But is there a difference or standard?

+6  A: 

There's a difference. When you write the definition of the function within the class definition (case 2), then the function is considered to have been declared inline. This is standard C++.

Usage, is to declare the member functions (Java methods) within the class definition, in a header file (.h), and to define these member functions in a C++ file (.cpp, .cc, or .C, …) This reduces compilation time, when you change the body of a function, only the C++ file has to be compiled, whereas if you change something in the header file, all C++ files that include this header are to be compiled.

Didier Trosset
+3  A: 

The usual way is to put the implementation of functions within a separate cpp file, which is not included by other files, just compiled. This way it is easier to manage dependencies.

If the implementation of a function is given within the class definition (as in your 2nd example), this is a hint for the compiler to inline that function (which it may or may not do in the end, depending on e.g. the internals of the function). As a rule of thumb, function bodies longer than a few lines of code are better put into the cpp file, as these are probably not going inlined, however these may include extra dependencies and clutter the class definition.

The exception is template functions, where the bodies need to be in the header too, for the compiler must see them in order to instantiate the template.

Péter Török
+1  A: 

I believe that if the method body is inside the class definition, then it is inlined everywhere is it called.

Douglas Leeder
AFAIK this is only a _hint_ to the compiler - which is still free to decide whether or not that function is inlined in the end.
Péter Török
Maybe it's only if the class is defined in a header that it has to be inlined? Or maybe each compilation unit gets it own copy of the code, but only one?
Douglas Leeder
+4  A: 

It's much cleaner if you only define prototypes in the class definition (which belongs into a header file) and implement the methods in a cpp file.

When you have very small classes doing everything in the class definition might sound easier since everything is at the same place, but as soon as your class grows any developer using it will hate you since he'll have your code between the method prototypes he might look at to find out something (not everyone uses an IDE which shows all available methods!).

However, there is one exception: Template class methods needs to be implemented in the header as they need to be compiled for every specialization of the template.

ThiefMaster
+1 for mentioning templates as a special case.
Donal Fellows
+1  A: 

In C++, member functions which are defined in class are implicitly inline, member functions defined outside the class are not.

This affects the linkage of your two examples - the linker will complain about multiple definitions if the first example is #included in multiple source files, whereas the second example will not.

In terms of what's standard/normal:

  • The first form is used for complex classes, where the implementation of the functions requires extra headers to be pulled in.
  • The second form is used for simple classes which don't have extra dependencies.
  • Either form is used for template classes based on developer preference.
Joe Gauterin
+1  A: 

There is a slight difference in that functions defined inside the class are automatically inline. The latter definition can be put into a separate file. Otherwise there is no difference nor preference.

Note that we usually don't write semicolons after function definitions (though they are allowed inside a class definition).

void set_values (int a, int b) {
  x = a;
  y = b;
} // no semicolon here
avakar