1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #include <iostream>
- using namespace std;
-
- class Shape {
- protected:
- int width, height;
- public:
- Shape() { width = height = 0; }
- Shape(int w, int h) { width = w; height = h; }
- virtual void display() const;
- };
-
- void Shape::display() const {
- cout << width << " " << height << endl;
- }
-
- class Rectangle : public Shape {
- public:
- Rectangle() {}
- Rectangle(int w, int h) : Shape(w, h) { }
- int perimeter() const;
- void display() const;
- };
-
- int Rectangle::perimeter() const{
- return 2 * width + 2 * height;
- }
-
- void Rectangle::display() const {
- cout << "I am rectangle of width " << width
- << " and height " << height << endl;
- }
-
- class Triangle: public Shape {
- public:
- Triangle() {}
- Triangle(int w, int h) : Shape(w,h) {}
- void display() const;
- };
-
- void Triangle::display() const {
- cout << "I am triangle of side " << width << endl;
- }
-
-
- int main() {
- Shape* a[4];
- a[0] = new Rectangle(3,4);
- a[1] = new Triangle(5,7);
- a[2] = new Rectangle(10,5);
- a[3] = new Triangle(2,8);
-
- for (auto e: a) {
- e->display();
- }
- }
|