'Is it possible to define multiple classes in just one .cpp file?
I'm working on a project for school and the instructor insists that all code go into one .cpp file (for easier grading on his part). I would like to define multiple classes within this file. Will I run into any problems in doing this?
Solution 1:[1]
Yes, you can. This is easily verifiable.
class C
{
};
class D
{
};
int main(int argc, char**argv)
{
return 0;
}
Solution 2:[2]
You can perfectly have several classes declared and defined in the same file.
You have to be careful, though, when a class depends on another which comes later. If a class A is to be a member of another class B, the compiler needs its complete declaration (so that it knows its size), and you need to put it higher up in the file. If this member is just a pointer however (which has a size independent of the size of the class pointed to), a simple forward-declaration of the pointed-to class is enough.
Note that with the #include mechanism, that's pretty much what happens anyway: the preprocessor will "copy-paste" all included files into the file being compiled. To the compiler, it's just the same.
Solution 3:[3]
There is no problem in writing multiple classes into single file. It is only matter of maintenance style.
Solution 4:[4]
Typically, you should only put multiple classes in a single file if...
The classes are very tightly linked. E.g., if a class defines its own iterator, then it might be appropriate to put that iterator class in the same file as the class that it's used to iterate over.
One of the classes is for public consumption, and the remaining classes are used to implement it. E.g., this will apply if you use the "pimpl" idiom, where the only member that the public class contains is a pointer to the private class. In this case, it may be appropriate to put the private/hidden classes in the source file corresponding to the public class that uses them.
In both cases, the decision of whether to put the "public helper" (e.g. iterator) and "private helper" (e.g. pimpl) classes in the same source file or a different source file should be made once for an entire project, and followed consistently.
Solution 5:[5]
Just scalability/maintainability issues ;) There's no strict rules about it.
Solution 6:[6]
Yes you can. This must be supported because otherwise you wouldn't be able to create nested classes.
class outside
{
public:
class nested
{
public:
static int x;
static int y;
int f();
int g();
};
};
Solution 7:[7]
You won't have any trouble defining multiple classes in a single file.
It isn't especially good practice for production systems, but that's not an issue for homework
Solution 8:[8]
The question is coming from the java/C# style source representation. In C++ the source code layout is very different. One source unit /"cpp file"/ could handle as much declarations as you wish to put in to. However it is not necessary a good strategy since at some point you need to tell to other classes how to deal with your classes. In C++ a class can have a separated definition and declaration section and you are able to place them in to different files. The definition of the class is shared between other source files with #include statements so you can use those classes in any scenario where the definition is needed. If you're dealing with classes which are used only in one source file, you can hide it right in to the source files, but if you would like to connect them to other classes you need to put the definition in to a header file.
It could help you to improve the compilation speed - especially if you're using pre-compiled headers, but caresully, because pch-s are not part of the C++ standard and every compiler treats them in a different way! - and the flexibility of your code. Your teacher should be aware of these facts...
Solution 9:[9]
Yes, you could do it without problem. either multiple class or nested class.
Solution 10:[10]
In C++ you can define multiple classes inside of a single file. You will not run into trouble by doing it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
