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
?