fork download
  1. #include <iostream>
  2.  
  3. class Rectangle {
  4. private:
  5. int length;
  6. int width;
  7.  
  8. public:
  9.  
  10. Rectangle() : length(1), width(1) {
  11. std::cout << "Rectangle object with default values created." << std::endl;
  12. }
  13.  
  14. Rectangle(int l, int w) : length(l), width(w) {
  15. std::cout << "Rectangle object with given values created." << std::endl;
  16. }
  17.  
  18.  
  19. ~Rectangle() {
  20. std::cout << "Rectangle object destroyed." << std::endl;
  21. }
  22.  
  23.  
  24. int getArea() const {
  25. return length * width;
  26. }
  27.  
  28. void displayDetails() const {
  29. std::cout << "Length: " << length << ", Width: " << width << ", Area: " << getArea() << std::endl;
  30. }
  31. };
  32.  
  33. int main() {
  34. Rectangle rect1;
  35. std::cout << "Details of rect1:" << std::endl;
  36. rect1.displayDetails();
  37.  
  38.  
  39. Rectangle rect2(5, 3);
  40. std::cout << "Details of rect2:" << std::endl;
  41. rect2.displayDetails();
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Rectangle object with default values created.
Details of rect1:
Length: 1, Width: 1, Area: 1
Rectangle object with given values created.
Details of rect2:
Length: 5, Width: 3, Area: 15
Rectangle object destroyed.
Rectangle object destroyed.