【发布时间】:2023-03-19 00:21:01
【问题描述】:
对于实验室,我必须重载 + 运算符来添加来自同一类的对象,并重载 == 运算符来比较来自两个 不同 类的对象。重载 == 运算符的函数给我带来了很多麻烦(可能是因为我正在使用它来比较不同类的对象区域)。我一直在不懈地寻找解决方案,并且我尝试了所有发现的建议都没有成功,所以我不得不使用我的特定代码来询问:
// OBJECTIVES:
// Add areas of 2 circles
// Add areas of 2 rectangles
// Compare areas of a circle and a rectangle
#include <iostream>
using namespace std;
// **********************Header*********************
class circle
{
friend bool operator==(const circle& ,
const circle&);
friend circle operator+(const circle& ,
const circle&);
public:
double radius, area;
void calcArea();
};
class rect
{
friend bool operator==(const rect& ,
const rect&);
friend rect operator+(const rect& ,
const rect&);
public:
double length, width, area;
void calcArea();
};
void displayMenu();
// **************************************************
// **********************Program*********************
int main()
{
int selection; // variable for menu selection
circle firstCircle; // objects from circle class
circle secondCircle;
rect firstRect; // objects from rect class
rect secondRect;
do {
displayMenu();
cin >> selection;
cout << endl;
if (selection == 1) // add area of 2 circles
{
firstCircle.calcArea();
secondCircle.calcArea();
circle thirdCircle = firstCircle + secondCircle;
cout << "The sum of your two circles is: " ;
cout << thirdCircle.area;
cout << endl;
}
else if (selection == 2) // add area of 2 rectangles
{
firstRect.calcArea();
secondRect.calcArea();
rect thirdRect = firstRect + secondRect;
cout << "The sum of your two rectangles is: " ;
cout << thirdRect.area;
cout << endl;
}
else if (selection == 3) // compare areas of a circle and a rectangle
{
firstCircle.calcArea();
firstRect.calcArea();
if (firstCircle.area == firstRect.area)
{
cout << "The area of your circle is equal to that of your rectangle." << endl;
}
else
{
cout << "The area of your circle is not equal to that of your rectangle." << endl;
}
}
else if (selection == 4) // exit program
{
return 0;
}
else
{
cout << "Please enter a valid selection.";
cout << endl;
continue;
}
} while (1);
return 0;
}
// **************************************************
// ******************Implementation******************
void circle::calcArea() // compute circle area
{
cout << "Enter a radius: ";
cin >> radius;
area = 3.14159265359 * radius * radius;
}
void rect::calcArea() // compute rectangle area
{
cout << "Enter a length: ";
cin >> length;
cout << "Enter a width: ";
cin >> width;
area = length * width;
}
bool operator==(const circle& firstCircle, // compare areas of objects
const rect& firstRect) // from different classes
{
return (firstCircle.area == firstRect.area &&
firstCircle.area == firstRect.area);
}
circle operator+ (const circle& firstCircle, // overload + for circle class
const circle& secondCircle)
{
circle circleSum;
circleSum.radius = firstCircle.radius + secondCircle.radius;
circleSum.area = firstCircle.area + secondCircle.area;
return circleSum;
}
rect operator+ (const rect& firstRect, // overload + for rect class
const rect& secondRect)
{
rect rectSum;
rectSum.length = firstRect.length + secondRect.length;
rectSum.width = firstRect.width + secondRect.width;
rectSum.area = firstRect.area + secondRect.area;
return rectSum;
}
void displayMenu() // menu options
{
cout << endl;
cout << "What would you like to do?" << endl;
cout << "1. Add the area of 2 circles."<< endl;
cout << "2. Add the area of 2 rectangles."<< endl;
cout << "3. Compare the area of a circle and a rectangle."<< endl;
cout << "4. Exit.";
}
// **************************************************
现在,我没有使用重载的 == 来比较矩形和圆形区域,因为这样会出现很多编译器错误。如果你们能提供任何帮助我将firstCircle.area == firstRect.area 更改为firstCircle == firstRect,我们将不胜感激。
【问题讨论】:
-
看起来至少有一个问题是在代码中声明重载 == 运算符之前使用它。在 main 上方添加声明(您仍然可以在 main 函数代码块之后定义它)。
-
我建议您需要考虑继承。 Circle 和 Rectangle 都是有面积的形状(提示 Circle 和 Rectangle 继承自 Shape 类。
-
建议:当询问堆栈溢出问题时,备份您的程序,然后删除与问题无关的所有内容。如果问题不是关于菜单的,菜单就会出现。如果问题不是关于
operator +,+就可以了。你想要的只是main和你遇到问题的东西。谁知道呢,如果没有额外的噪音,您可能会自己找出问题所在! -
战术要点:为什么一个名为
calcArea的方法接受输入?听起来应该是计算面积。一个函数应该做一件事,而且只做一件事。如果它做了两件事,它实际上就没那么有用了。例如,您无法计算现有圆的面积。你必须再次在半径范围内喂食,这有点愚蠢。
标签: c++ class overloading