本题要求定义一个简单的学生类,数据成员仅需要定义学号和姓名,函数成员的原型见给出的代码,请给出函数成员的类外完整实现。 函数接口定义: class Student { private: int m_id; char m_name[10]; public: Student(int id=0,char *name="NULL"); ~Student(); void print(); }; 其中m_id和m_name分别表示学生的学号和姓名,类型已经定义好。类内声明了3个成员函数,分别表示构造函数、析构函数和用于输出学生信息的函数。 构造函数要完成两个数据成员的初始赋值,并做一行输出,形如:“Hi!学号 姓名”。 析构函数要求输出一行,形如:“Bye!学号 姓名”。 print函数要求在一行中输出学生信息,形如:“学号 姓名”。 注:学号和姓名之间用一个空格分开。 裁判测试程序样例: #include <iostream> #include <cstring> using namespace std; int main() { Student stu_array[3]={Student(1,"Zhang"),Student(2,"Wang")}; return 0; }
时间: 2024-02-29 10:52:34 浏览: 17
完整实现如下:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class Student {
private:
int m_id;
char m_name[10];
public:
Student(int id=0, char *name="NULL") { // 构造函数
m_id = id;
strcpy(m_name, name);
cout << "Hi!" << m_id << " " << m_name << endl;
}
~Student() { // 析构函数
cout << "Bye!" << m_id << " " << m_name << endl;
}
void print() { // 输出学生信息
cout << m_id << " " << m_name << endl;
}
};
int main() {
Student stu_array[3] = {Student(1, "Zhang"), Student(2, "Wang")};
stu_array[0].print();
stu_array[1].print();
return 0;
}
```
相关问题
本题要求定义一个简单的学生类,数据成员仅需要定义学号和姓名,函数成员的原型见给出的代码,请给出函数成员的类外完整实现。 函数接口定义: class Student { private: int m_id; char m_name[10]; public: Student(int id=0,char *name="NULL"); ~Student(); void print(); };
以下是完整的类外实现代码:
```
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
private:
int m_id;
char m_name[10];
public:
Student(int id, char* name)
{
m_id = id;
strncpy(m_name, name, 10);
}
~Student()
{
cout << "student object has been destroyed" << endl;
}
void print()
{
cout << "Student ID: " << m_id << endl;
cout << "Student Name: " << m_name << endl;
}
};
int main()
{
Student s(123, "Bob");
s.print();
return 0;
}
```
在这个实现中,我们定义了一个 Student 类,它有两个数据成员:m_id 和 m_name,分别表示学号和姓名。构造函数 Student(int id, char* name) 用于初始化这两个数据成员,析构函数 ~Student() 用于在对象销毁时输出一条消息。函数成员 print() 用于打印学号和姓名。在 main 函数中,我们创建了一个 Student 对象 s,调用了它的 print 函数。
6-2 2020Student类与对象数组分数 10 全屏浏览题目 切换布局 作者 shou 单位 上海海洋大学 本题要求定义一个简单的学生类,数据成员有学号、姓名和一门课成绩分数,函数成员的原型见给出的代码,请给出函数成员的类外完整实现。 此外,还需要实现一个显示学生对象数组的函数。函数接口定义如下: void disp_students(Student a[], int n); 其中,a 为学生对象数组,n为对象数组元素个数。
以下是完整的类外实现,包括函数成员的定义和`disp_students`函数的实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name; // 学生姓名
string id; // 学生学号
int score; // 学生成绩
public:
Student(string n, string i, int s);
string getName();
string getId();
int getScore();
};
Student::Student(string n, string i, int s)
{
name = n;
id = i;
score = s;
}
string Student::getName()
{
return name;
}
string Student::getId()
{
return id;
}
int Student::getScore()
{
return score;
}
void disp_students(Student a[], int n)
{
for (int i = 0; i < n; i++) {
cout << "姓名:" << a[i].getName() << endl;
cout << "学号:" << a[i].getId() << endl;
cout << "分数:" << a[i].getScore() << endl;
cout << endl;
}
}
```
在这个实现中,我们定义了一个名为`Student`的类,它有三个私有数据成员:`name`、`id`和`score`,分别表示学生姓名、学号和成绩。我们提供了一个构造函数`Student(string n, string i, int s)`来初始化这些数据成员。
然后,我们还提供了三个公有成员函数`getName()`、`getId()`和`getScore()`,用于获取学生的姓名、学号和成绩信息。
最后,我们还实现了一个`disp_students`函数,用于显示一个学生对象数组的信息。这个函数接受一个`Student`对象数组和数组元素个数作为参数,然后遍历数组并输出每个学生的姓名、学号和成绩信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)