1

I have a little C++ background but am just getting into the object oriented programming facet of the language. I was reading the C++ Tutorials website about class formatting. I read that it is possible to declare a function within a class then define it outside the class using the scope operator, :: . Given my little experience, I was wondering if it is widely preferred that the function be defined within the class, or preferred if it be defined outside the class, or if it really does not matter.

From C++ Tutorials website

// classes example
#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area() {return width*height;}
};

void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
}

Meaning, wouldn't it be more compact and efficient to just include the set_values definition within the class?

As always, thanks.

John Frye
  • 45
  • 6
  • 1
    There are times when you either don't have a choice, or have expressed interest in *not* convoluting a header file with implementation, the most obvious case of the latter being the [pimpl idiom](http://stackoverflow.com/questions/8972588/is-the-pimpl-idiom-really-used-in-practice). Regardless, I suspect this question will be rife with opinionated answers. Preference questions usually are. (and I equally suspect it should take little time to dissect which answers come from Java vs. C native-speaking tongues). – WhozCraig Jun 26 '15 at 20:42
  • Bah. Everyone always ignores the Pascal speakers. – user4581301 Jun 26 '15 at 21:00

8 Answers8

1

Usually declarations are found in .h files, while definitions are in .cpp files of the same name.

Example:

rectangle.h:

class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area() {return width*height;}
};

rectangle.cpp:

#include <rectangle.h>

void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
}

This helps at the moment of distributing your code to others. You may just distribute a .dll and a .h for others to use, keeping your actual code in .cpp files.

It's also useful when .h files start to grow. It's not rare to see .cpp files of a thousand lines. Going through all of it just to find public interfaces to that class would take a lot of effort.

Note: .h and .cpp are just conventions. You may find others like .cxx and .inl (for inline functions).

Blito
  • 158
  • 1
  • 8
0

Short methods are fine inside the declaration. Longer ones are generally better kept with the other methods in the cpp file. Getters and setters are typically short, so someone (not the original author) need not care about them much while learning or debugging the other more significant methods.

donjuedo
  • 2,395
  • 14
  • 26
0

It depends on the convention you follow.

Commonplace convention is to put getters and setters inside the header file since these are functions likely to be called frequently and should therefore be inlined by the compiler. Putting functions in the header file encourages (but does not guarantee) this operation by the compiler.

Moving on, longer declarations should be kept in the .cpp for a variety of reasons. First and foremost, if you make any change in the .h, it has to recompile any files including that header. This can be a significant time increase depending on your project. Second, this provides a method to 'hide' and proprietary code that you want to build into a library. In this case, and in the most common case of good programming practice, you want the header file to be a guide for the programmer where they can quickly look at what a class does, and judging by the comments decide how to approach usage of the features provided by the class.

0

It's really personal choice in that case. I would put the function inside the class and not worry about scope resolution.

The scope resolution is used for accessing a local and global variable with the same name.

0

In C++ you mostly put the class definition in a header file (.h) and the implementation of the member functions in a code file (.cpp). Hence, it is very common that you implement a function outside its class definition. The only prominent exception being the definition of template classes, in which the full implementation sits inside the .h (or even .) file.

This is in contrast to other programming languages like C# or Java, that do not require this split-up into .h/.cpp files, because the compiler output contains the meta data of the contained classes and hence make .h files obsolete.

nv3
  • 380
  • 4
  • 9
0

Yes, it is more compact if the class definition and implementation are in the same file.

But typically you will separate the definition into a header file and the implementation into a source file. This allows you to replace the behind-the-scenes implementation without having to change the definition and force recompliation of the class's clients.

Further, if the implementation and definition are tied together, including the header will cause the implementation to be duplicated resulting in much larger output object files and often linker disputes over duplicate names.

For smaller methods, a single operation getter for example, the size impact is minimal. Often the method will be discarded by the compiler in favour of direct access.

user4581301
  • 29,019
  • 5
  • 26
  • 45
0

It is definitely down to preference. I definitely have always kept the actual function in the .cpp file as I want to keep the header file as clean as possible. To keep an important point of OOP in mind, where anyone can get a good overview on what this class can generally do, keeping the details out of the header file seems to work better than keeping functions with the declaration and making the entire file very long and hard to get a good overview of. But again, it is really down to preference.

Static
  • 1
  • 2
0

Unlike Java and C#, the usual C++ practice is that the class definitions are put in .h files and the implementation of class member functions are put in .cpp files. There are a few reasons for doing it this way.

  • This increases the readability of the class.
  • If you are library developer, by separating implementation of your code in .cpp files, you are making your library easily distributable. When you want your code to be used as library within several other projects, you distribute the compiled binary files (.a, .dll, .so or .dylib) along with header files of your project. This makes it easier for anybody who is trying to use your library and its done in almost all the third-party libraries.

There are some exceptions where short functions (which generally do not contain any loops etc.) are declared within the header files itself, and also prefixed as inline functions. This tells the compiler to optimize the given member function and replace them with inline instructions. (More about benefits of inline functions here

Community
  • 1
  • 1
Mital Vora
  • 2,048
  • 15
  • 18