[C++学生类] The Student Class

[C++学生类] The Student Class

题目

 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.

输入

输出

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

代码答案

main.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;
}

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值