thespeedracer38

Shape class with Overloading

Feb 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. class Shape{
  8.     public:
  9.         double area();
  10. };
  11.  
  12. class Square:public Shape{
  13.     double side;
  14.     public:
  15.         Square(double = 0.0);
  16.         double area();
  17. };
  18.  
  19. class Circle:public Shape{
  20.     double radius;
  21.     public:
  22.         Circle(double = 0.0);
  23.         double area();
  24. };
  25.  
  26. class Rectangle:public Shape{
  27.     double length, breadth;
  28.     public:
  29.         Rectangle(double = 0.0, double = 0.0);
  30.         double area();
  31. };
  32.  
  33. inline Square::Square(double side){
  34.     Square::side = side;
  35. }
  36.  
  37. inline Circle::Circle(double radius){
  38.     Circle::radius = radius;
  39. }
  40.  
  41. inline Rectangle::Rectangle(double length, double breadth){
  42.     Rectangle::length = length;
  43.     Rectangle::breadth = breadth;
  44. }
  45.  
  46. inline double Circle::area(){
  47.     return M_PI * radius * radius;
  48. }
  49.  
  50. inline double Rectangle::area(){
  51.     return length * breadth;
  52. }
  53.  
  54. inline double Square::area(){
  55.     return side * side;
  56. }
  57.  
  58. int main(int argc, char** argv) {
  59.     system("cls");
  60.     cout << "For square" << endl;
  61.     cout << "Enter a side: ";
  62.     double side;
  63.     cin >> side;
  64.     cout << "For circle" << endl;
  65.     cout << "Enter a radius: ";
  66.     double radius;
  67.     cin >> radius;
  68.     cout << "For rectangle" << endl;
  69.     cout << "Enter a length: ";
  70.     double length;
  71.     cin >> length;
  72.     cout << "Enter a breadth: ";
  73.     double breadth;
  74.     cin >> breadth;
  75.     Circle c(radius);
  76.     Rectangle r(length, breadth);
  77.     Square s(side);
  78.     cout << "Area of circle = " << c.area() << endl;
  79.     cout << "Area of square = " << s.area() << endl;
  80.     cout << "Area of rectangle = " << r.area() << endl;
  81.     cin.ignore();
  82.     cin.get();
  83.     return 0;
  84. }
Add Comment
Please, Sign In to add comment