Question and Hello

This is a discussion on Question and Hello within the C++ Programming forums, part of the General Programming Boards category; Hi I am new here and to C++. I am practicising with C++ to improve and am working with a ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    Question Question and Hello

    Hi I am new here and to C++. I am practicising with C++ to improve and am working with a class and was curious if there was a way to make the name of the class an input string. I will apologize incase this is answered elsewhere but would sincerely appreciate any help or a place to find the answer.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Welcome to the boards!

    As to your question, I'm not sure if I understand completely what you're trying to do here.....are you trying to get the input from a user and create a class with that name? or something else?

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    Thanks for the welcome.
    And yes that is what I am trying to do.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    hmm if your trying to create a class object or define an instance of a class with the name the user inputed I would say no. Somone can correct me if I'm wrong but I think unless you are exporting classes from a DLL or have included debugging symbols in your exe, the exe dosen't contain the names of the classes and even dlls can only export the memory where a class is stored not the actual defination.
    When you reference the name of the class or any data the program just uses the address of the class and dosen't know what it's name is.

    PS Welcome to the board.

  5. #5
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    I Don't understand why you would want to do that. Is there a specific reason?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    your question is still a bit ambiguous to me

    is this what you want?

    Code:
    #include <ofstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	ofstream out("MyClass.h", ios_base::binary);
    	string classname = "";
    		
    
    	if(!out)
    		return -1;
    
    	out << "#ifndef MYCLASS_H\n#defineMYCLASS_H\n\n\n";
    
    	cout << "Enter class name>";
    	cin >> classname;	
    
    	out << "class " << classname << "\n{\n};\n";
    	out << "\n#endif\n\r\n\r";
    
    	return 0;
    }

    this will cretae a file called myclass.h
    that looks like
    Code:
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    class <user supplied class name>
    {
    };
    
    #endif
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    You can do that with a series of if statements:

    string className;
    cout<<"enter a class name: ";
    cin>>className;

    if(className == "Apple") //some statements

    else if(className == "Cat") //some statements

    else if(className == "Dog") //some statements

    But, it's hard to tell you what you are going to need inside those if statements without some more detail as to what you want to do. I suspect it will involve pointers and dynamically allocating memory, since variables declared inside an if statement are local variables and they will be destroyed when the if statement ends. I also see a problem with knowing which if branch executed, so in your program how will you know which pointer to use?
    Last edited by 7stud; 04-06-2005 at 11:28 PM.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    Maybe I am not clearly explaining it so I will try my best to show it.
    char name [26];
    ...
    cin.getline ( name, 26 );
    class name;
    except that I want the class to actually be called the string in the character array not name.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Quantum1024
    hmm if your trying to create a class object or define an instance of a class with the name the user inputed I would say no. Somone can correct me if I'm wrong but I think unless you are exporting classes from a DLL or have included debugging symbols in your exe, the exe dosen't contain the names of the classes and even dlls can only export the memory where a class is stored not the actual defination.
    When you reference the name of the class or any data the program just uses the address of the class and dosen't know what it's name is.

    PS Welcome to the board.

    research RTTI and keyword typeid.

    i'm not sure you can necessarily reference a class, or should i say, make reference to a class simply from a string, you could however use the STL map container to reference via string.

    of course, i see no good reason to do this though.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  10. #10
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    of course, i see no good reason to do this though
    Yeah that's my biggest question why does it matter what the name of the class. If a user is using a program he doesn't need to know the details like the name of a variable or a class. I mean maybe you would want to make a program that creates a file with the class shell with the users inputed name in it but that would just be writing to a file. Which is what misplaced did earlier.
    Last edited by stumpster123; 04-06-2005 at 11:30 PM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by sal880612m
    Maybe I am not clearly explaining it so I will try my best to show it.
    char name [26];
    ...
    cin.getline ( name, 26 );
    class name;
    except that I want the class to actually be called the string in the character array not name.
    Let's just assume for a moment that you could do exactly that. What would your program do next? Post a few lines of code that you envision having after those statements.

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    It's entirely possible that that is exactly what I want to do, I am new to C++ and trying to practice but maybe for what I am trying to do I need to expand what I can do first. Thanks for the help and I will look deeper into writing/reading files. Also if you know any really good sites for tutorials for a begginer a link or web adress would help me out quite abit. Sorry if this was a really stupid question.

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    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.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Yes, that's one problem--you are new to C++. But, I suspect you have a major flaw in your understanding of classes. Suppose, the user entered 'Googaplex' for the string. And, then you did this:

    class Googaplex;

    What is your understanding about what that does?

  15. #15
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    This is a good beginner tutorial it has basics on classes and file I/O

    Tutorial

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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