Generic OOP question

Discussion in 'Mac Programming' started by Cromulent, Mar 5, 2009.

  1. Cromulent
    Expand Collapse
    macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #1
    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.
     
  2. pstoehr
    Expand Collapse
    macrumors member

    Joined:
    Aug 5, 2003
    Location:
    Scheßlitz, Oberfranken, Bavaria, Germany
  3. Cromulent
    Expand Collapse
    thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    Cheers. Programming convention for OOP is quite different to how I did things in C.
     
  4. lazydog
    Expand Collapse
    macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4
    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
     
  5. Cromulent
    Expand Collapse
    thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    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.
     
  6. lazydog
    Expand Collapse
    macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    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
     
  7. trule
    Expand Collapse
    macrumors 6502

    Joined:
    Mar 16, 2007
    #7
    PHP does not have all C/C++'s hangups so require_once() normally does the trick.
     
  8. Cromulent
    Expand Collapse
    thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #8
    :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.
     
  9. kainjow
    Expand Collapse
    Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    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.
     
  10. Cromulent
    Expand Collapse
    thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #10
    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.
     
  11. ryan
    Expand Collapse
    macrumors 6502

    Joined:
    May 17, 2002
    Location:
    Denver, CO
    #11
    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 {
       //...
    }
    
     

Share This Page