Is there a way to avoid the Graph::
repetition in the implementation file, yet still split the class into header + implementation? Such as in:
Header File:
#ifndef Graph_H
#define Graph_H
class Graph {
public:
Graph(int n);
void printGraph();
void addEdge();
void removeEdge();
};
#endif
Implementation File:
Graph::Graph(int n){}
void Graph::printGraph(){}
void Graph::addEdge(){}
void Graph::removeEdge(){}
If you want to avoid typing the "Graph::" in front of the printGraph, addEdge etc., then the answer is "no", unfortunately. The "partial class" feature similar to C# is not accessible in C++ and the name of any class (like "Graph") is not a namespace, it's a scope.
No, there is no way to avoid it. Otherwise, how would you know if a given function definition is for a class function or for a static function?
I'm guessing this is to avoid lots of "unnecessary typing". Sadly there's no way to get rid of the scope (as many other answers have told you) however what I do personally is get the class defined with all my function prototypes in nice rows, then copy/paste into the implementation file then ctrl-c your ClassName:: on the clip board and run up the line with ctrl-v.
If you are asking if you can define a member function such as
Graph::printGraph
without specifying the class name qualification, then the answer is no, not the way that you want. This is not possible in C++:The above will compile just fine, but it won't do what you want. It won't define the member function by the same name within the
Graph
class. Rather, it will declare and define a new free function calledprintEdge
.This is good and proper, if by your point of view a bit of a pain, because you just might want two functions with the same name but in different scopes. Consider:
Which scope should the definition apply to? C++ does not restrict you from having different functions with the same names in different scopes, so you have to tell the compiler what function you're defining.
EDIT: I misread your question :( this would be an answer to the question whether you can split header-files. sorry
The simple answer: You can split up c++-file, but you can not split up header-files.
The reason is quite simple. Whenever your compiler needs to compile a constructor, it needs to know exactly how many memory it needs to allocate for such an object.
for example:
'new Foo()' would require the allocation of 12 bytes memory. But if you were allowed to extends your class definitions over multiple files (i.e. split header files), you would easily make a mess of this. Your compiler would never know if you already told it everything about the class, or whether you did not. Different places in your code could have different definitions of your class, leading to either segmentation faults or extremely cryptical compiler errors.
For example:
h1.h:
h2.h: #include "h1.h"
foo1.cpp:
foo2.cpp:
foo1.h:
foo2.h:
main.cpp:
Now very carefully check what happens if you first compile main.cpp to main.o, then foo1.cpp to foo1.o and foo2.cpp to foo2.o, and finally link all of them together. This should compile, but the makeFoo() allocates someting else then the cleanupFoo() deallocated.
So there you have it, feel free to split .cpp files, but don't split up classes over header files.