- 1、本文档共12页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
计算机与信息学院
实 验 报 告
课程名称: C++面向对象程序设计
班 级: B 15 计信
学 号:
姓 名: 陈 立
指导教师: 潘 炯 光
实验项目名称
C++类和对象的特性
及其应用
实验学时
实验日期
2016年4月26日
实验地点
软件工程实验室J1303
实验成绩
实验目的
掌握工程程序的基本编程方法,了解类的封装性和信息隐蔽。
掌握对象的构造函数、析构函数、对象数组、对象指针、对象引用。
掌握对象共用数据的保护(const)、动态建立和释放、赋值和复制。
掌握对象的静态成员、友元函数、友元类、类模板。
实验内容
第二章习题5
第三章习题4、5、9、10、11、12
实验设备及软件环境
计算机,Windows?XP以上操作系统,Visual?C++?6.0?软件
实验方法及基本操作步骤
第二章 习题5
5、将本章的例2.4改写为一个多文件的程序:
(1)将类定义放在头文件arraymax.h中;
(2)将成员函数定义放在源文件arraymax.cpp中;
(3)将主函数放在源文件filel.cpp中。
//arraymax.h
#include iostream
using namespace std;
class Array_max
{public:
void set_value();
void max_value();
void show_value();
private:
int array[10];
int max;
};
//arraymax.cpp
#include iostream
#include arraymax.h
using namespace std;
void Array_max::set_value()
{ int i;
for (i=0;i10;i++)
cinarray[i];
}
void Array_max::max_value()
{int i;
max=array[0];
for (i=1;i10;i++)
if(array[i]max) max=array[i];
}
void Array_max::show_value()
{coutmax=max;
}
//filel.cpp
#include iostream
#include arraymax.h
using namespace std;
int main()
{Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return 0;
}
运行结果:
14 24 35 78 92 47 34 24 54 99
Max=99
第三章 习题4、5、9、10、11、12
4、建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。
#include iostream
using namespace std;
class Student
{
public:void get_student();
void display();
private:int num;int score;
};
void Student::get_student(){
cinnumscore;
}
void Student::display()
{coutnum=num\tscore=scoreendl;}
int main(){int i;Student *p;p=new Student[];Student *t = p;
for(i=0;i5;i++){
cout请输入第i+1名学号和成绩:;p-get_student();
p++;
}cout输出第1.3.5个学生的数据:\n;
p=t;
for(i=0;i5;i=i+2)
{
p-display();
p++;
p++;
}delete p; delete t;return 0;}
运行结果:
5、建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
#include iostream
using namespace std;
class Student
{ private:
文档评论(0)