写出func函数的三个重载函数value of x is 7 value of x is 9.132 value of x and y is 85,64
时间: 2024-04-29 14:22:13 浏览: 17
下面是三个重载函数的代码示例:
```python
def func(x: int) -> None:
print(f"value of x is {x}")
def func(x: float) -> None:
print(f"value of x is {x:.3f}")
def func(x: int, y: int) -> None:
print(f"value of x and y is {x},{y}")
```
调用这些函数的方法如下:
```python
func(7)
func(9.132)
func(85, 64)
```
输出结果分别为:
```
value of x is 7
value of x is 9.132
value of x and y is 85,64
```
相关问题
写出func函数的三个重载函数,使得输出为 value of x is7
以下是三个重载函数的实现:
```c++
#include <iostream>
#include <string>
void func(const int& x) { // 接收整型常量引用
std::cout << "value of x is" << x << std::endl;
}
void func(const std::string& x) { // 接收字符串常量引用
std::cout << "value of x is" << x << std::endl;
}
void func(char* x) { // 接收字符指针
std::cout << "value of x is" << x << std::endl;
}
int main() {
func(7); // 调用第一个重载函数
func(std::to_string(7)); // 调用第二个重载函数
func("7"); // 调用第三个重载函数
return 0;
}
```
其中第一个重载函数接收整型常量引用,第二个重载函数接收字符串常量引用,第三个重载函数接收字符指针。在 `main` 函数中分别调用这三个函数即可输出 "value of x is7"。
本关任务:编译时的多态,根据上下文代码,写出func函数的3个重载函数,使得输出为: value of x is 7 value of x is 9.132 value of x and y is 85, 64
以下是可能的实现:
```c++
#include <iostream>
using namespace std;
void func(int x) {
cout << "value of x is " << x << endl;
}
void func(double x) {
cout << "value of x is " << x << endl;
}
void func(int x, int y) {
cout << "value of x and y is " << x << ", " << y << endl;
}
int main() {
int x = 7;
double y = 9.132;
func(x);
func(y);
func(x, y);
return 0;
}
```
输出结果:
```
value of x is 7
value of x is 9.132
value of x and y is 7, 9
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)