Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
Say I have two classes:

Code:
class A
{
    // stuff
}

and

Code:
class B extends A
{
    // other stuff
}

is it standard practice to keep the two classes in the same file or to put them in separate files? I'm kinda new to OOP programming practices.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi

Yup I would say different files too. as a result nowever, with a reasonable sized project you do end up with loads of files in C++. So on occasions I do bend the 'rules' and lump certain classes together. For example,

class uipoint ;
class uibounds ;

They will most certainly always be used together so I declare them in the same file. I guess I get a bit lazy sometimes but this isn't necessarily a bad thing as an overcomplicated #include hierarchy can make using your C++ classes a pain - one of the problems with having separate files for every class declaration. One way you can reduce the number of dependancies of #includes is to use forward declarations of classes in your .h files. See Item 31 of Scott Meyers Effective C++ for a really good explanation (much better than what I could give here!).

b e n
 

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
They will most certainly always be used together so I declare them in the same file. I guess I get a bit lazy sometimes but this isn't necessarily a bad thing as an overcomplicated #include hierarchy can make using your C++ classes a pain - one of the problems with having separate files for every class declaration. One way you can reduce the number of dependancies of #includes is to use forward declarations of classes in your .h files. See Item 31 of Scott Meyers Effective C++ for a really good explanation (much better than what I could give here!).

b e n

Thanks for the advice. I am actually using PHP and not C++, but the concepts are most likely the same. Although I am not sure if PHP has a function similar to the one you describe for C++. The only thing I can find is the autoload function.
 

trule

macrumors 6502
Mar 16, 2007
310
0
Thanks for the advice. I am actually using PHP and not C++, but the concepts are most likely the same. Although I am not sure if PHP has a function similar to the one you describe for C++. The only thing I can find is the autoload function.

PHP does not have all C/C++'s hangups so require_once() normally does the trick.
 

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
I am very sorry to hear that. I wish you a speedy recovery from the RSI you're going to get from typing $this-> many times.

b e n

:D.

I actually started this project using Java Server Pages and PostgreSQL but due to needing a dev server I just couldn't find any shared hosts that offered what I needed, plus I couldn't justify the $80 or so a month needed for my own dedicated server so I had to fall back on using PHP and MySQL.

Shame really, PostgreSQL had some really nice abilities that MySQL is missing.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Doesn't Java require each class to be a separate file?

In Objective-C I often put multiple classes in the same file, especially if it's used within the class only.
 

ryan

macrumors 6502
May 17, 2002
283
0
Denver, CO
Doesn't Java require each class to be a separate file?

Not necessarily. You can't have two public classes in the same file but you can have something like the following in the same (Item.java) file:

Code:
public class Item {
   //...
}

class SubItem {
   //...
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.