面向对象程序设计c实验报告_(编辑修改稿)内容摘要:

地址 student 类增加两个静态成员,人数( count)和平均分( average)。 注意构造函数、析构函数的编写以及静态成员的初始化,并说明这两个成员设置为静态成员的原因。 include iostream include string using namespace std。 class student { private: int sno。 char *name。 int age。 int score。 static int count。 static float average。 static float sum。 public: void input() { cinsnonameagescore。 } void print() { cout学号 snoendl。 cout姓名 nameendl。 cout年龄 ageendl。 cout分数 scoreendl。 } void show() { cout学生人数 :count。 cout平均成绩 :averageendl。 } student(int snol,char *namel,int agel,int scorel) { sno=snol。 name=new char[strlen(namel)+1]。 strcpy(name,namel)。 age=agel。 score=scorel。 ++count。 sum=sum+score。 average=sum/count。 } ~student() { delete name。 count。 sum=sumscore。 } }。 int student::count=0。 float student::sum=。 float student::average=。 int main() student a1(1,小小 ,20,99)。 ()。 ()。 student a2(2,小江 ,12,100)。 ()。 ()。 student a3(3,肖肖 ,21,89)。 ()。 ()。 return 0。 } count 随着定义对象的增加而增加。 每定义一个对象, count 加 1,累计学生人数, average也应该随着定义对象的增加而改变着。 因此它们被声明为静态数据成员,被 studengt 类的对象所共享。 output,实现和成员函数 print 相同的功能。 说明友元函数作用以及和成员函数的区别。 include iostream include string using namespace std。 class student { private: int sno。 string name。 int age。 int score。 public: void input() { cinsnonameagescore。 } friend void output(student amp。 )。 void print() { cout学号 snoendl。 cout姓名 nameendl。 cout年龄 ageendl。 cout分数 scoreendl。 } }。 void output(student amp。 p) { cout学号 姓名 endl。 cout年龄 分数 endl。 } int main() { student k[10]。 int i。 for(i=0。 i=9。 i++) { k[i].input()。 output(k[i])。 } } 友员函数可以通过入口参数传递进来的对象名(或对象指针、对象引用)来访问该对象所有的的数据成员,提高了程序运行效率。 没有友员机制,外部函数访问类的私有数据,必须调用公有成员函数,在需要频繁调用私有数据时,会带来很大的开销,降低程序运行效率。 实验五 派生类与继承 院(系): 课程名称: 面向对象程序设计 教师签名: 班级 学号 实验室 姓名 实验成绩 所用软件 Word 文档 、 VC++软件 实验目的和要求 1 理解类的继承的概念,能够定义和使用类的继承关系。 实验内容 1. 定义一个基类 MyArray,基类中可以存放一组整数。 class MyArray{ public: MyArray(int leng)。 ~MyArray()。 void Input()。 void Display()。 protected: int *alist。 //指向动态申请的一组空间 int length。 //整数的个数 }。 基类中有构造函数、析构函数、输入数据和输出数据的函数。 include class MyArray{ public: MyArray(int leng)。 ~MyArray()。 void Input()。 void Display()。 protected: int *alist。 //指向动态申请的一组空间 int length。 //整数的个数 }。 MyArray::MyArray(int leng) { alist=new int[leng]。 length=leng。 } MyArray::~MyArray() { delete []alist。 cout显示调用了析构函数 endl。 } void MyArray::Input() { int i。 cout请输入数组元素: endl。 for(i=0。 ilength。 i++) { cinalist[i]。 } } void MyArray::Display() { int j。 cout输出的数组元素为: endl。 for(j=0。 jlength。 j++) { coutalist[j]endl。 } } int main() { MyArray s(4)。 ()。 ()。 } 2. 定义一个类 SortArray 继承自 MyArray,在该类中定义函数实现排序功能。 includeiostream using namespace std。 class MyArray {public: MyArray(int leng) { length=leng。 alist=new int[length]。 } ~MyArray() {delete alist。 } void input() { int i。 for(i=0。 ilength。 i++) cinalist[i]。 } void display() { int j。 for(j=0。 jlength。 j++) coutalist[j]endl。 } protected: int *alist。 int length。 }。 class SortArray:public MyArray { public: SortArray(int leng):MyArray(leng) {} void pai() { int i。 int j。 int t。 for(i=0。 ilength。 i++) {for(j=i+1。 jlength。 j++) {if(alist[i]alist[j]) { t=alist[i]。 alist[i]=alist[j]。 alist[j]=t。 } } } for(i=0。 ilength。 i++) coutalist[i]。 } }。 int main() { int leng。 cinleng。 SortArray A(leng)。 ()。 ()。 ()。 return(0)。 } 3. 定义一个类 ReArray 继承自 MyArray,在该类中定义函数实现逆转功能。 includeiostream using namespace std。 class MyArray {public: MyArray(int leng) { length=leng。 alist=new int[length]。 } ~MyArray() {delete alist。 } void input() { int i。 for(i=0。 ilength。 i++) cinalist[i]。 } void display() { int j。 for(j=0。 jlength。 j++) coutalist[j]endl。 } protected: int *alist。 int length。 }。 class ReArray:public MyArray { public: ReArray(int leng):MyArray(leng) {} void nizhuan() { int j。 int t。 for(j=0。 jlength/2。 j++) { t=alist[j]。 alist[j]=alist[length1j]。 alist[lengthj1]=t。 } for(j=0。 jlength。 j++) {coutalist[j]。 } } }。 int main() { int leng。 cinleng。 ReArray A(leng)。 ()。 ()。 ()。 return(0)。 } 4. 定义一个类 AverArray 继承自 MyArray,在该类中定义函数 Aver 求解整数的平均值。 includeiostream using namespace std。 class MyArray {public: MyArray(int leng) { length=leng。 alist=new int[length]。 } ~MyArray() {delete alist。 } void input() { int i。 for(i=0。 ilength。 i++) cinalist[i]。 } void display() { int j。 for(j=0。 jlength。 j++) coutalist[j]endl。 } protected: int *alist。 int length。 }。 class AverArray:public MyArray { public: AverArray(int leng):MyArray(leng) {} void pingjun() { int j。 float sum。 float ave。 sum=。 for(j=0。 jlength。 j++) { sum+=alist[j]。 } ave=sum/length。 coutaveendl。 } }。 int main() { int leng。 cinleng。 AverArray A(leng)。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。