【填空题】下呅吥呉呄吥输呁呃果是: (4,5) (7,8) 填空使呉呄完呇。 #include <iostream> using namespace std; class Point { private: int x, y; public: Point(int x_, int y_) : x(x_), y(y_) { } ________________①__________________ ; }; _____②_____ operator<<(_______③_______ , const Point& p) { ____________④______________ ; return _________⑤__________ ; } int main() { cout << Point(4, 5) << Point(7, 8); return 0; }
时间: 2023-08-21 07:14:41 浏览: 42
下面是代码的完整实现,需要填空的地方已经标出:
```c++
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point(int x_, int y_) : x(x_), y(y_) { }
friend ostream& operator<<(ostream& os, const Point& p); // ①友元函数声明
};
ostream& operator<<(ostream& os, const Point& p) { // ②友元函数定义
os << "(" << p.x << "," << p.y << ")"; // ④输出运算符的具体实现
return os; // ⑤返回输出流对象
}
int main() {
cout << Point(4, 5) << Point(7, 8);
return 0;
}
```
解析:
- 本题考察了输出运算符的重载,需要将输出运算符定义为友元函数。
- 友元函数的声明需要写在类的声明中,并且需要在函数名前加上`friend`关键字。
- 友元函数的定义需要在类的外部进行,函数名前不需要加上`friend`关键字。
- 输出运算符的实现主要是将Point类对象的成员变量x和y输出到输出流对象中。
- 在`operator<<`函数中,需要返回输出流对象的引用,以便可以实现链式输出。
相关问题
c语言程序填空题:定义一个5*5矩阵整形,生成10-99随机数
在C语言中,可以使用rand()函数生成随机数。但是由于rand()函数生成的随机数是一个0-32767之间的整数,我们需要进行适当的处理,才能生成10-99之间的随机数。
首先需要使用srand()函数来初始化随机数种子,可以使用time()函数来设置随机数种子为当前时间值。
接着,可以使用一个嵌套循环来遍历5*5的矩阵,并使用rand()函数生成一个0-89之间的随机数。随机数加上10后,就可以得到10-99之间的随机数了。
下面是参考代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int matrix[5][5];
int i, j, num;
// use current time as seed for random generator
srand(time(0));
// generate 5x5 matrix with random numbers between 10 and 99
for(i = 0; i < 5; i++) {
for(j = 0; j < 5; j++) {
num = rand() % 90; // generate a random number between 0 and 89
matrix[i][j] = num + 10; // add 10 to get a number between 10 and 99
}
}
// print the generated matrix
for(i = 0; i < 5; i++) {
for(j = 0; j < 5; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
以上代码会生成一个5*5的矩阵,其中的每个元素都是10-99之间的随机整数。这个程序可以通过编译并运行,生成符合要求的随机数矩阵。
c语言数组的填空题5个
1. int arr[5] = {1, 2, ___ , 4, 5}; // 3
2. char str[6] = {'H', 'e', 'l', 'l', 'o', ___}; // '\0'
3. float float_arr[3] = {1.2, 3.4, ___}; // 5.6
4. int arr_2d[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, ___, 9}}; // 8
5. char str_2d[2][5] = {"Hello", "W___"}; // "orld"
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)