C++实验程序讲课稿

上传人:go****e 文档编号:137368766 上传时间:2020-07-07 格式:DOC 页数:5 大小:28KB
返回 下载 相关 举报
C++实验程序讲课稿_第1页
第1页 / 共5页
C++实验程序讲课稿_第2页
第2页 / 共5页
C++实验程序讲课稿_第3页
第3页 / 共5页
C++实验程序讲课稿_第4页
第4页 / 共5页
C++实验程序讲课稿_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《C++实验程序讲课稿》由会员分享,可在线阅读,更多相关《C++实验程序讲课稿(5页珍藏版)》请在金锄头文库上搜索。

1、C+实验程序精品文档实验2/lab4_1.cpp#include using namespace std;enum CPU_Rank P1=1,P2,P3,P4,P5,P6,P7;class CPUprivate: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() const ret

2、urn rank; int GetFrequency() const return frequency; float GetVoltage() const 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; ;int main()CPU a(P6,300,2.8);a

3、.Run();a.Stop();实验4#include #include using namespace std;class Employeeprivate:char name30;char street30;char city18;char zip7;public:Employee(char *n, char *str, char *ct, char *z);void change_name(char *n);void display();Employee:Employee (char *n,char *str,char *ct, char *z)strcpy(name, n);strcpy

4、(street, str);strcpy(city, ct);strcpy(zip, z);void Employee:change_name (char *n)strcpy(name, n);void Employee:display ()cout name street ;cout city zip endl;/ lab6_6.cpp#include employee.hint main()Employee emp5 = Employee(张三,平安大街3号, 北京, 100000), Employee(李四,王府井大街20号, 北京, 100000), Employee(赵刚,中山路11

5、2号, 重庆, 400000), Employee(陈芳,南京路234号, 上海, 200000), Employee(周欣,人民东路476号, 重庆, 400000);for(int i=0;i5;i+)empi.display();实验5#include using namespace std;class vehicleprivate:int MaxSpeed;int Weight;public: vehicle()MaxSpeed=0; Weight=0;vehicle();void Run() cout Now it is running! endl; void Stop() cout

6、 Now it has stopped! endl; ;class bicycle : virtual public vehicle private:int Height;public:bicycle();bicycle(); class motorcar : virtual public vehicle private:int SeatNum;public:motorcar();motorcar();class motorcycle : public bicycle , public motorcar public:motorcycle ();motorcycle ();int main()

7、motorcycle a;a.Run();a.Stop();实验6#include using namespace std;class vehicleprivate:int MaxSpeed;int Weight;public: vehicle()MaxSpeed=0; Weight=0;vehicle();virtual void Run() cout A vehicle is running! endl; virtual void Stop() cout A vehicle has stopped! endl; ;class bicycle : virtual public vehicle

8、 private:int Height;public:bicycle();bicycle();void Run() cout A bicycle is running! endl; void Stop() cout A bicycle has stopped! endl; ; class motorcar : virtual public vehicle private:int SeatNum;public:motorcar();motorcar();void Run() cout A motorcar is running! endl; void Stop() cout A motorcar

9、 has stopped! endl; ;class motorcycle : public bicycle , public motorcar public:motorcycle ();motorcycle ();void Run() cout A motorcycle is running! endl; void Stop() cout A motorcycle has stopped! Run();ptr = &b;ptr-Run();ptr = &c;ptr-Run();ptr = &d;ptr-Run();实验二 类与对象一、实验目的1、 掌握类和对象的声明和使用2、 复习具有不同访

10、问属性的成员的访问方式3、 观察构造函数和析构函数的执行过程4、 使用debug调试功能观察程序流程,跟踪观察类的构造函数、析构函数、成员函数的执行顺序二、实验任务声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,有两个公有成员函数run, stop。其中,rank为枚举类型CPU_Rank , 声明enumCPU_Rank P1=1,P2,P3,P4,P5,P6,P7,frequency单位是MHZ的整型数,voltage为浮点型的电压值。观察构造函数和析构函数的调用顺序。实验三 C+程序的结构一、实验目的1、 观察程序运行中变量的作用域、生存

11、期和可见性2、 学习类的静态成员的使用二、实验任务1、 运行下面的程序,观察变量x,y的值#includeusing namespace std;void fn1();int x=1,y=2;int main() cout”Begin”endl; cout”x=”xendl;cout”y=”yendl;cout”evaluate x and y in main()”endl;int x=10,y=20;cout”x=”xendl;cout”y=”yendl;cout”step int fn1()”endl;fn1();cout”back in main”endl;cout”x=”xendl;c

12、out”y=”yendl;return 0;void fn1()int y=200;cout”x=”xendl;cout”y=”yendl;2、 类的静态成员的使用编译、运行例5.5,观察程序的输出结果。实验四 数组与指针一、实验目的1、学习使用数组数据对象2、掌握指针的使用方法3、通过debug观察指针的内容及其所指对象的内容二、实验任务1、声明一个employee类,其中包括姓名、街道地址、城市和邮编等属性,以及change_name()和display()等函数。display()显示姓名、街道地址、城市和邮编等属性,change_name()改变对象的姓名属性,实现并测试这个类。2、声明包含5个元素的对象数组,每个元素都是employee类型的对象。实验五 继承与派生一、实验目的1、学习声明和使用类的继承关系,声明派生类2、熟悉不同继承方式下对基类成员的访问控制3、学习利用虚基类解决二义性问题二、实验任务声明一个车(vehicle)类,具有maxspeed、w

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 其它小学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号