Create a class named ‘Rectangle’, Triangle and Square.
Rectangle class:
                       Data Members: length, width and area
                       Functions:input() ,area_Calculator()
Square class:
                       Data Members: length,area
                       Functions :input() , area_Calculator()
Triangle class:
                       Data Members: base,hieght,area
                       Functions :Input() , area_Calculator()
Compare their areas in a friend function and tell which shape has greatest area.
Area of rectangle: length*width;
Area of triangle: ½(base*height);
Area of square: (length)2
#include <iostream>
using namespace std;
class Triangle;
class Square;
////////////////////////// Rectangle
class Rectangle {
public:
  void input();
  double area_Calculator();
private:
  double length;
  double width;
  double area;
  friend bool compare(Rectangle, Triangle);
  friend bool compare(Rectangle, Square);
  friend bool compare(Square, Triangle);
  friend bool compare(Square, Rectangle);
  friend bool compare(Triangle, Rectangle);
  friend bool compare(Triangle, Square);
};
void Rectangle::input() {
  cout << "Enter a length: ";
  cin >> length;
  cout << "Enter a width: ";
  cin >> width;
}
double Rectangle::area_Calculator() {
  area = length * width;
  return area;
}
///////////////////////////////////// Square
class Square {
public:
  void input();
  double area_Calculator();
private:
  double length;
  double area;
  friend bool compare(Rectangle, Triangle);
  friend bool compare(Rectangle, Square);
  friend bool compare(Square, Triangle);
  friend bool compare(Square, Rectangle);
  friend bool compare(Triangle, Rectangle);
  friend bool compare(Triangle, Square);
};
void Square::input() {
  cout << "Enter a length: ";
  cin >> length;  Â
}
double Square::area_Calculator() {
  area = length * length;
  return area;
}
////////////////////////////// Triangle
class Triangle {
public:
  void input();
  double area_Calculator();
private:
  double base;
  double height;
  double area;
  friend bool compare(Rectangle, Triangle);
  friend bool compare(Rectangle, Square);
  friend bool compare(Square, Triangle);
  friend bool compare(Square, Rectangle);
  friend bool compare(Triangle, Rectangle);
  friend bool compare(Triangle, Square);
};
void Triangle::input() {
  cout << "Enter a base: ";
  cin >> base;
  cout << "Enter a height: ";
  cin >> height;
}
double Triangle::area_Calculator() {
  area = base * height / 2;
  return area;
}
////////////////////////////// compare
bool compare(Rectangle r, Triangle t) {
  return r.area > t.area;
}
bool compare(Rectangle r, Square s) {
  return r.area > s.area;
}
bool compare(Square s, Triangle t) {
  return s.area > t.area;
}
bool compare(Square s, Rectangle r) {
  return s.area > r.area;
}
bool compare(Triangle t, Rectangle r) {
  return t.area > r.area;
}
bool compare(Triangle t, Square s) {
  return t.area > s.area;
}
///////////////////////////// main
int main() {
  Rectangle r;
  Triangle t;
  Square s;
  cout << "Rectangle:" << endl;
  r.input();
  r.area_Calculator();
  cout << endl << "Triangle:" << endl;
  t.input();
  t.area_Calculator();
  cout << endl << "Square:" << endl;
  s.input();
  s.area_Calculator();
  cout << endl;
  if (compare(r, s)) {
    cout << "Rectangle is greater than Square" << endl;
  }
  else {
    cout << "Rectangle is not greater than Square" << endl;
  }
  if (compare(s, t)) {
    cout << "Square is greater than Triangle" << endl;
  }
  else {
    cout << "Square is not greater than Triangle" << endl;
  }
  if (compare(t, r)) {
    cout << "Triangle is greater than Rectangle" << endl;
  }
  else {
    cout << "Triangle is not greater than Rectangle" << endl;
  }
  return 0;
}
Comments
Leave a comment