用c++ 编程:写一个学生类和课程类

3.1); lisi.add (&math char* argv[]){ course math("math"); course physics("physics"); stu zhangsan("zhangsan"); stu lisi("lisi"); zhangsan.add (&math cs)==0) return cours[i]->getscore(this->nm()); else cout << "he doesn't opt that course" << endl;}float stu::average (){ float sum=0; for (int i=0;i<cnt;++i) sum=cours[i]->getscore(this); return sum/cnt;}int main(int argc sc);}float stu::get_score(char* cs){ for (int i=0;i<cnt;++i) if (strcmp(cours[i]->nm() float sc){ cours[cnt++]=cs; cs->add(this nm);}void stu::add(course* cs nm)==0) return score[i]; else cout << "can not find that student" << endl;}float course::getscore (stu* st){ for (int i=0;i<cnt;++i) if (student[i]==st) return score[i];}float course::average(){ float sum=0; for (int i=0;i<cnt;++i) sum+=score[i]; return sum/cnt;}// student implementaionstu::stu(char* nm):cnt(0){ strcpy(name float sc){ student[cnt]=st; score[cnt++]=sc;}float course::getscore(char* nm){ for (int i=0;i<cnt;++i) if (strcmp(student[i]->nm() nm);}void course::add(stu* st float sc = 0); float get_score(char* nm); float average();};// course implemetaioncourse::course(char* nm):cnt(0){ strcpy(name float sc); float getscore(char* nm); float getscore(stu* st); float sc(int n){return score[n];} float average();};// student class class stu{ char name[20]; int cnt; course* cours[5];public: stu(char* nm); char* nm() {return name;} void add(course* cs #include <iostream.h>#include <string.h>class stu;#define size 20// course class class course { //static const size = 20; char name[20]; stu* student[size]; int cnt; float score[size];public: course(char* nm); char* nm() {return name;} void add(stu* st 4.5); cout << math.average (); int stop; cin >> stop; return 0;}应该可以的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 、先定义一个学生类student,其私有成员分别为(学号、生名、性别)3个,再定义一个研究生graduate(私有成员分别为:学号、生名、性别、年龄、地址)5个。其中研究生学生类继承过来,分别采用公有继承与私有继承、保护继承三种方式,程序中包括输入数据的函数,运行时输入其5项的值,程序应输出5个数据值。 答案:略 2、(1) 声明一个Animal,有私有整型成员变量age,构造其派生dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。 (2) 声明一个BaseClass,有整形成员变量Number,构造其派生DerivedClass,观察构造函数和析构函数的执行情况。 (3) 声明一个车(vehicle)基,具有MaxSpeed,Weight等成员变量,Run,Stop等成员函数,由此派生出自行车(bicycle),汽车(motorcar)。自行车(bicycle)有高度(Height)等属性,汽车(motorcar)有座位数(SeatNum)等属性。从bicycle和motorcar派生出摩托车(motocycle),在继承过程中,注意把vehicle设置为虚基。如果不把vehicle设置为虚基,会有什么问题?编程试试看。 (4)(选做)从people(人员)派生出student(学生),添加属性:班号 char classNo[7];从people派生出teacher(教师),添加属性:职务char principalship[11]、 部门char department[21]。从student中派生出graduate(研究生),添加属性:专业char subject[21]、导师teacherradviser;从graduate和teacher派生出TA(助教生),注意虚基的使用。重载相应的成员函数,测试这些

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值