Нет описания

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // We have added a new member function (perimeter) to the
  2. // subclass Rectangle. Subclasses can have many more data members
  3. // and member functions besides the ones they inherit from their
  4. // base class.
  5. #include <iostream>
  6. using namespace std;
  7. class Shape {
  8. protected:
  9. int width, height;
  10. public:
  11. Shape() { width = height = 0; }
  12. Shape(int w, int h) { width = w; height = h; }
  13. void display() const;
  14. };
  15. void Shape::display() const {
  16. cout << width << " " << height << endl;
  17. }
  18. class Rectangle : public Shape {
  19. public:
  20. Rectangle() {}
  21. Rectangle(int w, int h) : Shape(w, h) { }
  22. int perimeter() const;
  23. };
  24. int Rectangle::perimeter() const{
  25. return 2 * width + 2 * height;
  26. }
  27. int main() {
  28. Rectangle r;
  29. Rectangle q(4,5);
  30. r.display();
  31. q.display();
  32. cout << "Perimeter of q is: " << q.perimeter() << endl;
  33. return 0;
  34. }