Part 1 10 marks The file for GeometricObject class is provid

Part 1 (10 marks)
The file for GeometricObject class is provided for this exercise.
Design a class named Triangle that extends GeometricObject. The class contains the following:
Three double data fields named side1, side2, and side3 to denote three sides of the triangle.
A no-arg constructor that creates a default triangle with each side 1.0.
A constructor that creates a rectangle with the specified side1, side2, and side3.
The constant accessor functions for all three data fields.
A constant function named getArea() that returns the area of this triangle.
A constant function named getPerimeter() that returns the perimeter of this triangle.
A constant function named toString() that returns a string “Triangle Object”.
Implement the class. Write a test program that prompts the user to enter three sides of the triangle, enter a color, and enter 1 or 0 to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether filled or not.
A Sample run of Part 1:
Enter three sides: 12.3 15.5 23.3
Enter the color: blue
Enter 1/0 for filled (1: true, 0: false): 1
Area is 87.4939
Perimeter is 51.1
Color is blue
Filled is true
Part 2 (10 marks)
The files for GeometricObject, Circle and Rectangle classes are provided for this exercise.
i) Implement a constant member function named compareTo( ) in each of the Circle, Rectangle
and Triangle class. The function should compare the area of the two geometric objects. The
function should return 1 if the current object’s area is larger than the argument object’s area,
return -1 if the argument object’s area is larger and return zero if they are equal.
The compareTo function should have the function signature as follow:
int compareTo(Circle g) const; //for Circle
int compareTo(Triangle g) const;//for Triangle
int compareTo(Rectangle g) const;//for Rectangle
ii) This exercise is about polymorphism, runtime-binding and dynamic cast. You need to make
some changes to the GeometricObject, Circle and Rectangle classes in order for runtime-binding
to take effects.
Implement a non member function named larger in your new test program. This function takes
two geometric objects of the same type as arguments and returns the larger one. If two geometric
objects are not of the same type are used as arguments, the function returns NULL. You should
make use of the compareTo function in (i) in this function to determine which object is larger.
The function prototype is as follow:
GeometricObject* larger(GeometricObject & o1, GeometricObject & o2)
Write a test program to test the larger function. Create two objects of Circle, Rectangle and
Triangle and use them to call the larger function repeatedly. Call the larger function again by
using two different object types. Your program should give the output as follow:
The Geometric object is: Circle object
The larger Circle object\'s Area is: 201.062
The Geometric object is: Rectangle object
The larger Rectangle object\'s Area is: 9
The Geometric object is: Triangle object
The larger Triangle object\'s Area is: 9.92157
The two objects are different and not comparable

Hi,

Need help on this program. needs to be done C++ language, can not mix with JAVA or another language. please help me

Solution

1)

#include<iostream>
using namespace std;
class GeometricObject
{
  
   string name;
  
public:
   GeometricObject()
   {
       name = \"\\0\";
   }
   GeometricObject(string s)
   {
       name = s;
   }
   virtual double getArea()
   {
       return 0;
   }
   virtual double getPerimeter()
   {
       return 0;
   }
};

class triangle: public GeometricObject
{
   double side1,side2,side3,area,perimeter;
public:
   triangle()
   {
       side1 = 1.0;
       side2 = 1.0;
       side3 = 1.0;
       area = 0;
       perimeter= 0;

       GeometricObject(\"triangle\");
   }
   triangle(double s1,double s2 , double s3)
   {
       side1 = s1;
       side2 = s2;
       side3 = s3;
       area = getArea();
   }
   double getArea()
   {
       double p,prod;
       p = (side1 + side2 + side3)/2;
       prod = p*(p-side1)*(p-side2)*(p-side3);
       area = sqrt(prod); //Area   =      p   (   p      a   )    (   p      b   )   (   p      c   )
       return area;
   }
   double getPerimeter()
   {
       return(side1+side2+side3);
   }
};
int main()
{
   GeometricObject *obj;
   double s1,s2,s3;
   char col[10];
   bool filled;

   cout<<\"Enter 3 side : \";
   cin>>s1>>s2>>s3;

   triangle obj_tri(s1,s2,s3);
   obj = &obj_tri;

   cout<<\"Enter the color: \";
   cin>>col;

   cout<<\"Enter 1/0 for filled (1: true, 0: false)\";
   cin>>filled;

   cout<<\"Area is : \"<<obj->getArea()<<endl;
   cout<<\"Perimeter is: \"<<obj->getPerimeter()<<endl;

   cout<<\"Colour is : \"<<col<<endl;
   if( filled == 0)
       cout<<\"Filled is \"<<\"False\"<<endl;
   else
       cout<<\"Filled is \"<<\"true\"<<endl;
}

2)

due to lack of time just giving other classes and also non member function larger

class circle: public GeometricObject
{
   double radius,area,perimeter;
public:
   circle()
   {
       radius = 1.0;
       area = 0;
       perimeter= 0;

       GeometricObject(\"circle\");
   }
  
   circle(double s1)
   {
       radius = s1;
       area = getArea();
   }
   double getArea()
   {
       double PI = 3.14159;
       area = PI * radius * radius;
       return area;
   }
   double getPerimeter()
   {
       double PI = 3.14159;
       return(2 * PI * radius );
   }
   double compareTo(GeometricObject &t1, GeometricObject &t2)
   {
       if(t1.getname().compare( t2.getname()) )
       {
           cout<<\"Different objects\"<<endl;
           return -1;
       }
       if( t1.getArea() > t2.getArea() )
           return 1;
       else
           return 0;
       //return (t1.getArea() - t2.getArea());
   }


};

class rectangle: public GeometricObject
{
   double side1,side2,area,perimeter;
public:
   rectangle()
   {
       side1 = 1.0;
       side2 = 1.0;
       area = 0;
       perimeter= 0;

       GeometricObject(\"circle\");
   }
  
   rectangle(double s1, double s2)
   {
       side1 = s1;
       side2 = s2;
       area = getArea();
   }
   double getArea()
   {
       area = side1 * side2;
       return area;
   }
   double getPerimeter()
   {
       return(2*(side1+side2));
   }
   double compareTo(GeometricObject &t1, GeometricObject &t2)
   {
       if(t1.getname().compare( t2.getname()) )
       {
           cout<<\"Different objects\"<<endl;
           return -1;
       }
       if( t1.getArea() > t2.getArea() )
           return 1;
       else
           return 0;
       //return (t1.getArea() - t2.getArea());
   }


};

GeometricObject* larger(GeometricObject & o1, GeometricObject & o2)
{
   if (o1.compareTo(o1,o2))
   {
       cout<<\"The larger \"<<\" name of the object\"

<<\" object\'s Area is \"<<o1.getArea()<<endl;
       return &o1;
   }
   else
       {
           return &o2;
   }
}

Part 1 (10 marks) The file for GeometricObject class is provided for this exercise. Design a class named Triangle that extends GeometricObject. The class contai
Part 1 (10 marks) The file for GeometricObject class is provided for this exercise. Design a class named Triangle that extends GeometricObject. The class contai
Part 1 (10 marks) The file for GeometricObject class is provided for this exercise. Design a class named Triangle that extends GeometricObject. The class contai
Part 1 (10 marks) The file for GeometricObject class is provided for this exercise. Design a class named Triangle that extends GeometricObject. The class contai
Part 1 (10 marks) The file for GeometricObject class is provided for this exercise. Design a class named Triangle that extends GeometricObject. The class contai

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site