You are given this class definition:
class Rectangle{
private:
double side1, side2; // Side lengths
public:
Rectangle(); // Default constructor, set side lengths = 0
Rectangle(double s1, double s2); // Overloaded constructor
// Set side lengths = s1 and s2
void setSide(double s1, double s2); // Change side lengths to s1 & s2
double area(); // Calculate the area of the rectangular
double perimeter(); // Calculate the perimeter of the rectangular
double diagonal(); // Calculate the length of the diagonal
bool isSquare(); // Check if the rectangle is a square
};
Please complete this class.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Rectangle{
private:
  double side1, side2;  // Side lengths
public:
  Rectangle();  // Default constructor, set side lengths = 0
  Rectangle(double s1, double s2);  // Overloaded constructor
                    // Set side lengths = s1 and s2
  void setSide(double s1, double s2); // Change side lengths to s1 & s2
  double area(); // Calculate the area of the rectangular
  double perimeter(); // Calculate the perimeter of the rectangular
  double diagonal(); // Calculate the length of the diagonal
  bool isSquare();  // Check if the rectangle is a square
  Â
};
Rectangle::Rectangle()
  {
  side1 = 0;
  side2 = 0;
}
 Rectangle::Rectangle(double s1, double s2)
{
side1 = s1;
side2 = s2;
}
void Rectangle:: setSide(double s1, double s2)
{
side1 = s1;
side2 = s2;
}
double Rectangle:: area(){
return side1*side2;
}
double Rectangle:: perimeter()
{
return 2*(side1+side2);
}
double Rectangle:: diagonal()
{
return sqrt((side1*side1) + (side2*side2));
}
bool Rectangle:: isSquare()
{
return side1 == side2;
}
int main()
{
Rectangle r(10,5);
}
Comments
Leave a comment