写出func函数的三个重载函数value of x is 7 value of x is 9.132 value of x and y is 85,64
时间: 2024-04-29 08:22:13 浏览: 94
下面是三个重载函数的代码示例:
```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函数的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
```
阅读全文