暂无描述

inh03.cpp 865B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // We have overloaded the display function of Rectangle
  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. Rectangle r;
  31. Rectangle q(4,5);
  32. r.display();
  33. q.display();
  34. cout << "Perimeter of q is: " << q.perimeter() << endl;
  35. return 0;
  36. }