Tip # 1:
Whenever a .h
or .cpp
has external references, the header of that external reference must be included. This increases decoupling between modules and improves the readability of the code.
References:
SoftwareEngineering : link
Tip # 2:
Always use the reserved word void
in methods / functions that do not receive parameters, for example, replacing T::foobar()
with T::foobar(void)
is an excellent practice because:
Improves readability: T::foobar()
looks like a call
while T::foobar(void)
looks like an implementation and / or
definition);
Improves the compiler's interpretation of the code, avoiding
problem called most vexing parse ;
Ensures interface compatibility with C
, since C
a
functions declared as foobar()
do not have the same meaning
which functions declared as foobar(void)
.
References:
Wikipedia link
StackOverflow:
link
Tip # 3:
Always using virtual destructors in class definitions is another good practice, this allows for polymorphic manipulations, allows cascatear
to call destructors from the derived class to base classes, avoiding leaks from memory.
References:
StackOverflow: link
Tip # 4:
Class builders allow you to initialize your members in a startup list, this is meant to be used! Avoid initializing your class members within the constructor implementation.
References:
StackOverflow: link
StackOverflow:
link
How would your code look like:
main.cpp
#include "Teste_1.h"
int main(void){
Teste_1 * test = new Teste_1( "you never win! but you can get my money", "Hello" );
delete test;
return 0;
}
Test_1.h:
#ifndef TESTE_1_H_INCLUDED
#define TESTE_1_H_INCLUDED
#include <string>
class Teste_1{
private:
std::string var_str1;
std::string var_str2;
public:
Teste_1( std::string var1, std::string var2 );
virtual ~Teste_1(void);
};
#endif // TESTE_1_H_INCLUDED
Test_1.cpp
#include "Teste_1.h"
#include <string>
Teste_1::Teste_1( std::string var1, std::string var2 ) :
var_str1(var1),
var_str2(var2)
{
}
Teste_1::~Teste_1(void){
}
Compiling:
$ g++ main.cpp Teste_1.cpp -o teste