定义一个包含 5 名学生信息的结构数组,在主西数中对该结构数组的所有元表 进行初始化,并输出数组中第3个同学的学号、姓名、平均分
时间: 2024-03-08 11:49:37 浏览: 26
好的,我来为您演示如何定义并初始化这个结构数组,并输出第3个同学的信息。
```c
#include <stdio.h>
struct student {
int id; // 学号
char name[20]; // 姓名
float score; // 平均分
};
int main() {
// 定义并初始化结构数组
struct student stu[5] = {
{ 1, "张三", 89.5 },
{ 2, "李四", 78.0 },
{ 3, "王五", 92.5 },
{ 4, "赵六", 85.0 },
{ 5, "钱七", 79.5 }
};
// 输出第3个同学的学号、姓名、平均分
printf("第3个同学的学号:%d\n", stu[2].id);
printf("第3个同学的姓名:%s\n", stu[2].name);
printf("第3个同学的平均分:%.2f\n", stu[2].score);
return 0;
}
```
输出结果为:
```
第3个同学的学号:3
第3个同学的姓名:王五
第3个同学的平均分:92.50
```
相关问题
定义一个具有数组性质的类,对该类重载操作符“口” 实现数组元素的访问。要求:在操作符“口”重载西数中对下标进行检查,越界时拋掷一个异常对象。
下面是一个示例代码:
```c++
#include <iostream>
#include <exception>
class MyArray {
private:
int* data;
int size;
public:
MyArray(int n) {
size = n;
data = new int[size];
}
int& operator[](int index) {
if (index < 0 || index >= size) {
throw std::out_of_range("Index out of range!");
}
return data[index];
}
~MyArray() {
delete[] data;
}
};
int main() {
MyArray arr(3);
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
try {
std::cout << arr[3] << std::endl;
} catch (std::out_of_range& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
```
在这个示例中,我们定义了一个名为 `MyArray` 的类,它有一个整型指针 `data` 和一个整型 `size`,用于存储数组元素和数组大小。我们重载了操作符 `[]`,并且在重载函数中检查了下标是否越界。如果越界,我们拋出了一个 `std::out_of_range` 类型的异常对象。在主函数中,我们创建了一个 `MyArray` 对象并初始化了其中的元素,接着我们尝试访问一个越界的元素,捕获了异常并输出了异常信息。
从立方体、园柱体抽象出一个公共基类Container,定义抽象类Container,模拟实现一个容器类层次结构,在主西数进行多态机制测试。各派生类要求实现基类的所有纯虛函数。
好的,我可以给您提供一个示例代码,供您参考:
```c++
#include <iostream>
using namespace std;
// 容器的抽象基类
class Container {
public:
virtual double getVolume() const = 0; // 获取容积
virtual double getSurfaceArea() const = 0; // 获取表面积
};
// 立方体类
class Cube : public Container {
private:
double length_; // 立方体的边长
public:
Cube(double length) : length_(length) {}
double getVolume() const override { return length_ * length_ * length_; }
double getSurfaceArea() const override { return 6 * length_ * length_; }
};
// 圆柱体类
class Cylinder : public Container {
private:
double radius_; // 圆柱体的底面半径
double height_; // 圆柱体的高度
public:
Cylinder(double radius, double height) : radius_(radius), height_(height) {}
double getVolume() const override { return 3.14 * radius_ * radius_ * height_; }
double getSurfaceArea() const override { return 2 * 3.14 * radius_ * height_ + 2 * 3.14 * radius_ * radius_; }
};
// 主函数
int main() {
Container* containers[2];
containers[0] = new Cube(2);
containers[1] = new Cylinder(1, 3);
for (int i = 0; i < 2; i++) {
cout << "Container " << (i + 1) << " volume: " << containers[i]->getVolume() << endl;
cout << "Container " << (i + 1) << " surface area: " << containers[i]->getSurfaceArea() << endl;
}
return 0;
}
```
在这个示例代码中,我们定义了一个抽象基类`Container`,并且在其中定义了两个纯虚函数`getVolume()`和`getSurfaceArea()`。然后,我们又分别定义了一个派生类`Cube`和一个派生类`Cylinder`,并且在这两个派生类中都实现了基类的所有纯虚函数。最后,在主函数中,我们创建了一个`Container`类型的数组,并分别用`Cube`和`Cylinder`类的对象进行初始化。在循环中,我们遍历这个数组,并且调用每一个对象的`getVolume()`和`getSurfaceArea()`方法,以测试多态机制的正确性。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)