您的位置:首页 > 编程语言 > C语言/C++

【C++语言实现】【本科实验系列】编写程序在CirCle类中实现关系运算符(大于,小于,等于),实现按半径对Circle对象排序,自己定义类中所需成员。完成上述功能。

2019-05-10 21:37 736 查看
版权声明:版权所有©漫天风沙里的人 https://blog.csdn.net/qq_30204431/article/details/90084342

【C++语言实现】【本科实验系列】编写程序在CirCle类中实现关系运算符(大于,小于,等于),实现按半径对Circle对象排序,自己定义类中所需成员。完成上述功能。

前言介绍

要求如本文标题所言
废话不多说,直接上代码

代码

(此代码注释特别详细,认真看应该能看懂)

/* 实验四:编写程序在CirCle类中实现关系运算符
(大于,小于,等于),实现按半径对Circle对象排序。
自己定义类中所需成员。完成上述功能。
作者:秦瑞迁(漫天风沙里的人)
本代码已经发布到本人CSDN,供学习者学习使用
注:本题目有很大的拓展性,比如可以加入圆面积计算方法
以及可以用链表来实现动态地搞一个CirCle数组*/
#include<iostream>
using namespace std;
//圆类型
class CirCle
{
private:
double radius;
public:
//构造函数声明
CirCle();
//括号形式赋值方法声明
CirCle(double input_radius);
//Get半径方法声明
double Get_Radius();
//赋值运算符(一个变量赋值给另一个变量)重载声明
CirCle& operator = (const CirCle& other);
//赋值运算符(一个double数赋值)重载声明
CirCle& operator = (double other_radius);
//关系运算符==(raidus之间比较)重载声明
bool operator == (const CirCle& other);
//关系运算符==(radius与数之间比较)重载声明
bool operator == (double other_radius);
//关系运算符>重载声明
bool operator > (const CirCle& other);
//关系运算符<重载声明
bool operator < (const CirCle& other);
//关系运算符>=重载声明
bool operator >= (const CirCle& other);
//关系运算符<=重载声明
bool operator <= (const CirCle& other);
//输出流运算符重载声明
friend ostream& operator << (ostream& output,CirCle& c);
//输入流运算符重载声明
friend istream& operator >> (istream& input,CirCle& c);
};
//构造函数初始化radius方法
CirCle::CirCle()
{
this->radius = 0;
}
//括号形式赋值方法实现
CirCle::CirCle(double input_radius)
{
this->radius = input_radius;
}
//Get半径方法
double CirCle::Get_Radius()
{
return this->radius;
}
//赋值运算符(一个变量赋值给另一个变量)重载实现
CirCle& CirCle::operator = (const CirCle& other)
{
this->radius = other.radius;
return *this;
}
//赋值运算符(一个double数赋值)重载实现
CirCle& CirCle::operator = (double other_radius)
{
this->radius = other_radius;
return *this;
}
//关系运算符==(raidus之间比较)重载
bool CirCle::operator == (const CirCle& other)
{
//如果半径等于右侧CirCle对象的半径
if((this->radius) == other.radius)
{
//返回真
return true;
}
//如果半径不等于右侧CirCle对象的半径
else
{
//返回假
return false;
}
}
//关系运算符==(radius与数之间比较)重载实现
bool CirCle::operator == (double other_radius)
{
//如果右值相等
if((this->radius) == other_radius)
{
//返回真
return true;
}
//否则
else
{
//返回假
return false;
}
}
//关系运算符>重载实现(实现方法与==雷同)
bool CirCle::operator > (const CirCle& other)
{
if((this->radius > other.radius))
{
return true;
}
else
{
return false;
}
}
//关系运算符<重载实现
bool CirCle::operator < (const CirCle& other)
{
if((this->radius < other.radius))
{
return true;
}
else
{
return false;
}
}
//关系运算符>=重载实现
bool CirCle::operator >= (const CirCle& other)
{
if((this->radius >= other.radius))
{
return true;
}
else
{
return false;
}
}
//关系运算符<=重载实现
bool CirCle::operator <= (const CirCle& other)
{
if((this->radius <= other.radius))
{
return true;
}
else
{
return false;
}
}
//输出流运算符重载实现
ostream& operator << (ostream& output,CirCle& c)
{
output<<c.radius;
return output;
}
//输入流运算符重载实现
istream& operator >> (istream& input,CirCle& c)
{
input>>c.radius;
return input;
}
int main()
{
//由大到小冒泡排序
CirCle array[4]={4,3,2,1};
for(int i=0;i<3;i++)
{
for(int j=i+1;j<4;j++)
{
if(array[i]<array[j])
{
CirCle temp = array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
for(int i = 0;i<4;i++)
{
cout<<array[i].Get_Radius()<<endl;
}
return 0;
}

总结

这个实验中规中矩,主要就是练练怎么重载关系运算符,不过呢,接下来我要给出一个装逼方案,可以让老师眼前一亮,本来想自己写链表,但是源代码文件丢了(也是本人水平有限,再写一个链表我头痛得一批),接下来的演示我将直接使用Vector来制作动态对象的版本

装逼代码

对于数组实现可以用Vector来实现,可以搞成动态数组
首先声明一下vector

#include<vector>

然后把主函数的数组排序的算法改成如下这段

vector(CirCle) arr;
arr.push_back(1);
arr.push_back(2);
arr.push_back(3);
arr.push_back(2.5);

上面的代码相当于一个动态的数组,依次给数组(其实其本质是链表)依次添加了CirCle对象的元素{1,2,3,2.5}
对于动态数组的冒泡排序可以用如下代码实现

for(int i = 0;i<arr.size();i++)
{
int val = arr.at(i);
cout<<val;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐