Aucune description

inh06.cpp 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <iostream>
  2. using namespace std;
  3. class Shape {
  4. protected:
  5. int width, height;
  6. public:
  7. Shape() { width = height = 0; }
  8. Shape(int w, int h) { width = w; height = h; }
  9. virtual void display() const;
  10. };
  11. void Shape::display() const {
  12. cout << width << " " << height << endl;
  13. }
  14. class Rectangle : public Shape {
  15. public:
  16. Rectangle() {}
  17. Rectangle(int w, int h) : Shape(w, h) { }
  18. int perimeter() const;
  19. void display() const;
  20. };
  21. int Rectangle::perimeter() const{
  22. return 2 * width + 2 * height;
  23. }
  24. void Rectangle::display() const {
  25. cout << "I am rectangle of width " << width
  26. << " and height " << height << endl;
  27. }
  28. class Triangle: public Shape {
  29. public:
  30. Triangle() {}
  31. Triangle(int w, int h) : Shape(w,h) {}
  32. void display() const;
  33. };
  34. void Triangle::display() const {
  35. cout << "I am triangle of side " << width << endl;
  36. }
  37. int main() {
  38. Shape* a[4];
  39. a[0] = new Rectangle(3,4);
  40. a[1] = new Triangle(5,7);
  41. a[2] = new Rectangle(10,5);
  42. a[3] = new Triangle(2,8);
  43. for (auto e: a) {
  44. e->display();
  45. }
  46. }