No Description

inh04.cpp 862B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Trying to invoke the display function from a pointer.
  2. #include <iostream>
  3. using namespace std;
  4. class Shape {
  5. protected:
  6. int width, height;
  7. public:
  8. Shape() { width = height = 0; }
  9. Shape(int w, int h) { width = w; height = h; }
  10. void display() const;
  11. };
  12. void Shape::display() const {
  13. cout << width << " " << height << endl;
  14. }
  15. class Rectangle : public Shape {
  16. public:
  17. Rectangle() {}
  18. Rectangle(int w, int h) : Shape(w, h) { }
  19. int perimeter() const;
  20. void display() const;
  21. };
  22. int Rectangle::perimeter() const{
  23. return 2 * width + 2 * height;
  24. }
  25. void Rectangle::display() const {
  26. cout << "I am rectangle of width " << width
  27. << " and height " << height << endl;
  28. }
  29. int main() {
  30. Shape *p;
  31. Shape *s;
  32. p = new Rectangle(4,8);
  33. s = new Shape(3,5);
  34. s->display();
  35. p->display();
  36. return 0;
  37. }