Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programing language is C++ define three classes: Shape, Rect, and Triangle. Rect

ID: 644254 • Letter: P

Question

Programing language is C++

define three classes: Shape, Rect, and Triangle. Rect and Triangle should inherit from the Shape class. Shape class is quite generic and just has two functions: CheckValidity() for checking if the sides given forms a valid shape, and GetArea() for calculating the area of the shape. Of course, you won't be able to do real implementation until you inherit this to a specific shape class (Rect and Triangle). Rect has two member variables (a, b) and its own version of CheckValidity() and GetArea(). It also has Get/Set functions for the member variables. Similarly, Triangle has three member variables (a, b, c) and its own version of CheckValidity() and GetArea(). It also has Get/Set functions for the member variables. Note that classes should have their own constructors to initialize member variables.

create two file called "Rsides.txt" and "Tsides.txt" and put them in the same folder as your program. The file contains multiple lines with each line having the required sides, separated by white spaces, e.g., for triangle,

3 4 5

6 8 10

5 6 7

....

Note that values can be invlid when you use them to form the specific shape, that's where validity check is performed.

then create two vectors: vRect and vTriangle that contain Rect objects and Triangle objects, respectively.

for each line you read from the side files, create an object of the specific shape class, check validity. If valid, then insert the object into the vector. Otherwise, read the next line.

declare and define two "global" function saveRArea() and saveTArea() that saves all area values to two files called "Rarea.txt" and "Tareas.txt", respectively. These files are located in the same folder.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

class Shape{
public:
   virtual double GetArea();
   virtual bool CheckValidity();
};

class Rectangle{
private:
   double a, b;
public:
   Rectangle(double a, double b){
       setA(a);
       setB(b);
   }
   void setA(double val){a = val;}
   void setB(double val){b = val;}
   double getA() const {return a;}
   double getB() const {return b;}
   bool CheckValidity(){
       if(a > 0 && b > 0){
           return true;
       }
       return false;
   }
   double GetArea(){
       return a * b;
   }
};

class Triangle{
private:
   double a, b, c;
public:
   Triangle(double a, double b, double c){
       setA(a);
       setB(b);
       setC(c);
   }
   void setA(double val){a = val;}
   void setB(double val){b = val;}
   void setC(double val){c = val;}
   double getA() const {return a;}
   double getB() const {return b;}
   double getC() const {return c;}
   bool CheckValidity(){
       if(a > 0 && b > 0 && c > 0){
           if(a > b && a > c){
               return a < (b + c);
           }
           else if(b > a && b > c){
               return b < (a + c);
           }
           else if(c > a && c > b){
               return c < (a + b);
           }
       }
       return false;
   }
   double GetArea(){
       double p = (a + b + c) / 2;
       return sqrt(p * (p - a) * (p - b) * (p - c));
   }
};

using namespace std;

int main(){
   vector<Rectangle> vRect;
   vector<Triangle> vTriangle;
   ifstream in;
   in.open("Tsides.txt");
   double a, b, c;
   if(in.is_open()){
       while(in >> a){
           in >> b >> c;
           Triangle temp = Triangle(a, b, c);
           if(temp.CheckValidity()){
               vTriangle.push_back(temp);
           }
       }
   }
   in.close();
   in.open("Rsides.txt");
   if(in.is_open()){
       while(in >> a){
           in >> b;
           Rectangle temp = Rectangle(a, b);
           if(temp.CheckValidity()){
               vRect.push_back(temp);
           }
       }
   }
   in.close();
   ofstream out;
   out.open("Tareas.txt");
   for(int i = 0; i < vTriangle.size(); ++i){
       out << vTriangle[i].GetArea() << endl;
   }
   out.close();
   out.open("Rareas.txt");
   for(int i = 0; i < vRect.size(); ++i){
       out << vRect[i].GetArea() << endl;
   }
   out.close();
   return 0;
}