Topic : Runtime Polymorphism
Author : Donovan Rebbechi
Page : << Previous 2  
Go to page :


     |/========//   |   \n"
            <<  "     |//==/--       |   \n"
            <<  "     L______________J   \n"
            << std:: endl;
    }
    virtual ~Square(){}
};


int main()
{
    int choice = 0;
    Shape* s;
    std::list<Shape*> shapes;
    do {
        std::cout << "Pick a shape by entering a number from 1 to 3: \n"
            << "(1) Square\n"
            << "(2) Triangle\n"
            << "(3) Circle\n"
            << "(4) Quit" << std::endl;
        
        switch ( choice ) {
            case 1:
                s = new Square;
                break;
            case 2:
                s = new Triangle;
                break;
            case 3:
                s = new Circle;
                break;
            default:
                s = 0;
        } // switch

        if (s) shapes.push_back(s);
    }
    while ( cin >> choice && choice != 4 );

    std::list<Shape*>::const_iterator it;

    if ( ! shapes.empty() )
        for ( it = shapes.begin(); it != shapes.end(); ++it )
        {
            (*it)->draw();
            delete (*it);
        }

    return 0;


}


Page : << Previous 2