我是靠谱客的博主 坚定八宝粥,这篇文章主要介绍1348 [BA1000] The Student Class with Public Data Fields,现在分享给大家,希望可以做个参考。

Description

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
With a class "Student" declared as below: class Student { public: int id; char name[50]; // Data field int age; // Data field Student(); Student(int, char*, int); }; You are required to implement the class functions and also two other functions used to process Student objects, which can get output as specified later. void set(Student &, int, char*, int); void print(Student); 1) You don't need to submit the main function. 2) You don't need to include the header file for class declaration by yourself.

Output

复制代码
1
2
3
Steven Gates (100) is 61 years old. Larry Jordan (123) is 18 years old. No Name (124) is 0 years old.

提示,头文件请包涵如下代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream> #include<cstring> using namespace std; class Student { public: int id; char name[50]; // Data field int age; // Data field Student(); Student(int, char*, int); //void set(int, char*, int); //void print(); }; void set(Student &, int, char*, int); void print(Student);

Provided Codes

framework.cpp

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> #include <cstring> #include "source.h" using namespace std; int main() { Student std1, std2(123, "Larry Jordan", 18), std3(124); set(std1, 100, "Steven Gates", 61); print(std1); print(std2); print(std3); return 0; }

Submission

source.h

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream> #include <cstring> using namespace std; class Student { public: int id; char name[50]; // Data field int age; // Data field Student(); Student(int, char*, int); }; Student::Student(){ id=0; strcpy(name,"No Name"); age=0; } Student::Student(int Id,char* Name="No Name",int Age=0){ id=Id; strcpy(name,Name); age=Age; } void set(Student &stu, int Id, char* Name, int Age){ stu.id=Id; strcpy(stu.name,Name); stu.age=Age; } void print(Student stu){ cout<<stu.name<<" ("<<stu.id<<") is "<<stu.age<<" years old."<<endl; }

Standard Answer

source.h

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream> #include <cstring> using namespace std; class Student { public: int id; char name[50]; // Data field int age; // Data field Student(); Student(int, char*, int); //void set(int, char*, int); //void print(); }; void set(Student &, int, char*, int); void print(Student); Student::Student() { id=0; strcpy(name,"No Name"); age=0; } Student::Student(int pid,char* pname="No Name",int page=0) { id=pid; strcpy(name,pname); age=page; } void set(Student&stu,int id,char*name,int age) { stu.id=id; stu.age=age; strcpy(stu.name,name); } void print(Student stu) { cout<< stu.name <<" ("<<stu.id<<") "<<"is "<<stu.age<<" years old."<<endl; }

最后

以上就是坚定八宝粥最近收集整理的关于1348 [BA1000] The Student Class with Public Data Fields的全部内容,更多相关1348内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部