Write a simple function template for predict function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false if they are not equal. Use this function template in a program that calls isEqualTo only with a variety of built-in types. 34 实验指导手册 Now write a separate version of the program that calls isEqualTo with a user-defined class type Complex, but does not overload the equality operator. What happens when you attempt to run this program? Now overload the equality operator (with the operator function) operator ==. Now what happens when you attempt to run this program?
时间: 2023-11-22 17:53:52 浏览: 128
Here is the function template for `isEqualTo`:
```
template<typename T>
bool isEqualTo(const T& a, const T& b) {
return a == b;
}
```
We can use this function template in a program to compare built-in types like so:
```
#include <iostream>
int main() {
int a = 5, b = 7;
std::cout << isEqualTo(a, b) << std::endl; // false
double c = 3.14, d = 3.14;
std::cout << isEqualTo(c, d) << std::endl; // true
char e = 'a', f = 'b';
std::cout << isEqualTo(e, f) << std::endl; // false
return 0;
}
```
When we attempt to run this program with a user-defined class type `Complex` without overloading the equality operator, we will get a compilation error because the compiler does not know how to compare two `Complex` objects with the equality operator.
Once we overload the equality operator for the `Complex` class, the program will compile and execute without errors, allowing us to compare `Complex` objects using the `isEqualTo` function template. For example:
```
#include <iostream>
class Complex {
public:
Complex(double r, double i) : real(r), imag(i) {}
double real, imag;
};
bool operator==(const Complex& a, const Complex& b) {
return a.real == b.real && a.imag == b.imag;
}
int main() {
Complex x(1.0, 2.0), y(1.0, 2.0);
std::cout << isEqualTo(x, y) << std::endl; // true
Complex z(3.0, 4.0);
std::cout << isEqualTo(x, z) << std::endl; // false
return 0;
}
```
In this program, we have defined a `Complex` class with a constructor that initializes its real and imaginary parts. We have also overloaded the equality operator to compare two `Complex` objects by their real and imaginary parts. We can now use the `isEqualTo` function template to compare `Complex` objects and get the expected results.
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)