How to enable a choice for what type of object to create?

This is a discussion on How to enable a choice for what type of object to create? within the C++ Programming forums, part of the General Programming Boards category; I am having trouble with how to go about implementing a way for there to be a choice of what ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    32

    How to enable a choice for what type of object to create?

    I am having trouble with how to go about implementing a way for there to be a choice of what kind of object to create from the two derived classes. I have tried something like this, but it gives me two errors.

    error C2065: 'makeobject' : undeclared identifier
    error C2228: left of '.display' must have class/struct/union type

    Code:
    #include <iostream>
    
    class first
    {
    protected:
    	int variable1;
    	int variable2;
    };
    
    class foo : public first
    {
    private:
    	int variable3;
    
    public:
    	void display()
    	{
    	std::cout << variable1 << " " << variable2 << " " << variable3 << std::endl;
    	}
    };
    
    class test : public first
    {
    private:
    	int variable4;
    
    public:
    	void display()
    	{
    	std::cout << variable1 << " " << variable2 << " " << variable4 << std::endl;
    	}
    };
    
    int main()
    {
    	std::cout << "Enter 1 for class foo and 2 for class test. " << std::endl;
    	int answer = 0;
    	std::cin >> answer;
    	if(answer == 1)
    		foo makeobject;
    	if(answer == 2)
    		test makeobject;
    	makeobject.display();
    	return 0;
    }
    I want to be able to choose which object to create.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You have a scope problem. Variables declared in a block are destroyed once the block ends. Your if statements are a short hand notation for:
    Code:
    if(answer == 1)
    {
    	foo makeobject;
    }
    if(answer == 2)
    {
    	test makeobject;
    }
    Once a block ends, the objects declared therein cease to exist(unless the objects are dynamically allocated memory with the new operator, in which case they stay in existence until you delete them).

    Also, you should consider how protected/private members are assigned values in a class.
    Last edited by 7stud; 03-08-2005 at 01:58 PM.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by 7stud
    You have a scope problem. Variables declared in a block are destroyed once the block ends. Your if statements are a short hand notation for:
    Code:
    if(answer == 1)
    {
        foo makeobject;
    }
    if(answer == 2)
    {
        test makeobject;
    }
    Once a block ends, the objects declared therein cease to exist(unless the objects are dynamically allocated memory with the new operator, in which case they stay in existence until you delete them).
    That should do the trick I had not considered using new and delete.

    Quote Originally Posted by 7stud
    Also, you should consider how protected/private members are assigned values in a class.
    Thanks for the concern (not being sarcastic but sincere). It was just a quick example to reproduce my problem so I neglected to include any constructors. If this is not in regard to the constructor can you please explain?

    Thank you very much for taking the time to help me 7stud.
    Last edited by Anubis; 03-08-2005 at 06:20 PM.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If memory serves me correctly, you can do something like this:
    Code:
    int main()
    {
           first* makeobject;
    	std::cout << "Enter 1 for class foo and 2 for class test. " << std::endl;
    	int answer = 0;
    	std::cin >> answer;
    	if(answer == 1)
    		makeobject = new foo;
    	if(answer == 2)
    		makeobject = new test;
    	makeobject.display();
                    delete makeobject;
    	return 0;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Whooooo guys, you've all overlooked the fact that first has no display() function
    Revised:
    Code:
    #include <iostream>
    
    class first
    {
    public:
    	virtual void display();
    
    protected:
    	int variable1;
    	int variable2;
    };
    
    class foo : public first
    {
    private:
    	int variable3;
    
    public:
    	void display()
    	{
    	std::cout << variable1 << " " << variable2 << " " << variable3 << std::endl;
    	}
    };
    
    class test : public first
    {
    private:
    	int variable4;
    
    public:
    	void display()
    	{
    	std::cout << variable1 << " " << variable2 << " " << variable4 << std::endl;
    	}
    };
    
    int main()
    {
    	std::cout << "Enter 1 for class foo and 2 for class test. " << std::endl;
    	int answer = 0;
    	std::cin >> answer;
    
    	first* objectPtr;
    	if(answer == 1)
    		objectPtr = new foo;
    	else if(answer == 2)
    		objectPtr = new test;
    
    	objectPtr->display();
    	delete objectPtr;
    	return 0;
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by JaWiB
    If memory serves me correctly, you can do something like this:
    Yes that is what I had in mind thank you.

    Quote Originally Posted by Hunter2
    Whooooo guys, you've all overlooked the fact that first has no display() function
    Thank you very much Hunter2.


    I want to thank everyone for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Changing an objects Type
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2004, 07:18 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Creating object of type HWND from a dll
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-12-2002, 11:40 PM

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