1348 [BA1000] The Student Class with Public Data Fields

Description

 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

Steven Gates (100) is 61 years old.
Larry Jordan (123) is 18 years old.
No Name (124) is 0 years old.

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

#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

#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

#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

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值