Defining classes following user input. Inheritance.

This is a discussion on Defining classes following user input. Inheritance. within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm writing a program and part of the specification requires the users to be able to add new objects. ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Defining classes following user input. Inheritance.

    Hi,

    I'm writing a program and part of the specification requires the users to be able to add new objects.

    I've made a little diagram which is attached:


    My question is how to gather the information from the user.

    Should I first ask the user for the attributes for the Vehicles base class, then make the last question 'Is it a Car or a Bike?', gather all the details for either the car or the bike, then again make the last question is it a Sports Bike or a Trails bike, and then ask for the attributes of this derived class?

    Would this method work OK?

    I've never used inheritance before, so am unsure on the best approach to take.

    Many thanks for any advice! It's most welcome
    Attached Images Attached Images  

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Assuming you have a finite number of classes, my approach would be to have a list of all possible answers, not make a binary tree question approach. The user PROBABLY knows exactly what he/she is trying to achieve, and do not want to answer 20 questions style.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Yes, there is a limited number of classes, but I'm not sure what you mean matsp.

    each of the classes have other attributes to them, for example cars has MPG and Number of Doors, so these will need to be asked.

    Sorry.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Something like this:

    Code:
    class VehicleBase
    {
    public:
        void GetAttributesFromUser();
    
    protected:
        void AddRequiredAttribute(const std::string &attribName, AttributeType attribType);
    
        std::vector<std::pair<std::string, AttributeType> > requiredAttributes;
    };
    
    class Car : public VehicleBase
    {
    public:
        Car()
        {
            AddRequiredAttribute("numberOfDoors", AttributeInteger);
            AddRequiredAttribute("typeOfFuel", AttributeString);
            ...
        }
    };
    That implies a whole lot of surrounding code, but does it communicate the idea?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks brewbuck, but afraid not.

    First I plan to ask the user to enter the attributes for the base class (vehicles).

    Once the user has answered these questions, I plan to lastly ask 'Is it a Car or a Bike?'.

    If say it is a Car, I shall then ask them to enter the attributes for the Car class, which is an inherited class from the Vehicles class.

    Once they have answered these questions, I shall again make the last question 'Is it a hard top or a Soft Top?', and then enter the attributes for whichever type it is.



    So once the process is over, a new vehicle will of been created, which I will add to the database.

    What I don't understand is if this way is best.

    As matsp suggested top have a list of all possible answers, and users would not want '20 questions', the way I would describe that is like an Ajax application, the next set of questions would depend on the previous set of answers.

    Rather confused now.

    Sorry for having to explain my question is a high level way, but that's how I need to try and understand it.
    Last edited by Swerve; 03-16-2009 at 02:16 PM.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    I could really do with some advice on this chaps.

    If my way will work then I'm going to go with that, I was asking because since it's the core of the program, it would be nice to do it well, and I doubt my plan is best.

    Thanks

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My suggestion would be something like this:
    Code:
    char *choices[] = 
    {
       "Motorbike - Sport",
       "Motorbike - Trail",
       "Car - Hard Top",
       "Car - Soft Top",
    };
    Then display this list, and ask for a number (or letter) corresponding to each choice.
    Once you have that, you do some switch/if-statement(s) to create the right type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Preferably,
    Code:
    const char* choices[] = 
    {
       "Motorbike - Sport",
       "Motorbike - Trail",
       "Car - Hard Top",
       "Car - Soft Top",
    };
    Because string literals are generally unmodifiable.
    But since this is C++, I'd just do
    Code:
    const std::string choices[] = 
    {
       "Motorbike - Sport",
       "Motorbike - Trail",
       "Car - Hard Top",
       "Car - Soft Top",
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  3. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 09:23 AM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21