// ken moore 9%24%03 // demonstrate polymorphism #include using namespace std; #include "shapes.h" #include "rect.h" #include "circle.h" void main(){ // make a shape shapes s; cout << s.getArea() << endl; // output shape area s.print(); // make a rect rect r; cout << r.getArea() << endl;// output rect area r.print();// call print function in rect s = r; s.print(); // does not work due to slicing // goes to shape version of print // ---------- do pointer version --------- shapes *s2 = new shapes();//must use pointer notation cout << "s2->getArea() " << s2->getArea() << endl; s2->print(); rect *r2 = new rect(2,4,4,8); cout << r2->getArea() << endl; r2->print();//call rectangle print circle *c = new circle(10,10,1); c->print(); // demonstrate polymorphism // must have virtual print in shapes // must use pointers shapes *s3[3]; s3[0] = r2; s3[1] = c; s3[2] = &s; cout << "printing array of shapes " << endl; for(int i=0; i<3; i++) s3[i]->print(); delete s2; delete r2; delete c; }