HackerRank C++ Rectangle Area




The RectangleArea class is derived from Rectangle class, i.e., it is the sub-class of Rectangle class. The class should have read_input() method, to read the values of width and height of the rectangle. The RectangleArea class should also overload the display() method to print the area (widthxheight) of the rectangle.



  1. #include <iostream>
  2. using namespace std;
  3. class Rectangle
  4. {
  5. protected:
  6. int width;
  7. int height;
  8. public:
  9. void read_input()
  10. {
  11. cin>>width>>height;
  12. }
  13. void display()
  14. {
  15. cout<<width<<" "<<height<<"\n";
  16. }
  17. };
  18. class RectangleArea :public Rectangle
  19. {
  20. public:
  21. void display()
  22. {
  23. cout<<width * height;
  24. }
  25. };
  26. int main()
  27. {
  28. /*
  29. * Declare a RectangleArea object
  30. */
  31. RectangleArea r_area;
  32. /*
  33. * Read the width and height
  34. */
  35. r_area.read_input();
  36. /*
  37. * Print the width and height
  38. */
  39. r_area.Rectangle::display();
  40. /*
  41. * Print the area
  42. */
  43. r_area.display();
  44. return 0;
  45. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube