PDA

View Full Version : Generic OOP question




Cromulent
Mar 5, 2009, 04:06 AM
Say I have two classes:

class A
{
// stuff
}

and

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.



pstoehr
Mar 5, 2009, 04:23 AM
Separate files

Cromulent
Mar 5, 2009, 04:25 AM
Separate files

Cheers. Programming convention for OOP is quite different to how I did things in C.

lazydog
Mar 5, 2009, 05:08 AM
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
Mar 5, 2009, 06:05 AM
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.

lazydog
Mar 5, 2009, 06:15 AM
I am actually using PHP and not C++.

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

trule
Mar 5, 2009, 06:33 AM
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
Mar 5, 2009, 06:41 AM
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
Mar 5, 2009, 08:51 AM
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.

Cromulent
Mar 5, 2009, 09:17 AM
Doesn't Java require each class to be a separate file?

Yeah, but as this is PHP and it is a class that extends another one I wasn't sure if that was just something particular to Java or not.

ryan
Mar 5, 2009, 11:56 AM
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:


public class Item {
//...
}

class SubItem {
//...
}