fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class CPolygon {
  5. protected:
  6. int width, height;
  7. public:
  8. void set_values (int a, int b) {
  9. width = a;
  10. height = b;
  11. }
  12. };
  13.  
  14. class CRectangle : public CPolygon {
  15. public:
  16. int area () {
  17. return (width * height);
  18. }
  19. };
  20.  
  21. class CTriangle : public CPolygon {
  22. public:
  23. int area () {
  24. return (width * height / 2);
  25. }
  26. };
  27.  
  28. int main() {
  29. CRectangle rect;
  30. CTriangle trgle;
  31. rect.set_values(4, 5);
  32. trgle.set_values(4, 5);
  33. cout << rect.area() << endl;
  34. cout << trgle.area() << endl;
  35. return 0;
  36. }
  37. /*
  38. CPloygon::width and CPloygon::height are protected class members.
  39. CRectangle::width and CRectangle::height are protected class members.
  40. CTriangle::width and CTriangle::height are protected class members.
  41. CRectangle::set_values is a public member function.
  42. CTriangle::set_values is a public member function.
  43. CRectangle and CTriangle are derived classes of the base class CPolygon.
  44. */
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
20
10