How do I let the user choose between two classes at runtime?

Howdy. I know the title is somewhat vague, and I apologize. In my CSC 265 course, I'm making a program that uses a List class and a Set class. Both classes are identical (the Set class is derived from the List class, but has no additional features), but my professor wants the user to be able to choose which to use at the beginning (to show that we understand inheritance). My problem is, how do I let them choose? I tried using:
1
2
3
4
if (choice == 1){
     Set list;}
else if (choice == 2){
     List list;}


But upon compiling, it told me that "list" was undefined. Any ideas?
Think about the scope of those variables. Let's assume that choice == 1. list is created as a Set. Its scope ends when the scope of the if statement ends, which is the closing curly brace immediately after it.

I suspect your professor wants you to be allocating them dynamically, but it's at least the best way that I can think of to do this.

http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
Topic archived. No new replies allowed.