Thread: Question and Hello

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by sal880612m
    It was mostly about accessing the information and as I stated at the top I am new to this and while I have read a lot of the tutorials and such, implementing them is largely still beyond me.
    If I were you, I would not start out learning to program a class with any user interaction. Get the basics down first, and then when you have a good grasp of classes, then you can start interacting with the user. Here is an example of a simple class:
    Code:
    include <iostream>
    using namespace std;
    
    class Apple
    {
    public:
    
    	int size;
    
    };
    
    int main()
    {
    	Apple myApple;
    	myApple.size = 10;
    
    	cout<<myApple.size<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 04-06-2005 at 10:47 PM.

  2. #17
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    say you created a program call a.exe. if you want to use a class that you compiled when you compiled a.exe then you will first need to enable your compiler for for RTTI (run-time type id). this is compiler dependant on how you go about this. also, i'm not sure if it's a standard feature. you would still need to do some thing like, if(string == typeid(MyClass)) then use MyClass. i don't see how this could be very useful.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #18
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    It would create an instance of whatever class is, called Googaplex or it would create a class called Googaplex. It all depends on what you were refering to when you used class.
    Stumpster123 thanks for the link I will check it out.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by sal880612m
    It would create an instance of whatever class is, called Googaplex or it would create a class called Googaplex. It all depends on what you were referring to when you used class.
    Stumpster123 thanks for the link I will check it out.
    To create an instance of the Googaplex class, you would already have to have declared and defined a class called Googaplex in your program. The declaration and definition would look similar to the Apple class in my previous post. In other words, you can't create objects of a class unless the class has been previously declared and defined in your program.

    In the code I posted, I can declare an object of type Apple because higher up on the page somewhere the Apple class has been declared and defined. It is identical to this:

    int number;
    number = 100;

    I can assign 100 to number because number has been declared higher up on the page. What you are trying to do is this:

    another_number = 200;

    Well, the C++ compiler looks up the page and it doesn't see another_number declared anywhere, so you it would give you an error. The same thing happens with class Googaplex. If you try to create an instance of Googaplex, then the C++ compiler looks up the page and if there isn't a Googaplex class declared somewhere higher up on the page, your program won't work.
    Last edited by 7stud; 04-06-2005 at 10:59 PM.

  5. #20
    Registered User
    Join Date
    May 2003
    Posts
    82
    You could make a class which knows it's name is a user-entered string. ie it could output, change, and compare it's "name."

    I'm going to have to join the people who are very confused about why you want to do this. It just looks like you're trying to do something and picking the wrong tool.

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I suggest you get a book like:

    1) C++: A beginner's guide(Herbert Schildt)

    2) Sam's Teach Yourself C++ in 21 Days.

  7. #22
    Registered User
    Join Date
    May 2003
    Posts
    82
    well if it's time for that conversation...

    Accelerated C++ by Koenig and Moo is a great book, especially if you have past programming experience and want to start using the powerful tools C++ offers quickly.

  8. #23
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Not sure if anyone's pointed this out already or not, as skimming through the posts it appears most of them are just reiterating the same thing over and over

    Anyway, there is a method called polymorphism which can be used in this situation, though not very stylistically/programmatically appreciated.

    Say you had a set of classes: apple, carrot, pizza, etc... and they're all food. So you have a base class called the "food" class:

    Code:
    class food
    {
    //implementation with virtual functions and etc...
        virtual void doFoodStuff()=0;
    };
    This class can then be "inherited" by your food classes to create the children classes:

    Code:
    class apple : public food
    {
    //Now you actually implement the food functions
        void doFoodStuff();
    };
    
    void apple::doFoodStuff()
    {
    // stuff
    }
    And then, say you wanted the user to input what type of food they want to interact with, you could do the following:

    Code:
    <grab user input for a name>
    
    food* foodItem=0
    
    if(!strcmp(foodname,"apple"))
       foodItem=new apple();
    else if(<etc>)
    else
       // error
    
    foodItem->doFoodStuff(); // This will now execute whichever item of food's function that the user asked for
    Polymorphism is a very fun concept. This, however, is only one solution to the problem at hand, and another (possibly) better solution would be to have a class behave much like a relational array, where when you create the class you give it a name:

    Code:
    class myCustomClass
    {
    private:
        char** classList;
    public:
        int createNewClass(char* className);
    };
    Then the implementation of the constructor function would associate the class with a new id in the classList array. Then from that point on, you could have an associative array (overloaded of course) so that whenever you do [myClass] where myClass is a char* it will automatically pull the class's variables that you're looking for.

    Hopefully this hasn't been a bit too over-the-top and you can get something from it. See my article on Module Design for Game Development and also the discussions following it for more information on polymorphism.
    Last edited by jverkoey; 04-07-2005 at 12:52 PM. Reason: Yummy syntax colorizing!

Popular pages Recent additions subscribe to a feed