C++ C++;重载==比较不同类中的对象
对于实验室,我必须重载+运算符来添加来自同一类的对象,重载==运算符来比较来自两个不同类的对象。重载==运算符的函数给我带来了很多麻烦(可能是因为我用它来比较不同类的对象区域)。我一直在不懈地寻找解决方案,我尝试了所有找到的建议,但都没有成功,因此我不得不使用我的特定代码询问:C++ C++;重载==比较不同类中的对象,c++,class,overloading,C++,Class,Overloading,对于实验室,我必须重载+运算符来添加来自同一类的对象,重载==运算符来比较来自两个不同类的对象。重载==运算符的函数给我带来了很多麻烦(可能是因为我用它来比较不同类的对象区域)。我一直在不懈地寻找解决方案,我尝试了所有找到的建议,但都没有成功,因此我不得不使用我的特定代码询问: // OBJECTIVES: // Add areas of 2 circles // Add areas of 2 rectangles // Compare areas of a circle and a recta
// 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.";
}
// **************************************************
//目标:
//添加两个圆圈的区域
//添加两个矩形的区域
//比较圆和矩形的面积
#包括
使用名称空间std;
//*************************标题*********************
班级圈子
{
友元布尔运算符==(常数圆&,
常数圆&);
朋友圈运算符+(常数圈&,
常数圆&);
公众:
双半径,面积;
钙质空洞();
};
类矩形
{
friend bool运算符==(常量rect&,
const rect&);
friend rect运算符+(const rect&,
const rect&);
公众:
双倍长度、宽度、面积;
钙质空洞();
};
void displayMenu();
// **************************************************
//*************************计划*********************
int main()
{
int selection;//用于菜单选择的变量
circle firstCircle;//circle类中的对象
第二圈;
rect firstRect;//rect类中的对象
rect secondRect;
做{
显示菜单();
cin>>选择;
cout操作符重载可以像下面这样重载
#include <iostream>
using namespace std;
class rect;
class circle
{
friend bool operator==(const circle&,
const circle&);
//friend circle operator+(const circle&,
// const circle&);
public:
double radius, area;
//void calcArea();
public:
friend bool operator == (const circle&, const rect&);
double getArea()const { return area; }
};
class rect
{
friend bool operator==(const rect&,
const rect&);
//friend rect operator+(const rect&,
// const rect&);
public:
double length, width, area;
//void calcArea();
public:
friend bool operator == (const rect&, const circle&);
double getArea() const{ return area; }
};
int main(int argc, char *argv[]) {
circle c;
rect r;
if (c == r) {
cout << "It was a miracle a random circle is equal to a random rectangle in size!!!" << endl;
}
return 0;
}
bool operator == (const circle &c1, const circle &c2) {
return c1.getArea() == c2.getArea();
}
bool operator == (const rect &r1, const rect &r2) {
return r1.getArea() == r2.getArea();
}
bool operator == (const circle &c, const rect &r) {
return c.getArea() == r.getArea();
}
bool operator == (const rect &r, const circle &c) {
return c.getArea() == r.getArea();
}
#包括
使用名称空间std;
类rect;
班级圈子
{
友元布尔运算符==(常数圆&,
常数圆&);
//朋友圈运算符+(常数圈&,
//常数圆&);
公众:
双半径,面积;
//钙质空洞();
公众:
friend bool运算符==(常数圆&,常数矩形&);
double getArea()常量{return area;}
};
类矩形
{
friend bool运算符==(常量rect&,
const rect&);
//friend rect运算符+(const rect&,
//const rect&);
公众:
双倍长度、宽度、面积;
//钙质空洞();
公众:
friend bool运算符==(常数矩形&,常数圆&);
double getArea()常量{return area;}
};
int main(int argc,char*argv[]){
圈c;
矩形r;
if(c==r){
cout有许多方法可以编写您想要的比较运算符。最简单的方法是,在不干预类代码的情况下,只实现两个非成员运算符:
bool operator==(const circle& c, const rect& r) { return r.area == c.area; }
bool operator==(const rect& r, const circle& c) { return c.area == r.area; }
class circle
{
public:
bool operator==(const circle& other) const { return area == other.area; }
// ...skipped...
};
您可以将它们放在main
函数上方的源文件中,或者放在单独的头文件中。请注意,它们不需要是circle
和rect
的朋友,因为区域
成员是公共的
另一种方法是编写成员函数,但首先我们需要修复现有的比较运算符:
bool operator==(const circle& c, const rect& r) { return r.area == c.area; }
bool operator==(const rect& r, const circle& c) { return c.area == r.area; }
class circle
{
public:
bool operator==(const circle& other) const { return area == other.area; }
// ...skipped...
};
它与您的版本有何不同
它是一个成员函数,它将此
与其他
进行比较,只使用一个参数
从圈外调用是公共的
类
它是const限定的,因为它只需要查找类数据而不需要修改
此外,常量限定符允许您比较常量对象,如下所示:
const circle c1 = getSomeCircle();
const circle& c2 = otherCircle;
c1 == c2; // Note: statement has no effect, despite it's syntax is correct.
c2 == c1;
非const限定的比较将无法编译。总之,这是编写C++中比较运算符的最惯用的方式。
最后,让我们将圆添加到rect比较:
class rect;
class circle
{
public:
bool operator==(const circle& other) const { return area == other.area; }
bool operator==(const rect&) const;
// ...skipped...
};
class circle { /* class definition skipped */ };
bool operator==(const rect& r) const { return area == r.area; }
这里首先我们声明了rect
类。当类被声明但未定义(这是正向声明)时,我们可以使用指针和引用来引用它的实例,但不能引用实例本身。我们不能使用实例的原因之一是类的大小在定义之前是未知的
然后我们声明成员operator==
接受对前向声明类的引用,最后在rect
定义之后,我们可以实现该操作符
rect
到circle
的比较可能以相同的方式实现。看起来至少有一个问题是重载==运算符在代码中声明之前就被使用了。在main上方添加一个声明(您仍然可以在main functions代码块之后定义它).我建议你考虑一下继承问题.圆形和矩形都是有面积的形状(提示:使圆形和矩形继承自类形状。有用的阅读:建议:在询问堆栈溢出问题时,备份程序,然后删除与问题无关的所有内容。如果问题与菜单无关,则菜单将显示。如果问题与操作符+
无关,+
将显示。所有您需要的want是一个main
和您遇到问题的东西。谁知道呢,如果没有所有额外的噪音,您可能会自己找出问题所在!战术要点:为什么名为Calcrea
的方法需要输入?听起来它应该计算面积。一个函数应该做一件事,而且只有一件事。如果它做了两件事例如,你不能计算现有圆的面积。你必须再次输入半径,这有点愚蠢。注意:如果circle
和rect
派生自,比如说,shape
,并且shape
有一个getArea
方法,那么你可以bool操作符==(constshape&a,constshape&b){返回a.getArea()==b.getArea();}值得注意的是,比较<代码>双 s,对于等式来说,可能会变得非常讨厌。通常你需要。@ USER881301。我只是跳过这个问题,因为它已经足够大了C++初学者。不用担心。注释更多的是给他们和那些追随的人,因为当你不期望它时,它是一个非常讨厌的惊喜。哦,有一个新的。