1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- // Trying to invoke the display function from a pointer.
-
- #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; }
- 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;
- }
-
- int main() {
-
- Shape *p;
- Shape *s;
- p = new Rectangle(4,8);
- s = new Shape(3,5);
-
- s->display();
- p->display();
-
- return 0;
- }
|