《C++程序设计》
实验报告
实验名称: 继承与组合 * 名: ** 学 号: ******** 专 业: 通信工程 实验时间: 2016.11.15
杭州电子科技大学 通信工程学院
一、实验目的
1. 了解继承在面向对象程序设计中的重要作用。 2. 进一步理解继承与派生的概念。 3. 掌握通过继承派生出一个新类的方法。 4. 了解虚基类的作用和用法。 5. 掌握类的组合。 二、实验内容
1、 请先阅读下面的程序,分析程序运行的结果,然后再上机运行程序,验证自己分析的结果
是否正确 4、 按下列要求编写程序。
(1)定义一个分数类score。 它有3个数据成员:
Chinese //语文课成绩 English //英语课成绩 Mathematics //数学课成绩 2个构造函数:无参的和带参数的。
3个成员函数:是否带参数根据需要自定。 sum () //计算三门课总成绩 prin() //输出三门课成绩和总成绩 modify() //修改三门课成绩
(2)定义一个学生类student。 它有3个数据成员:
Num //学号 \" Name //姓名 MyScore //成绩 I
2个构造函数:无参的和带参数的
3个成员函数:是否带参数根据需要自定。 Sum() //计算某学生三门课总成绩 Print() //输出某学生学号、姓名和成绩 modify() //修改某学生学号、姓名和成绩
(3)在主函数中,先定义一个学生类对象数组,再通过for循环给对象数组赋上实际值,最后输出对象数组各元素的值。
三、实验过程及实验结果 第1题: 程序1
#include using namespace std; class A {public:
A(){ cout<<\"A::A()called.\\n\"; } ~A(){ cout<<\"A::~A()called.\\n\"; } };
class B:public A {
public:
B(int i) {
cout<<\"B::B()called.\\n\"; buf=new char[i]; }
virtual ~B() {
delete []buf;
cout<<\"B::~B() called.\\n\"; } private:
char *buf; };
int main() {
B b(10); return 0; }
实验结果:
程序2
#include using namespace std; class A{ public:A(int a, int b):x(a), y(b){ cout<<\"A constructor...\"<void display(){ cout<<\"(\"<int x, y; };class B:private A{ private:
int i, j; A Aobj; public:
B(int a, int b, int c, int d):A(a, b), i(c), j(d), Aobj(1,1) { cout<<\"Bconstructor..\"<void Add(int x1, int y1, int x2, int y2) {A::Add(x1, y1); i+=x2; j+=y2; }
void display(){ A::display(); Aobj.display();
cout<<\"(\"<~B(){ cout<<\"destructor B...\"<int main() {B b(1,2,3,4); b.display(); b.Add(1,3,5,7); b.display(); return 0; }
实验结果:
程序3
#include using namespace std; class A{ public:A(int a):x(a){ cout<<\"A constructor...\"<~A(){cout<<\"destructor A...\"<class B:private virtual A{ private: int y; A Aobj; public:B(int a, int b, int c):A(a), y(c), Aobj(c) { cout<<\"B constructor..\"<void display(){cout<~B(){ cout<<\"destructor B...\"<class C: public B{ public:C(int a, int b, int c): B(a, b, c), A(0){ cout<<\"C constructor...\"<class D:public C, public virtual A{ public:D(int a, int b, int c): C(a, b, c), A(c){ cout<<\"D constructor...\"<int main() {D d(7,8,9); d.f();
d.display(); return 0; }
实验结果:
第4题
1.score头文件
#if ! define SCORE_H #define SCORE_H class Score {
public:
Score();
Score(float x1, float y1, float z1); float sum(); void print();
void modify(float x2, float y2, float z2); private:
float computer; float english; float mathematics; }; #endif
2.score源文件
#include #include\"score.h\" #includeScore::Score(){ computer=0; english=0; mathematics=0; } Score::Score(float x1, float y1, float z1){ computer=x1; english=y1; mathematics=z1; } float Score::sum(){ return (computer+english+mathematics);} void Score::print() {
cout<void Score::modify(float x2, float y2, float z2){ computer=x2; english=y2; mathematics=z2; } 3.student头文件 #include\"score.h\" class Student {private:
int number; char name[20]; Score ascore; public:
Student();
Student(int number1, char* pname1, float score1, float score2, float score3); float sum(); void print();
void modify(int number2, char* pname2, float score21, float score22, float
score23);};
4.student源文件
#include #include\"student.h\" #include #includeStudent::Student():ascore(){number=0;}
Student::Student(int number1, char* pname1, float score1, float score2, float score3):ascore(score1, score2, score3) {
number=number1;
strncpy(name, pname1, sizeof(name)); name[sizeof(name)-1]='\\0'; }
float Student::sum(){ return(ascore.sum()); } void Student::print() {
cout<cout<void Student::modify(int number2, char* pname2, float score21, float score22, float score23) {number=number2;
strncpy(name, pname2, sizeof(name)); name[sizeof(name)-1]='\\0';
ascore.modify(score21, score22, score23); }
5.main函数源文件 #include #include\"student.h\" #include const size=3; void main() {int numberi; char namei[20];
float score1, score2, score3; Student aSA[size];
for(int i=0; icout<<\"please input the data of NO.\"<>numberi; cout<<\"name:\";cin>>namei;
cout<<\"score of computer:\"; cin>>score1;
cout<<\"score of english:\"; cin>>score2;
cout<<\"score of mathematics:\"; cin>>score3;
aSA[i].modify(numberi, namei, score1, score2, score3); }
cout<<\"\\n\\n\";
cout<for(int j=0;j四、实验小结通过本次试验,我初步了解了继承在面向对象程序设计中的重要作用。通过做第一道练习题,更深地理解了继承与派生的概念。后面几道练习题,则让我掌握通过继承派生出一个新类的方法。
在程序运行失败的时候,学会了通过检查和一步步的调试来修改程序,直至运行成功为止。对虚基类的作用和类的组合更加熟悉了,对c++了解和运用也愈加熟练了。