Create a new class object through a function

This is a discussion on Create a new class object through a function within the C++ Programming forums, part of the General Programming Boards category; I want to know if it's possible. Basically, I want to give the user a choice of making a new ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    Create a new class object through a function

    I want to know if it's possible.

    Basically, I want to give the user a choice of making a new object that's part of a class. The user enters the choice and it should call a function "CreateObject()" which makes a new object and stores it in the heap "Class *Object = new Class".

    If anyone can lead me to some reference to explain this or explain it themselves it'd be greatly appreciated! I've been looking and I've found nothing so far.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Darkroman
    Basically, I want to give the user a choice of making a new object that's part of a class. The user enters the choice and it should call a function "CreateObject()" which makes a new object and stores it in the heap "Class *Object = new Class".
    I do not really understand what you are trying to do. What is the bigger picture of what you are trying to do?

    At the moment, the answer to your question is trivial:
    Code:
    if (choice == some_option)
    {
        Class *Object = new Class;
        // ...
    }
    But that's probably a misinterpretation of what you want.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Here's some code to give you an idea.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Critter
    {
    
    public:
        int year;
    };
    
    void CreateCritter()
    {
        Critter *crit = new Critter;
    
        cout << "You just created a critter!" << endl << endl;
        crit->year = 5;
    }
    
    int main()
    {
        int choice;
    
        do
        {
            cout << "0: Quit" << endl;
            cout << "1: Create Critter" << endl;
            cout << "2. Find out age" << endl;
            cout << "3. Release your critter back into the wild"
            cout << "Choice: ";
            cin >> choice;
    
            switch (choice)
            {
                case 0:
                cout << "Goodbye!" << endl;
                break;
    
                case 1:
                cout << endl;
                CreateCritter();
                break;
    
                case 2:
                cout << endl;
                GetAge();
    
                case 3:
                DeleteCritter();
    
                default:
                cout << endl;
                cout << "Sorry, but " << choice << " is not a valid choice!" << endl << endl;
            }
    
        } while (choice != 0);
    
        return 0;
    }
    Is there way to create an object of a class during runtime through a function, and be able to display the critter's age through a function without any scope issues during compile time?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Darkroman
    Is there way to create an object of a class during runtime through a function, and be able to display the critter's age through a function without any scope issues during compile time?
    Yes. You need to get your functions to take in arguments and return values, e.g., a pointer to Critter. You can check if the pointer is null to figure out if the user has a critter.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Any chance you could show me through some example code? It'd be greatly appreciated . I'm still new with c++ and even more recently, classes. I know somewhat about using pointers and/or references for functions, but this one stumps me!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Something like this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Critter
    {
    public:
        explicit Critter(int age) : year(age) {}
    
        int getAge() const
        {
            return year;
        }
    private:
        int year;
    };
    
    Critter* CreateCritter()
    {
        Critter* critter = new Critter(5);
        cout << "You just created a critter!";
        return critter;
    }
    
    void ReleaseCritter(Critter*& critter)
    {
        delete critter;
        critter = 0;
        cout << "You just released your critter!";
    }
    
    int main()
    {
        int choice;
        Critter* critter = 0;
        do
        {
            cout << "0: Quit\n"
                    "1: Create Critter\n"
                    "2. Find out age\n"
                    "3. Release your critter back into the wild\n"
                    "Choice: ";
            cin >> choice;
            cout << endl;
            switch (choice)
            {
            case 0:
                delete critter;
                critter = 0;
                cout << "Goodbye!";
                break;
    
            case 1:
                critter = CreateCritter();
                break;
            case 2:
                if (critter)
                {
                    cout << "Your critter is " << critter->getAge() << " years old!";
                }
                else
                {
                    cout << "You don't have a critter!";
                }
                break;
            case 3:
                if (critter)
                {
                    ReleaseCritter(critter);
                }
                else
                {
                    cout << "There's no critter to release!";
                }
                break;
            default:
                cout << "Sorry, but " << choice << " is not a valid choice!";
            }
            cout << '\n' << endl;
        } while (choice != 0);
    
        return 0;
    }
    Later, you will learn how to use smart pointers so you can make this less error prone, but notice that I added a delete critter; to case 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Awesome! Thank you so much! I did notice the delete critter to case 0, that's to free the memory, and setting the pointer to 0 makes it null (I am at least familiar with that). I made an additional condition to case 1 to check if there was already a critter made since (I believe) it wouldn't make sense to re-initialize the whole object. Actually I do have a question on that part. if I were to constantly choose 1 without checking if there was a critter already, would it overwrite the old critter, or just take up more space in the memory?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Darkroman
    I made an additional condition to case 1 to check if there was already a critter made since (I believe) it wouldn't make sense to re-initialize the whole object. Actually I do have a question on that part. if I were to constantly choose 1 without checking if there was a critter already, would it overwrite the old critter, or just take up more space in the memory?
    Good catch. If you did not check, you would get a memory leak.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Cool, good to know. I do have another question for the ReleaseCritter function. I haven't seen the use of a pointer and reference used together. I notice it compiles without & and when i run it, it doesn't delete the critter. It doesn't even compile if I just leave the & in there. Shouldn't it just leaving it as a pointer be well enough since it should delete the object seen through the pointer?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Darkroman
    I haven't seen the use of a pointer and reference used together. I notice it compiles without & and when i run it, it doesn't delete the critter. It doesn't even compile if I just leave the & in there. Shouldn't it just leaving it as a pointer be well enough since it should delete the object seen through the pointer?
    The idea is to pass the pointer by reference so that the pointer will be a null pointer in the caller too. If you pass the pointer by value, the critter is destroyed, but the pointer in the caller does not become a null pointer since the assignment only has an effect on the local pointer parameter.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-14-2011, 12:38 PM
  2. how to receive a class object in a function?
    By nyy601 in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2011, 12:55 AM
  3. how to receive a class object in a function?
    By nyy601 in forum C Programming
    Replies: 1
    Last Post: 02-26-2011, 12:07 AM
  4. Replies: 3
    Last Post: 01-22-2010, 09:24 PM
  5. Passing an array of a class object into a function
    By maoqiu in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2007, 08:42 AM

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