views:

448

answers:

11

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?

+14  A: 

There is no rule you have to follow (like in java). You're free to place and name classes in however named files you like (besides the suffix).

However its another question if that is good practices (its not!).

RED SOFT ADAIR
To add to this, don't forget that you have to put the classes you are going to use before your main class or the compiler won't find them.(unless you use forward declaration)
DaMacc
+5  A: 

There is no problem in writing multiple classes into single file. It is only matter of maintenance style.

aJ
+3  A: 

Just scalability/maintainability issues ;) There's no strict rules about it.

gorsky
If this school project suffers from scalability issues, Chad needs to negociate a salary for his work ;-)
Carl Seleborg
+7  A: 

Yes, you can. This is easily verifiable.

class C
{
};

class D
{
};

int main(int argc, char**argv)
{
  return 0;
}
Brian R. Bondy
although the usual C++ disclaimer applies, "just because your compiler accepts it doesn't mean it's allowed according to the C++ standard" (although in this case it is of course perfectly valid)
jalf
Yes, good point.
Brian R. Bondy
+3  A: 

No. You will not run into trouble. In C++ you can define multiple classes inside of a single file.

arhuaco
+3  A: 

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.

Carl Seleborg
+3  A: 

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

Liz Albin
+3  A: 

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();
      };
};
RC
+2  A: 

Typically, you should only put multiple classes in a single file if...

  1. 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.

  2. 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.

Edward Loper
+2  A: 

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...

progician
+2  A: 

Yes, you could do it without problem. either multiple class or nested class.

Steve Zhang