2010-05-20, 07:43 PM
With Object Oriented programming comes a concept calls "design by contract". What it essentially means is that you set a pre and post condition. While some languages support this inherently, others do not, thus you tend to have it as comments.
What goes in a .h file is generally the class definition (class variables and functions) and sometimes with the above mentioned contracts.
The advantage of splitting it into 2 files, .h and .cc/.cpp is that it keeps the declaration and definition separate. This allows you to change your definition without modifying the declaration (as mentioned above). This allows libraries to exist without actually releasing your implementation source and it also isolate changes.
With design by contract in place, your method essentially guarantees that "you give me the right inputs and i'll give you the right outputs". A simple example of this may be:
public function square_integer(int intput)
{
// pre: input has to be an integer
// post: output will be an integer and will be >= 0
}
(do note that I ignore certain conditions on purpose). Those information will be enough to enable you to use an external class.
What goes in a .h file is generally the class definition (class variables and functions) and sometimes with the above mentioned contracts.
The advantage of splitting it into 2 files, .h and .cc/.cpp is that it keeps the declaration and definition separate. This allows you to change your definition without modifying the declaration (as mentioned above). This allows libraries to exist without actually releasing your implementation source and it also isolate changes.
With design by contract in place, your method essentially guarantees that "you give me the right inputs and i'll give you the right outputs". A simple example of this may be:
public function square_integer(int intput)
{
// pre: input has to be an integer
// post: output will be an integer and will be >= 0
}
(do note that I ignore certain conditions on purpose). Those information will be enough to enable you to use an external class.

