CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Jan 2009
    Posts
    16

    Lightbulb Can I create classes interactively?

    I mean I was asking myself whether I could create classes like for example...

    Instead of having to declare a limited number of classes and declare them specifically with names in the code, the user could say, I would like to create a class and I would like it to have this name... Something like this pseudocode...

    Program asks if the user wants to create a class.
    User confirms.
    The program asks about the name of the class.
    The program creates name.class.

    Also, is there anyway to create a number of classes with a for cycle? I mean I was trying to do an exercise with student files. So my idea is for example...

    The program asks the user how many files do you want to create and it creates the same amount of classes and after that the user could edit a specific class by inputting its number or name. The one thing I'm almost sure won't work is classes named with numbers only.

    So, is that possible or?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Can I create classes interactively?

    Classes in C++ are AL declared and defined at compile time.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Can I create classes interactively?

    The program creates name.class.
    What do you mean with 'create' ? Allocate memory for a existing class ? Simply create it on the heap with the 'new' command. Or do you mean actually create a new, non-existing class out of nothing ?
    Last edited by Skizmo; January 7th, 2009 at 01:45 PM.

  4. #4
    Join Date
    Jan 2009
    Posts
    16

    Question Re: Can I create classes interactively?

    I mean, I have my class declaration for example:

    Code:
    class Number{
    
    public:
        Number();
        void getNumber();
    
    private:
        int number;
    
    };
    And in main I don't specify like

    Number A;

    rather than ask the user to name the class with a string for example:

    string name;

    Number name;

    or sort of, I'm not really sure if it can be done in C++.

    ---

    Btw, how would the for cycle with new class work? In a code example

  5. #5
    Join Date
    Jan 2009
    Posts
    16

    Re: Can I create classes interactively?

    Quote Originally Posted by TheCPUWizard View Post
    Classes in C++ are AL declared and defined at compile time.
    So, technically the first thing I am asking is impossible to do in C++?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Can I create classes interactively?

    The reason why this is a strange question is because the name of an object isn't terribly relevant at runtime; it's merely a way to refer to it in the code. Once the code is compiled the name becomes irrelevant.

    However, you might be able to do what you want using a std::map<std::string, Number>.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Can I create classes interactively?

    Quote Originally Posted by gtbgmaniak View Post
    So, technically the first thing I am asking is impossible to do in C++?
    No, you're just not asking your question correctly.

    You don't want to interactively create classes, you want to dynamically create objects. That is most definitely possible in C++.

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Can I create classes interactively?

    Quote Originally Posted by gtbgmaniak View Post
    So, technically the first thing I am asking is impossible to do in C++?
    Which is EXACTLY why you need to....

    1) Get a GOOD Book (the kind that is printed on PAPER)
    2) Read every word, starting at page 1
    3) Type in EVERY piece of code (even if there is a CD/DVD/Download - manually type it in)
    4) Step through every line of code with the debugger (yes, even the "hello world" sample.
    5) Do not move to the next chapter/section until you are 100% confident you understand EVERYTHING in the current chapter (slap yourself if you ever think "I dont need to know this specific aspect").

    Ad-hoc learning does NOT work for C++. It is the (probably) most detail oriented of all of the computer programming languages. A small gap in fundamental knowledge can quickly lead to fatal flaws that are virtually impossible to later overcome.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: Can I create classes interactively?

    Quote Originally Posted by gtbgmaniak View Post
    rather than ask the user to name the class with a string for example:

    string name;

    Number name;
    This kind of declaration generates error C2371.
    I kindly suggest you, please follow TheCPUWizard's advices. When you will understand C++ languages and you will practices it, you will find yourself the answer of many questions like this.

  10. #10
    Join Date
    Jan 2009
    Posts
    16

    Talking Re: Can I create classes interactively?

    Quote Originally Posted by TheCPUWizard View Post
    1) Get a GOOD Book (the kind that is printed on PAPER)
    Would you suggest me a good one? I'm reading Deitel's How to program in C++, I don't know if it's really good, but sometimes I feel the topics aren't ordered very well. Anyway, I'm still a beginner, so I would accept the professionals' advice.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Can I create classes interactively?

    Quote Originally Posted by gtbgmaniak
    Would you suggest me a good one?
    I am still a little concerned that the material really is too accelerated for a rank beginner, but other than that the book I would recommend to you is Accelerated C++: Practical Programming by Example by Andrew Koenig and Barbara E. Moo.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: Can I create classes interactively?

    Choose from here or Thinking in C++ (a very good book for beginers and medium programmers).
    Last edited by Maximus_X; January 8th, 2009 at 01:41 PM.

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Can I create classes interactively?

    Quote Originally Posted by gtbgmaniak View Post
    Would you suggest me a good one? I'm reading Deitel's How to program in C++, I don't know if it's really good, but sometimes I feel the topics aren't ordered very well. Anyway, I'm still a beginner, so I would accept the professionals' advice.
    The first thing I would suggest is a search here on CodeGuru, this topic has been discussed many time.

    I am always hesitant to recommend books in general. There are two criteria a book must meet to be "good":

    1) It must be accurate and current.
    2) It must be written in a style that is appropriate for the reader.

    As an example, I just LOVE the Scott Meyers books (they are more advanced that you are right now). I find them very enjoyable to read, with "funny" one-liners and sometimes very scarcastic comments. Other people find that distracting, and prefer a "dryer" style.

    After you do the search that I recommend (and many of the senior people here have posted good reasons NOT to use specific books - I say do not use anything published before 2004), then (if at all possible) go to a bookstore and spend a couple of hours reading the first part of each book that made the "recommend list". See which one is best suited to YOUR style of learning.

    This is the ONE place where a person doing independant study has a real opportunity that students in a formal classroom do not. They (almost always) MUST use the textbook that the teacher/professor dictates.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    Join Date
    Jan 2009
    Posts
    16

    Re: Can I create classes interactively?

    Well, actually, that's my problem, I'm a student and they tell us to use the Deitel book, maybe because the university has some contract with the editorial, I don't know, but anyway, thanks for the advice, I'll see where to start from.

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Can I create classes interactively?

    NOTE: The following post contains a personal bias....

    One book to consider is "SAMS Teach Yourself C++ in 21 Day" or the newer "SAMS Teach Yourself C++ in an Hour a Day".

    While I have a disagreement with the time scale, I am of the opinion that that the written material is quite readable, well organized, and covers the most important topics in learning C++.

    I can also stand behind the 5th and 6th editions of "SAMS Teach Yourself C++ in 21 Day" in terms of confiormance of the code samples with the ISO specification since I was the Technical Editor (ooops...now anwone can have a 50% chance of knowing my real name ) responsible for ISO compliance testing.

    I also know the Authors, which adds to the personal (positive) bias.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured