编写一个C++程序,演示函数重载
时间: 2023-05-17 10:04:18 浏览: 105
当然可以!函数重载是指在同一作用域内,可以定义多个同名函数,但是它们的参数类型或参数个数必须不同。下面是一个简单的例子:
```c
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
int main() {
int x = add(1, 2);
float y = add(1.5, 2.5);
printf("x = %d\n", x);
printf("y = %f\n", y);
return 0;
}
```
在这个例子中,我们定义了两个同名的函数 `add`,一个用于整数相加,一个用于浮点数相加。在 `main` 函数中,我们分别调用了这两个函数,并打印出它们的返回值。这就是函数重载的基本用法。
相关问题
c++函数重载,编程实现字符数组和整型数组的的逆向存放,并编写完整的主程序验证。
好的,关于您的问题,C++提供了函数重载的机制,可以根据不同的参数类型和数量,编写同名但不同参数列表的函数。对于字符数组和整型数组的逆向存放,可以分别写一个函数,实现两种不同类型的数组倒序存储。具体的代码实现如下:
```
#include <iostream>
#include <cstring>
using namespace std;
void reverse(char *str, int len) {
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
void reverse(int *arr, int len) {
for (int i = 0; i < len / 2; i++) {
int temp = arr[i];
arr[i] = arr[len - i - 1];
arr[len - i - 1] = temp;
}
}
int main() {
char str[] = "Hello World!";
int arr[] = {1, 2, 3, 4, 5};
reverse(str, strlen(str));
reverse(arr, 5);
cout << str << endl;
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
在以上代码中,我们首先定义了两个函数 `reverse`,一个用于字符数组的倒序存储,另一个用于整型数组的倒序存储。在 `reverse` 函数内部,我们使用了类似于冒泡排序的思路,用一个临时变量来交换数组中的元素,从而实现倒序存储。在主函数中,我们分别定义一个字符数组 `str` 和整型数组 `arr`,并调用两个 `reverse` 函数,将它们倒序存储。最后,我们输出倒序存储后的字符数组和整型数组。
需要注意的是,该程序只是演示了 C++ 中字符数组和整型数组的倒序存储实现,实际上还需要进一步的错误处理和边界检查,以确保程序运行的正确性和稳定性。
第1关:普通函数重载算术运算符
当我们在编写程序时,经常需要对自定义类型进行算术运算。C++ 中提供了运算符重载功能,使得我们可以通过重载运算符来定义自定义类型的算术运算。其中,重载算术运算符是比较常见的一种情况。
对于普通函数,我们可以通过重载算术运算符来实现对自定义类型的算术运算。例如,我们可以通过重载“+”运算符来实现自定义类型的加法运算。
下面是一个示例代码,演示了如何重载“+”运算符来实现自定义类型的加法运算。
```cpp
#include <iostream>
class MyInt
{
public:
MyInt(int val = 0) : m_val(val) {}
int getValue() const { return m_val; }
// 重载 "+" 运算符,实现 MyInt 类型的加法运算
MyInt operator+(const MyInt& rhs) const
{
return MyInt(m_val + rhs.m_val);
}
private:
int m_val;
};
int main()
{
MyInt a(1);
MyInt b(2);
MyInt c = a + b;
std::cout << "a + b = " << c.getValue() << std::endl;
return 0;
}
```
在上面的示例代码中,我们定义了一个 MyInt 类,其中包含一个整型成员变量 m_val。我们通过重载“+”运算符来实现 MyInt 类型的加法运算,重载函数的返回值为 MyInt 类型,参数为 const MyInt& 类型的引用。
在 main 函数中,我们创建了两个 MyInt 类型的对象 a 和 b,然后通过 a + b 的方式进行加法运算,将结果赋值给变量 c。最后,我们输出了变量 c 的值,也就是 a 和 b 的和。
这就是普通函数重载算术运算符的一个简单示例。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)