i am having trouble with my code that is in bold when i run the prgram i get err
ID: 3820751 • Letter: I
Question
i am having trouble with my code that is in bold when i run the prgram i get errors and any help would be highly apreciated
#include <iostream>
#include <string>
using namespace std;
class Rectangle {
public:
Rectangle(float length = 0, float width = 0);
void set(float length, float width);
void get(float &length, float &width);
string getType(); // Returns “RECTANGLE”
float area();
private:
float len, wid;
};
class Triangle {
public:
Triangle(float base = 0, float height = 0);
void set(float base, float height);
void get(float &base, float &height);
string getType(); // Returns “TRIANGLE”
float area();
private:
float b, h;
};
Rectangle::Rectangle(float length, float width) {
len = length;
wid = width;
}
void Rectangle::set(float length, float width) {
len = length;
wid = width;
}
void Rectangle::get(float &length, float &width) {
length = len;
width = wid;
}
std::string Rectangle::getType() {
return "RECTANGLE";
}
float Rectangle::area() {
return len*wid;
}
Triangle::Triangle(float base, float height) {
b = base;
h = height;
}
void Triangle::set(float base, float height) {
b = base;
h = height;
}
void Triangle::get(float &base, float &height) {
base = b;
height = h;
}
std::string Triangle::getType() {
return "TRIANGLE";
}
float Triangle::area() {
return 0.5*b*h;
}
template <typename C>
void display(C obj) {
cout << "Area of a " << obj.getType() << " is " << obj.area() << endl;
}
template <typename C, typename D>
bool equalArea(C obj1, D obj2) {
if (obj1.area() == obj2.area())
return true;
else
return false;
}
template <typename C, typename D>
int maxFill(C obj3, D obj4); {
return obj3.area / obj4.area;
}
int main() {
float b, h, len, wid;
cout << "Enter the base and height for a triangle: ";
cin >> b >> h;
cout << "Enter the length and width for a rectangle: ";
cin >> len >> wid;
Rectangle rec(len, wid);
Triangle tri(b, h);
display(rec);
display(tri);
if (equalArea(rec, tri))
cout << "The two objects are of equal area" << endl;
else
cout << "The two objects are not of equal area" << endl;
cout << "There are" << maxFill(tri,rec)<<"triangles that will fit in the rectangle";
cout << "There are" << maxFill(rec,tri)<< "rectangles that will fit in the triangle";
system("pause");
return 0;
}
Explanation / Answer
Please find the error free code. (Bold part are added or changed)
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Rectangle {
public:
Rectangle(float length = 0, float width = 0);
void set(float length, float width);
void get(float &length, float &width);
string getType(); // Returns “RECTANGLE”
float area();
private:
float len, wid;
};
class Triangle {
public:
Triangle(float base = 0, float height = 0);
void set(float base, float height);
void get(float &base, float &height);
string getType(); // Returns “TRIANGLE”
float area();
private:
float b, h;
};
Rectangle::Rectangle(float length, float width) {
len = length;
wid = width;
}
void Rectangle::set(float length, float width) {
len = length;
wid = width;
}
void Rectangle::get(float &length, float &width) {
length = len;
width = wid;
}
std::string Rectangle::getType() {
return "RECTANGLE";
}
float Rectangle::area() {
return len*wid;
}
Triangle::Triangle(float base, float height) {
b = base;
h = height;
}
void Triangle::set(float base, float height) {
b = base;
h = height;
}
void Triangle::get(float &base, float &height) {
base = b;
height = h;
}
std::string Triangle::getType() {
return "TRIANGLE";
}
float Triangle::area() {
return 0.5*b*h;
}
template <typename C>
void display(C obj) {
cout << "Area of a " << obj.getType() << " is " << obj.area() << endl;
}
template <typename C, typename D>
bool equalArea(C obj1, D obj2) {
if (obj1.area() == obj2.area())
return true;
else
return false;
}
template <typename C, typename D>
int maxFill(C obj3, D obj4) {
return obj3.area() / obj4.area();
}
int main() {
float b, h, len, wid;
cout << "Enter the base and height for a triangle: ";
cin >> b >> h;
cout << "Enter the length and width for a rectangle: ";
cin >> len >> wid;
Rectangle rec(len, wid);
Triangle tri(b, h);
display(rec);
display(tri);
if (equalArea(rec, tri))
cout << "The two objects are of equal area" << endl;
else
cout << "The two objects are not of equal area" << endl;
cout << "There are" << maxFill(tri,rec)<<"triangles that will fit in the rectangle";
cout << "There are" << maxFill(rec,tri)<< "rectangles that will fit in the triangle";
std::system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.