教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 资格考试 >

实验四类和对象

来源:网络收集 时间:2025-09-13
导读: 电子信息学院 实验报告书 课 程 名: 面向对象程序设计 题 目: 实验4 类和对象 实验类别: 设计研究型 班 级: BX1004 学 号: 35 姓 名: 赵鑫 评语: 实验态度:认真( ) 一般( ) 差( ) 实验结果:正确( ) 部分正确( )错( ) 实验理论:掌握(

电子信息学院 实验报告书

课 程 名: 面向对象程序设计 题 目: 实验4 类和对象

实验类别: 设计研究型

班 级: BX1004 学 号: 35 姓 名: 赵鑫

评语: 实验态度:认真( ) 一般( ) 差( ) 实验结果:正确( ) 部分正确( )错( ) 实验理论:掌握( ) 熟悉( ) 了解( ) 不懂( ) 操作技能:强( ) 一般( ) 差( ) 实验报告:好( ) 一般( ) 差( ) 成绩: 指导教师: 陈群贤 批阅时间: 年 月 日

一.实验目的

(1)掌握声明类的方法,类和类的成员的概念以及定义对象的方法。 (2)初步掌握用类和对象编制基于对象的程序 (3)学习检查和调试基于对象的程序 二.实验内容

(1)有以下程序:

#include using namespace std;

class Time //定义Time类

{ public: //数据成员为公用的 int hour; int minute; int sec; };

int main()

{ Time t1; //定义他为Time类对象 cin>>ti.hour; //输入设定时间 cin>>ti.minute; cin>>ti.sec;

cout<

改写程序,要求:

?将数据成员改为私有的;

?将输入和输出的功能改为由成员函数实现; ?在类体内定义成员函数。 然后编译和运行程序。请分析什么成员应指定为公用的?什么成员应指定为私有的?什么函数最好放在类中定义?什么函数最好在类外定义?

(2)分别给出如下的3个文件: ?含类定义的头文件student.h。

//student.h (这是头文件,在此文件中进行类的声明) class Student //类声明 {public:

void display(); //公用成员函数原型声明 private: int num;

char name[20]; char sex; };

?包含成员函数定义的源文件student.cpp。

//student.cpp 在此文件中进行函数的定义 #include

#include\ //不要漏写此行,否则编译通不过 void Student::display() //在类外定义display类函数 {cout<<\ cout<<\ cout<<\ }

?包含主函数的源文件main.cpp。

为了组成一个完整的源程序,应当有包括主函数的源文件: //main.cpp 主函数模块 #include

#include\ //将类声明头文件包含进来 int main() {Student stud;

Stud.display(); //定义对象

return 0; //执行stud对象的display函数 }

请完善该程序,在类中增加一个对数据成员赋初值的成员函数set_value。上机调试并运行。

(3)需要求3个长方柱的体积,请编一个基于对象的程序。数据成员包括length(长),width(宽),height(高)。要求用成员函数实现以下功能: ?由键盘分别输入3个长方柱的长,宽,高; ?计算长方柱的体积; ?输出3个长方柱的体积。 请编程序,上机调试并运行。

(4)定义日期类型Date。要求有以下面成员: ?可以设置日期; ?日期加一天操作;

?输入函数,输入格式为“XXXX年XX月XX日”。

三.实验结果与分析

⑴ #include using namespace std;

class Time {

public:

void set_time(void) {

cin>>hour; cin>>minute; cin>>sec; }

void show_time(void) {

cout<

int hour; int minute; int sec; };

Time t; int main() {

t.set_time(); t.show_time(); return 0; }

运行结果:

(2)//xt-4-1.cpp(main.cpp) #include using namespace std; #include \int main() { student stud; stud.set_value(); stud.display(); return 0; }

//xt2-4-2.cpp(即student.cpp) #include Using namespace std; #include \void student::display() { cout<<\ cout<<\ cout<<\

}

void student::set_value() { cin>>num; cin>>name; cin>>sex; }

//xt2-4.h class student {public:

void display(); void set_value(); private: int num; char name[20]; char sex; };

运行结果:

(3)#include using namespace std; class Box {

public:

void get_value(); void volume(); void display(); public:

float length; float width; float height; float vol;

};

void Box::get_value() {

cout<<\请输入长、宽、高 \ cin>>length; cin>>width; cin>>height; }

void Box::volume() {

vol=length*width*height; }

void Box::display() {

cout<

int main() {

Box box1,box2,box3; box1.get_value(); box1.volume();

cout<<\一号盒子的体积为:\ box1.display(); box2.get_value(); box2.volume();

cout<<\二号盒子的体积为:\ box2.display(); box3.get_value(); box3.volume();

cout<<\三号盒子的体积为:\ box3.display(); return 0; }

运行结果:

(4)#include using namespace std; extern int i = 0; class Date { public:

set(int y,int m, int d); add(); print(); private:

int year; int month; int day; };

Date::set(int y, int m, int d) { if(y < 0) { i = 1;

cout << \}

else year = y;

if(m < 1 || m > 12) {

i = 1; cout << \} else month = m; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12:

if(d< 1 || d > 31)

{ i = 1; cout << \ else day = d; break; case 4: case 6: case 9: case 11:

if(d < 1 || d > 30) { i = 1; cout << \ else day = d; break; case 2: if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ) { if(d < 1 || d > 29) { i = 1; cout << \ else day = d; } if(y % 4 != 0 || y % 100 == 0 && y % 400 != 0)

{ if(d < 1 || d > 28) { i = 1; cout << \ else day = d; } break; } }

Date:: …… 此处隐藏:1958字,全部文档内容请下载后查看。喜欢就下载吧 ……

实验四类和对象.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/656763.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)