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?
Re: Can I create classes interactively?
Classes in C++ are AL declared and defined at compile time.
Re: Can I create classes interactively?
Quote:
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 ?
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
Re: Can I create classes interactively?
Quote:
Originally Posted by
TheCPUWizard
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++?
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>.
Re: Can I create classes interactively?
Quote:
Originally Posted by
gtbgmaniak
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++.
Re: Can I create classes interactively?
Quote:
Originally Posted by
gtbgmaniak
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.
Re: Can I create classes interactively?
Quote:
Originally Posted by
gtbgmaniak
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.
Re: Can I create classes interactively?
Quote:
Originally Posted by
TheCPUWizard
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.
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.
Re: Can I create classes interactively?
Choose from here or Thinking in C++ (a very good book for beginers and medium programmers).
Re: Can I create classes interactively?
Quote:
Originally Posted by
gtbgmaniak
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.
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.
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 ;) :D) responsible for ISO compliance testing.
I also know the Authors, which adds to the personal (positive) bias.