#include #include using namespace std; class shape{ protected: double area; public: shape():area(-1.0){} shape(double a):area(a){} // pure virtual - makes abstract class //virtual void print()=0; virtual void print(){cout << "SHAPE, area = " << area << endl;} }; class wire: public shape{ public: int x1, y1, x2, y2; wire():shape(-3){} wire(double ar, int a, int b, int c, int d): shape(ar), x1(a), y1(b), x2(c), y2(d){} void print(){ cout << "WIRE area " << area << endl; cout << " x1 " << x1 << " y1 " << y1 << endl; cout << " x2 " << x2 << " y2 " << y2 << endl; } }; class transistor: public shape{ public: int x1, y1, x2, y2; string type; transistor():shape(-2),x1(0),y1(0),x2(0),y2(0){} transistor(double r, int a, int b, int c, int d):shape(r),x1(a),y1(b),x2(c),y2(d){} // redefined print void print(){ cout << "TRANSISTOR, area = " << area << endl; cout << " x1 " << x1 << " y1 " << y1 << endl; cout << " x2 " << x2 << " y2 " << y2 << endl; } }; void main(){ // makes some shapes, wire and transistors and print shape s(3); transistor t1, t2(8.0, 0, 0, 2, 4); s.print(); t1.print(); t2.print(); wire w(16,0,0,2,8); w.print(); // do an array of objects shape sa[4]; sa[0] = s; sa[1] = t1; sa[2] = t2; sa[3] = w; // see slicing in action! cout << endl << "sliced array " << endl; for(int i = 0; i < 4; i++) sa[i].print(); // polymorphism shape *sp[4];// array of pointers sp[0] = &s; sp[1] = &t1; sp[2] = &t2; sp[3] = &w; cout << endl << "polymorphic print " << endl; for(int i = 0; i < 4; i++) sp[i]->print(); }