0

Why is a class in 2 separated files a good style im c++?

I'm asking this because I'm a java programmer for 4 years and can't understand why I suddenly need 2 files? I'm struggling with the compiler to write a class similar to java in one file..:) or do I need to reconfigure the compiler to do so?

29th Jul 2017, 8:59 PM
Theresa Leighfy
Theresa Leighfy - avatar
5 Answers
+ 9
Imagine if you have one .cpp file about 10k lines which contains 10 classes, a few used for designs, a few for calculations, some used to deal with text files, some interdependent on other structures or classes (at worse, some of your classes may have been using part of global variables/functions). If you want to reuse one or a few of the classes for another project, you would be dangling in wires. By separating independent classes into different .h files, you can directly locate the entire .h file and add it to your existing solution/project. Even if dependencies exist, you can easily point out other interrelated classes (by looking at the #includes).
30th Jul 2017, 2:52 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
because classes are meant to be portable. Having a class in another file makes it easy to use in other projects.
29th Jul 2017, 10:10 PM
Edi Lipovac
Edi Lipovac - avatar
0
but I think they can also be portable in one file. Or am I missing something?
29th Jul 2017, 10:18 PM
Theresa Leighfy
Theresa Leighfy - avatar
0
exactly:)
29th Jul 2017, 10:31 PM
Theresa Leighfy
Theresa Leighfy - avatar
0
I know what you mean. I don't want to write 10 classes in one file. just one class like in java. One class per file you do can reuse. Thats all I wanted. But thank you all:)
30th Jul 2017, 4:55 PM
Theresa Leighfy
Theresa Leighfy - avatar