Problem Description
某校每位学生都要学习语文、英语、数学三门公共课。
会计学专业学生要学习会计学和经济学2门专业课,
化学专业学生要学习有机化学和化学分析2门专业课。
(1)编写抽象基类comFinal,数据成员有:姓名(字符数组类型)、语文成绩、英语成绩、数学成绩。
成员函数有:构造函数、析构函数、计算课程总分的纯虚函数Sum、显示学生所有信息的纯虚函数Display。
(2)编写继承comFinal的派生类Account,数据成员有:会计学成绩、经济学成绩。
(3)编写继承comFinal的派生类Chemistry,数据成员有:有机化学成绩、分析化学成绩。
完善以下程序
//你的代码写在这里
int main(void)
{
ComFinal* p = new Account (“AAA”, 90, 86, 80, 93, 91);
p->Display();
delete p;
p = new Chemistry(“BBB”, 90, 86, 80, 93, 91);
p->Display();
delete p;
return 0;
}
Sample Output
姓名:AAA
语文:90
英语:86
数学:80
会计学:93
经济学:91
课程总分:440
姓名:BBB
语文:90
英语:86
数学:80
有机化学:93
化学分析:91
课程总分:440
#include<iostream>
using namespace std;
#include <string>
class ComFinal
{
public:
string name;
int china,english,math;
ComFinal(){}
virtual int sum() = 0;//纯虚函数 =0!!!
virtual void Display() = 0;
};
class Account :public ComFinal
{
public:
int kuaiji, jingji;
Account(string a,int b,int c,int d,int e,int f)
{
name = a;
china = b;
english = c;
math = d;
kuaiji = e;
jingji = f;
}
int sum()
{
return china + math + english + kuaiji + jingji;
}
void Display()
{
cout << "姓名:" << name << endl;
cout << "语文:" << china << endl;
cout << "英语:" << english<< endl;
cout << "数学:" << math << endl;
cout << "会计学:" << kuaiji << endl;
cout << "经济学:" << jingji << endl;
cout << "课程总分:" << sum() << endl;
}
};
class Chemistry :public ComFinal
{
public:
int youji, fenxi;
Chemistry(string a, int b, int c, int d, int e, int f)
{
name = a;
china = b;
english = c;
math = d;
youji = e;
fenxi = f;
}
int sum()
{
return china + math + english + youji + fenxi;
}
void Display()
{
cout << "姓名:" << name << endl;
cout << "语文:" << china << endl;
cout << "英语:" << english << endl;
cout << "数学:" << math << endl;
cout << "有机化学:" << youji << endl;
cout << "分析化学:" << fenxi << endl;
cout << "课程总分:" << sum() << endl;
}
};
int main(void)
{
ComFinal* p = new Account("AAA", 90, 86, 80, 93, 91);
p->Display();
delete p;
p = new Chemistry("BBB", 90, 86, 80, 93, 91);
p->Display();
delete p;
return 0;
}
该博客介绍了如何使用C++创建一个抽象基类`ComFinal`,用于表示学生的基础信息和公共课程成绩。派生类`Account`和`Chemistry`分别表示会计学专业和化学专业学生,包含各自的专业课程成绩。在`main`函数中,通过实例化这两个派生类对象并调用`Display`方法展示了学生的所有信息和课程总分。

583

被折叠的 条评论
为什么被折叠?



