Hi,
Comming from Java, it seems that prototype declarations are like abstract
methods. I have not read classes in C++ yet, but prototype declarations are
a strange concept. Any comments appreciated.
Unlike Java, C++ allows you to split the specification of a class into two
pieces: its prototype declaration, and its implementation. i.e.:
class A
{
public:
void foo() {
frobnicate();
}
void bar() {
refrobnicate();
}
protected:
int baz() {
return alakazam();
}
};
is equivalent to:
// Interface description -- normally goes in .h file
class A
{
public:
void foo();
void bar();
protected:
int baz();
};
// Implementation -- normally goes in .cpp file
void A::foo() {
frobnicate();
}
void A::bar() {
refrobnicate();
}
int A::baz() {
return alakazam();
}
Although the preceding two blocks of code are essentially equivalent, there is
a significant advantage of splitting the two blocks of code into its prototype
declaration (typically saved in a "header" (normally *.h) file) and its
implementation (typically saved in a *.cpp file)...
Unlike C++, Java independently compiles each class (*.java) into its own
"object" file (*.class)--other classes that need to refer to a class will
examine the .class file to obtain the interface information, including the
names and types of each method. (Please correct me if I'm wrong--I am not a
Java expert by any means).
C++, however, relies on textual processing to evaluate the interface to a
class. Since the interface to each class must be declared before it is used,
each translation unit (.cpp file) that uses class A must describe the
interface to class A before using it. This is typically done by #include-ing a
common file to avoid replication of code.
However, it is an error to declare the _implementation_ of a method more than
once (your linker will complain about this), so the first code example cannot
be the file that other .cpp files include.
Instead, each file must include just enough information to completely describe
the class interface without supplying its implementation. Thus the interface
prototype goes into the .h file (which other .cpp files can freely include),
while the implementation is placed in a .cpp file, to be compiled only once
during the build.
Hope that helps. My apologies for the various inaccuracies that I have no
doubt inserted into the text above.