fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Shape {
  8. private:
  9. float width, height;
  10. public:
  11. Shape(float width = 0, float height = 0) {
  12. this->width = width;
  13. this->height = height;
  14. }
  15. friend ostream &operator<< (ostream &os, Shape &s) {
  16. os << s.width << " x " << s.height;
  17.  
  18. return os;
  19. }
  20. float getWidth() {
  21. return width;
  22. }
  23. float getHeight() {
  24. return height;
  25. }
  26. };
  27. class Triangle: public Shape {
  28. private:
  29. public:
  30. Triangle(float width = 0, float height = 0):Shape(width, height) {
  31. }
  32. float area() {
  33. return 0.5 * getWidth() * getHeight();
  34. }
  35. };
  36. class Rectangle: public Shape {
  37. private:
  38. public:
  39. Rectangle(float width = 0, float height = 0):Shape(width, height) {
  40. }
  41. float area() {
  42. return getWidth() * getHeight();
  43. }
  44. };
  45.  
  46. int main () {
  47. Triangle tamgiac(4,3);
  48. Rectangle chunhat(5,6);
  49.  
  50. cout << "> Tam giac kich thuoc " << tamgiac << ". Co dien tich " << tamgiac.area();
  51. cout << "\n> Hinh chu nhat kich thuoc " << chunhat << ". Co dien tich " << chunhat.area();
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0.01s 5392KB
stdin
Standard input is empty
stdout
> Tam giac kich thuoc 4 x 3. Co dien tich 6
> Hinh chu nhat kich thuoc 5 x 6. Co dien tich 30