Class implementation in the header itself

Asked

Viewed 562 times

3

In my object orientation studies, I have seen many say that in creating classes it is necessary to create a header, containing class, attributes and methods, and also another file cpp 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, the methods are being declared within the header, right? Is that correct? Because so far, I always create a hpp for the class and then implement it into a file cpp. I have tried this case, implementing the direct methods in header and the code worked perfectly, so it is not necessary to then implement in a cpp?

1 answer

4


Methods are always declared in the class definition. And the class is normally defined in a file header. But this is not a must, nothing prevents the class from being defined in another file, although this is more rare in actual applications.

The definition of methods, i.e., their implementation is commonly done in the algorithmic 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 already compiled code. This is one of the main reasons to separate.

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

When you have a function or method you want to be made an optimization of inline (copies the code instead of calling the function), the code must be available. Very simple methods, in general that do not make loops (unless in cases that may occur an optimization of unroll), are great candidates for the inline.

When you use jigs (template), the compiler will generate specialized versions of the class (or another component of the language that allows templates). 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, then it seems to be very appropriate to have the implementation in the header.

Just a general hint about programming: working doesn’t mean it’s right. You have to know why it worked. Otherwise you may learn wrong things that work only by coincidence, by luck. In this case you are right and now you know why.

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

  • I took this code in an object orientation tutorial. You would say half C on account of the strcpy?

  • The use of array of char as a whole.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.