原题目如下:
请为判断函数isEqualTo编写一个简单的函数模板,isEqualTo函数利用等号运算符(==)判断两个同类型的参数是否相等,如果相等则返回true;反之返回false。在主函数中,利用函数模板,对多种内置数据类型(int/char/double)调用isEqualTo函数。
接着,定义Someclass类,该类具有两个数据成员。请为该类添加必要的成员函数,使得在主函数中能够调用isEqualTo函数判断两个对象是否相等。
测试用例如下:
Sample Input:
3 5
d t
3.5 7.8
2 5.6
Sample Output:
3 and 5 are not equal
d and t are not equal
3.5 and 7.8 are not equal
The class objects (1,1.1) and (2,5.6) are not equal
Sample Input:
2 4
d D
1.2 1.2
1 1.1
Sample Output:
2 and 4 are not equal
d and D are not equal
1.2 and 1.2 are equal
The class objects (1,1.1) and (1,1.1) are equal
然后开始写吧,在认真掉发 思考后写出的源代码:
#include<iostream>
using namespace std;
template<typename T>
bool isEqualTo(T& x,T& y)
{
if(x==y) return true;
else return false;
}
//函数模板
class SomeClass
{
public:
int x;
double y;
SomeClass(int xx=0,double yy=0){x=xx;y=yy;}
bool operator==(SomeClass& z);
friend istream& operator>>(istream &in ,const SomeClass &z);
friend ostream& operator<<(ostream &out ,const SomeClass &m);
};
istream& operator>>(istream &in ,const SomeClass &z){
in>>z.x>>z.y;
return in;
}
ostream& operator<<(ostream &out ,const SomeClass &m){
out<<"("<<m.x<<","<<m.y<<")";
}
bool SomeClass:: operator==(SomeClass& z)
{
if(z.x==x&&z.y==y) return 1;
else return 0;
}
int main()
{
int a; // integers used for
int b; // testing equality
// test if two ints input by user are equal
// cout << "Enter two integer values: ";
cin >> a >> b;
char c; // chars used for
char d; // testing equality
// test if two chars input by user are equal
// cout << "\nEnter two character values: ";
cin >> c >> d;
double e; // double values used for
double f; // testing equality
// test if two doubles input by user are equal
// cout << "\nEnter two double values: ";
cin >> e >> f;
SomeClass g( 1, 1.1 ); // SomeClass objects used
SomeClass h; // for testing equality
cin >> h;
cout << a << " and " << b << " are "
<< ( isEqualTo( a, b ) ? "equal" : "not equal" ) << '\n';
cout << c << " and " << d << " are "
<< ( isEqualTo( c, d ) ? "equal" : "not equal" ) << '\n';
cout << e << " and " << f << " are "
<< ( isEqualTo( e, f ) ? "equal" : "not equal") << '\n';
// test if two SomeClass objects are equal
// uses overloaded << operator
cout << "The class objects " << g << " and " << h << " are "
<< ( isEqualTo( g, h ) ? "equal" : "not equal" ) << '\n';
return 0;
} // end main
看上去已经大功告成,写完后,小媛喝了口茶,她知道,事情没有那么简单。于是,在本地编译通过后,运行时在输入三组数之后总会自动结束。
新一轮的掉发头脑风暴后(实际是改了一两个小时无果,就随便试试)终于发现:
如果重载流运算符>>时将去掉const,就可以输入第四组数,并完成isEqualTo函数模板功能。
**~~~~~~~~~~~~~~**
分隔符:好啦,小媛准备明天的c++期末考试去了,考完试会再来讲述她的奇(diao)妙(fa)之旅
2020/06/19更新~~~~我来了~
前天考完c++,又摸了一天的鱼,终于要放暑(xue)假(xi)了,不过在此之前还是要写完这道题:
首先来回顾一下整道题的详细思路:
根据题意,首先利用函数模板写出isEqualTo函数,因为后面要利用它判断真假,因此定义为一个bool类型,在SomeClass类中有数据成员,构造函数,==操作符重载,输入输出流重载。 其中 ==操作符重载是因为函数模板中对SomeClass类型的相等判定需要我们自己定义,返回值依旧是bool类型的。
问题来了,在对流输入运算符<<运算符重载参数你用的是一个SomeClass类型的参数,为了安全起见,你加了const,使其变为只读。可是下面你要给类的数据成员赋值,和const的只读性矛盾,可是编译器却不会报错,只有在你运行时才能发现错误,如果代码量比较大,你又不容易debug,所以这种错误还是比较浪费时间的。
**总结:
一个很小却比较有用的知识点:我们老师讲过一句话:“当const可加可不加时,最好加上”,没错,加const的确有利于数据的安全性,可是在重载流输入运算符时,由于接受的参数是需要修改的,因此参数不能加const的。
最后可以参考这篇文章,对运算符重载中的const有比较详细的分析:
https://blog.youkuaiyun.com/weixin_43734095/article/details/104465307
最后的最后(* ̄︶ ̄),如果有不妥的地方欢迎批评指正。