hw10_01.cpp

1、(a) 设计一个名为Score的类,表示成绩,这个类包括:
两个int型数据成员:math和eng,分别表示数学成绩和英语成绩
一个带两个形参的构造函数,用给定的分数初始化math和eng
成员函数show(),输出数学成绩和英语成绩
(b) 设计一个名为Student 的类,表示学生,这个类包括:
两个数据成员:stuid(int型,表示学号)和mark(Score对象)
一个带三个形参的构造函数,对数据成员进行初始化
成员函数stushow(),输出学号和相应的成绩
实现这两个类,并在主函数中测试这个类:创建一个学生:学号为2017007,数学成绩为98,英语成绩为85,在屏幕上输出该生的学号和成绩。(程序取名为hw10_01.cpp)
#include <iostream>
#include<cmath>
using namespace std;
class Score {
    public:
    Score(int x,int y) {
        eng=x;math=y;
    }
    void Show() const{
        cout<<"eng:"<<eng<<endl;
        cout<<"math:"<<math<<endl;
    }
    private:
    int eng,math;
};
class Student {
    public:
    Student(int x,int eng,int math)
    : mark(eng,math)
    {
        stuid=x;
    }
    void stushow() const {
        cout<<"stuid:"<<stuid<<endl;
        mark.Show();
    }
private:
    int stuid;
    Score mark;
};
int main() { Student stu(2017007,85,97); stu.stushow(); }

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部