收藏 分销(赏)

类和对象的定义与使用1.doc

上传人:精**** 文档编号:2669982 上传时间:2024-06-04 格式:DOC 页数:9 大小:777.04KB 下载积分:6 金币
下载 相关 举报
类和对象的定义与使用1.doc_第1页
第1页 / 共9页
类和对象的定义与使用1.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
淮北师范大学实验报告 姓名 江 学号 20141204027 实验日期 预习 (满分20分) 报告 (满分30分) 成绩 院别 计算机 年级 2014 实验场地 A405 实验课程名称 面向对象程序设计实验 实验项目名称 类和对象的定义与使用 对于每一个实验项目,实验报告(含预习)一般应包含以下内容:第一部分—-预习后的书面汇报。其主要内容应包含:*1、实验目的;*2、实验内容.第二部分——实验结果的书面汇报.其主要内容应包含: *3、实验源代码;*4、实验结果及分析(含实验测试输入数据,试验运行结果截图,用简洁的语言总结实验,汇报是否达到实验目的);*5、实验体会、问题讨论(谈体会或感想、提出建议或意见、讨论与实验有关的且自己感兴趣的问题);6、回答课后思考题(按指导教师的要求)。 1、实验目的 (1)掌握类与对象的定义; (2)掌握构造函数和析构函数的实现方法; (3)掌握静态成员函数和友元函数的使用方法; (4)掌握类对象作为成员的使用方法。 2、实验内容 (1)声明一个CPU类,在该类中包括数据成员rank(等级)、frequency(频率)、voltage(电压)。其中,rank为字符型,可以取P1、P2等,frequency为整型(单位MHz),voltage为浮点型;还包括成员函数GetRank( )、GetFrequency( )、GetVoltage( )用来获取等级、频率、电压的值.SetRank( )、SetFrequency( )、SetVoltage( )用来设置等级、频率、电压的值。Run( )、Stop( )中Run( )要求输出“CPU开始运行!”,Stop( )要求输出“CPU停止运行!”。构造函数需要输出“构造一个CPU”,析构函数需要输出“析构一个CPU"。观察构造函数和析构函数的调用顺序。编写主函数使用这个类,实现对CPU数据的赋值和输出。 (2)声明一个Student类,在该类中包括一个数据成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account( )用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum( )用于返回学生的成绩之和,另一个静态成员函数average( )用于求全班成绩的平均值。在main( )函数中,输入某班同学的成绩,并调用上述函数求出全班学生的成绩之和和平均值。 (3)声明一个Car类,在该类中包括数据成员color(颜色)、speed(速度)和成员函数Input( )用于输入color和speed,利用友元函数输出Car类对象数据。 (4)声明日期Date类,包含数据成员year(年)、month(月)、day(日);人员Person类,包含数据成员id(编号)、name(姓名)、sex(性别)、birthday(出生日期),其中birthday定义为Date类内嵌子对象。用成员函数实现对人员信息的录入和显示 3、实验源代码 (1)#include 〈iostream.h> enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7}; class CPU {private: CPU_Rank rank; int frequency; float voltage; public: CPU (CPU_Rank r, int f, float v) { rank = r; frequency = f; voltage = v; cout << "构造了一个CPU!” 〈< endl; } ~CPU () { cout 〈< ”析构了一个CPU!" <〈 endl; } CPU_Rank GetRank() { return rank; } int GetFrequency() { return frequency; } float GetVoltage() { return voltage; } void SetRank(CPU_Rank r) { rank = r; } void SetFrequency(int f) { frequency = f; } void SetVoltage(float v) { voltage = v; } void Run() {cout << "CPU开始运行!" 〈< endl; } void Stop() {cout 〈〈 ”CPU停止运行!” 〈〈 endl; } }; void main() { CPU a(P6,300,2。8); a.Run(); a。Stop(); } /** * */ /*定义一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,cpu为CPU类的一个对象,ram为RAM类的一个对象,cdrom为CDROM类的一个对象,定义并实现这个类。 (2) #include〈iostream> using namespace std; class Student{ private: float score; static int count; static float total_score; public: void account(float score1) {score=score1; ++count; total_score=total_score+score; } static float sum() {return total_score; } static float average() { return total_score/count; } }; int Student:: count=0; float Student:: total_score=0。0; int main() { Student s1,s2; s1.account(99); cout<<Student::sum()<<endl; cout〈〈Student::average()<<endl; s2。account(70); cout<〈Student::sum()〈〈endl; cout〈<Student::average()〈〈endl; return 0; } (3) #include <iostream> using namespace std; class Car { friend void display(Car); private: int speed; char color[10]; public: void input( ) { cout〈<"Enter the color : "; cin〉〉color; cout〈<"Enter the speed : "; cin>>speed; } }; void display(Car c) { cout<<"color: "<<c。color〈〈endl; cout〈<”speed: ”〈〈c.speed<<endl; } int main( ) { Car mine; mine.input( ); display(mine); } (4) #include 〈iostream〉 #include 〈string> using namespace std; class Date { private: int year; int month; int day; public: Date() {} Date(int y,int m,int d) { year=y; month=m; day=d; } void set() { cin>>year〉>month〉>day; } void display() { cout<<year〈〈'/'<〈month〈〈'/'〈〈day; } }; class Person { private: int num; char sex; Date birthday; char ID[19]; public: Person() {} Person(int n,int y,int m,int d,char id[19],char s=’m'):birthday(y,m,d) { num=n; sex=s; strcpy(ID,id); } Person(Person& p) { num=p.num; sex=p。sex; birthday=p.birthday; strcpy(ID,p。ID); } void input() { cout<〈"录入数据:"〈〈endl; cout〈〈”编号:"; cin〉〉num; cout<<"性别(m/f):"; cin>〉sex;cout<<”生日:"; birthday。set(); cout<〈"身份证号:”; cin>〉ID; ID[18]='\0'; cout〈〈endl; } void output() { cout<〈”编号:"<〈num〈〈endl; cout〈<"性别:"〈〈sex<〈endl; cout〈<"生日:"; birthday。display(); cout〈<endl; cout〈〈"身份证号:”<<ID〈〈endl; } ~Person() { cout<<num<<"号人员信息已删除.”〈<endl; } }; int main() { Person p1; p1。input(); p1。output(); return 0; } 4、实验结果及分析 1 2 3 4. 5、实验体会、问题讨论 6、回答课后思考题 教师签字: 批改日期:
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服