Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include using namespace std; std::mt 1 9 9 3 7 ranGen; class Shape { public: virtual void Scale ( float scaleFactor )

#include
#include
#include
#include
using namespace std;
std::mt19937 ranGen;
class Shape {
public:
virtual void Scale(float scaleFactor)=0;
virtual void Display() const =0;
virtual ~Shape(){}
};
class Shape2D : virtual public Shape {
public:
virtual float Area() const =0;
void ShowArea() const;
virtual string GetName2D() const =0;
bool operator>(const Shape2D& rhs) const;
bool operator<(const Shape2D& rhs) const;
bool operator==(const Shape2D& rhs) const;
virtual ~Shape2D(){}
};
class Shape3D : virtual public Shape {
public:
virtual float Volume() const =0;
void ShowVolume() const;
virtual string GetName3D() const =0;
bool operator>(const Shape3D& rhs) const;
bool operator<(const Shape3D& rhs) const;
bool operator==(const Shape3D& rhs) const;
virtual ~Shape3D(){}
};
const float PI =3.14159f;
// Add 'override' specifier to virtual function implementations
void Shape2D::ShowArea() const {
cout << "The area of the "<< GetName2D()<<" is : "<< Area()<< endl;
}
bool Shape2D::operator>(const Shape2D& rhs) const {
return (this->Area()> rhs.Area());
}
bool Shape2D::operator<(const Shape2D& rhs) const {
return (this->Area()< rhs.Area());
}
bool Shape2D::operator==(const Shape2D& rhs) const {
return (this->Area()== rhs.Area());
}
void Shape3D::ShowVolume() const {
cout << "The volume of the "<< GetName3D()<<" is : "<< Volume()<< endl;
}
bool Shape3D::operator>(const Shape3D& rhs) const {
return (this->Volume()> rhs.Volume());
}
bool Shape3D::operator<(const Shape3D& rhs) const {
return (this->Volume()< rhs.Volume());
}
bool Shape3D::operator==(const Shape3D& rhs) const {
return (this->Volume()== rhs.Volume());
}
class Square : public Shape2D {
private:
float side;
public:
Square(float =0.0);
~Square(){}
float Area() const override;
string GetName2D() const override;
void Scale(float scaleFactor) override;
void Display() const override;
};
class Triangle : public Shape2D {
private:
float base, height;
public:
Triangle(float =0.0, float =0.0);
~Triangle(){}
float Area() const override;
string GetName2D() const override;
void Scale(float scaleFactor) override;
void Display() const override;
};
class Circle : public Shape2D {
private:
float radius;
public:
Circle(float =0.0);
~Circle(){}
float Area() const override;
string GetName2D() const override;
void Scale(float scaleFactor) override;
void Display() const override;
};
class TriangularPyramid : public Shape3D, private Triangle {
private:
float height;
public:
TriangularPyramid(float =0.0, float =0.0, float =0.0);
~TriangularPyramid(){}
float Volume() const override;
string GetName3D() const override;
void Scale(float scaleFactor) override;
void Display() const override;
};
class Cylinder : public Shape3D, private Circle {
private:
float height;
public:
Cylinder(float =0.0, float =0.0);
~Cylinder(){}
float Volume() const override;
string GetName3D() const override;
void Scale(float scaleFactor) override;
void Display() const override;
};
class Sphere : public Shape3D, private Circle {
private:
float radius;
public:
Sphere(float =0.0);
~Sphere(){}
float Volume() const override;
string GetName3D() const override;
void Scale(float scaleFactor) override;
void Display() const override;
};
// Implementations of Shape2D and Shape3D functions
float Square::Area() const {
return side * side;
}
string Square::GetName2D() const {
return "Square";
}
void Square::Scale(float scaleFactor){
side = side * scaleFactor;
}
void Square::Display() const {
Shape2D::ShowArea();
cout << "Length of a side: "<< side << endl;
}
float Triangle::Area() const {
return 0.5* base * height;
}
string Triangle::GetName2D() const {
return "Triangle";
}
void Triangle::Scale(float scaleFactor){
base = base * scaleFactor;
height = height * scaleFactor;
}
void Triangle::Display() const {
Shape2D::ShowArea();
cout << "Base: "<< base << endl;
cout << "Height: "<< height << endl;
}
float Circle::Area() const {
return PI * radius * radius;
}
string Circle::GetName2D() const {
return "Circle";
}
void Circle::Scale(float scaleFactor){
radius = radius * scaleFactor;
}
voidhe provided C++ code defines a hierarchy of classes representing geometric shapes in both 2D and 3D. The classes include Shape, Shape2D, Shape3D, and specific shapes like Square, Triangle, Circle, TriangularPyramid, Cylinder, and Sphere. The code uses virtual functions and operator overloading to handle area, volume, and comparisons between shapes.
Explanation:
The Scale function in the Square, Triangle, Circle, TriangularPyramid, Cylinder, and Sphere classes does not follow the virtual functio

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions

Question

III. Write the mechanism of the reactions below: (10 pts) 1. H3O+ 2

Answered: 1 week ago