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.