what is needed to be solved: Execute the rectangle class with some modifications: - add color and add other shape class like triangle with same attributes of rectangle class then upload the source files and screenshoot of the run ////////////////////////////////////////////////////////////////////////////////// this is a header file: /// Specification file for the Rectangle class. #ifndef RECTANGLE_H #define RECTANGLE_H /// Rectangle class declaration. class Rectangle { private: double width; double length; string color; public: void setWidth(double); void setLength(double); void setColor(string); double getWidth() const; double getLength() const; string getColor() const; double getArea() const; }; #endif ///////////////////////////////////////////////////////////////////////////////////// this is a cpp file: // Implementation file for the Rectangle class. #include "Rectangle.h" // Needed for the Rectangle class #include // Needed for cout #include // Needed for the exit function using namespace std; ///*********************************************************** /// setWidth sets the value of the member variable width. * ///*********************************************************** void Rectangle::setWidth(double w) { if (w >= 0) width = w; else { cout << "Invalid width\n"; exit(EXIT_FAILURE); } } ///*********************************************************** /// setLength sets the value of the member variable length. * ///*********************************************************** void Rectangle::setLength(double len) { if (len >= 0) length = len; else { cout << "Invalid length\n"; exit(EXIT_FAILURE); } } ///*********************************************************** /// getWidth returns the value in the member variable width. * ///*********************************************************** double Rectangle::getWidth() const { return width; } ///************************************************************* /// getLength returns the value in the member variable length. * ///************************************************************* double Rectangle::getLength() const { return length; } ///************************************************************ /// getArea returns the product of width times length. * ///************************************************************ double Rectangle::getArea() const { return width * length; }
what is needed to be solved:
Execute the rectangle class with some modifications:
- add color
and add other shape class like triangle with same attributes of rectangle class
then upload the source files and screenshoot of the run
//////////////////////////////////////////////////////////////////////////////////
this is a header file:
/// Specification file for the Rectangle class.
#ifndef RECTANGLE_H
#define RECTANGLE_H
/// Rectangle class declaration.
class Rectangle
{
private:
double width;
double length;
string color;
public:
void setWidth(double);
void setLength(double);
void setColor(string);
double getWidth() const;
double getLength() const;
string getColor() const;
double getArea() const;
};
#endif
/////////////////////////////////////////////////////////////////////////////////////
this is a cpp file:
// Implementation file for the Rectangle class.
#include "Rectangle.h" // Needed for the Rectangle class
#include <iostream> // Needed for cout
#include <cstdlib> // Needed for the exit function
using namespace std;
///***********************************************************
/// setWidth sets the value of the member variable width. *
///***********************************************************
void Rectangle::setWidth(double w)
{
if (w >= 0)
width = w;
else
{
cout << "Invalid width\n";
exit(EXIT_FAILURE);
}
}
///***********************************************************
/// setLength sets the value of the member variable length. *
///***********************************************************
void Rectangle::setLength(double len)
{
if (len >= 0)
length = len;
else
{
cout << "Invalid length\n";
exit(EXIT_FAILURE);
}
}
///***********************************************************
/// getWidth returns the value in the member variable width. *
///***********************************************************
double Rectangle::getWidth() const
{
return width;
}
///*************************************************************
/// getLength returns the value in the member variable length. *
///*************************************************************
double Rectangle::getLength() const
{
return length;
}
///************************************************************
/// getArea returns the product of width times length. *
///************************************************************
double Rectangle::getArea() const
{
return width * length;
}
Answer:
Main.cpp:
#include <iostream>
#include "Rectangle.h"
#include "Triangle.h"
using namespace std;
int main() {
Rectangle r1(5, 6, "blue");
cout << "Length of rectangle: " << r1.getLength() << endl;
cout << "Width of rectangle: " << r1.getWidth() << endl;
cout << "Color of rectangle: " << r1.getColor() << endl;
cout << "Area of rectangle: " << r1.getArea() << "\n\n";
Triangle t1(8, 4);
cout << "Length of triangle: " << t1.getHeight() << endl;
cout << "Width of triangle: " << t1.getBase() << endl;
cout << "Area of triangle: " << t1.getArea() << "\n\n";
return 0;
}
Step by step
Solved in 6 steps with 1 images