Thread: Help accessing classes and derived classes

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    12

    Help accessing classes and derived classes

    ok i have a program that has 1 base class and 3 derived classes, all appear to be workin fine, but is there any code that i could put in, that would ask the user what kind of "building" they want to build, and depending on what they put in for their answer, their answer would prompt them for the specs to put in to access the desired building(class)?

    N = for normal building(base class)
    H = for home (derived class)
    S = for school (derived class)
    B = for business (derived class)

  2. #2

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    hahaha ok, can someone give me a clue of what to start coding with?

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    In your base class write a virtual function and call it something like get_specs(). Use a base class pointer to create a derived object based on their entry then call get_specs() (which, of course, must be overridden by each derived class) and the corresponding function will prompt the user for input.

  5. #5
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Set up a union. If you don't know what that is, it's basically a structure that can store only one variable. However, this variable's type is flexible, based on how you define the union. Here's an example for your case...
    Code:
    typdef union MyUnion
     {
      normal N;
      home H;
      school S;
      business B;
     } building;
    The thing about unions is that you can only ever access one of its variables. Once you access one of them, you can't access any of the others. Now that we have the union set up, we can declare a building variable.
    Code:
    building MyBuilding;
    Use the user's input to decide which one of the variables you need to access.
    Code:
    void function(void)
     {
      function();
     }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't use a union. Use polymorphism, that's what it's for.

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You cannot use user-defined types in a union. Even if you could (and we may be able to in the next iteration of the C++ standard), there would be no reason to do so in this situation.

    Quote Originally Posted by Jaken Veina
    Once you access one of them, you can't access any of the others.
    This statement is untrue. You can interchangably access any of the elements within a union. The only special thing about a union is that they all share the same memory.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    12
    thanks for the input guys, im lookin at this book i have on C++ and reading on polymorphism and it's not very helpful for what i want to accomplish.

    is there code in C++ to use Select Case?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is a switch statment which is similar to select case.

    But in this case the reason you use inheritance is so you can take advantage of polymorphism. Do as LuckY said and you will learn about polymorphism and get into the good habit of using inheritance correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  2. Deleting derived classes
    By *DEAD* in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2008, 12:37 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Replies: 8
    Last Post: 07-27-2003, 01:52 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM