Implementation of classes in the header itself

3

In my object-oriented studies, I saw many saying that in class creation it is necessary to create a header , containing the class, attributes, and methods, as well as another cpp file to implement the methods. But I'm finding some classes on the internet where the methods are already defined in the class itself. For example:

class formaGeometrica{
private:
    float area;
    char nome[20];
    int cor;
public:
    formaGeometrica(void) {}
    ~formaGeometrica(void){}
    void setNome(char *nome)
    {
        strcpy(nome, nome);
    }
    void setArea(float area)
    {
        this.area = area;
    }
    void setCor(int cor)
    {
        this.cor = cor;
    }
}

In this case, methods are being declared inside header , right? Is this correct? Because so far, I always create a hpp for the class and then implement it in a cpp file. I have already tested this case, implementing the direct methods in header and the code worked perfectly, so it is not necessary to implement in cpp ?

    
asked by anonymous 19.09.2015 / 17:03

1 answer

4

Methods are always declared in the class definition. And the class is usually defined in a header file. But this is not an obligation. Nothing prevents the class from being defined in another file, although this is rarer in real applications.

The definition of methods, that is, their implementation is commonly done in the algorithm files (usually .cpp ), so the data structure and algorithms are separated and can be compiled on demand as needed. In general, the data structure needs the code to compile other parts of the application. The algorithm does not need source code, it is possible to use the code already compiled. This is one of the main reasons for separating.

Nothing prevents you from putting the implementation in header if you think it makes sense if you know that it is important that the algorithm be compiled together with the data structure. In fact in some cases the algorithm needs to be available.

When you have a function or method that you want to do inline optimization (copy the code instead of calling the function), the code needs to be available. Very simple methods, in general that do not bind (unless in an unroll optimization ), are good candidates for inline .

When you use templates ( template ), the compiler will generate specialized versions of the class (or another component of the language that allows you to gauge). So he needs the code to generate these versions as needed.

In the example shown the methods are very simple and it is highly desirable that they are optimized by inline , so it seems very appropriate to have the header implementation. >

Just a general tip on programming: working does not mean it's right. You have to know why it worked. Otherwise you can learn the wrong things that work just by coincidence, luckily. In this case it is and now you know why.

And you know that this code is half C ++ and half C, right?

    
19.09.2015 / 17:19