C++程序设计(第2版) 实验三、C++类和对象.docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《C++程序设计(第2版) 实验三、C++类和对象.docx》由会员分享,可在线阅读,更多相关《C++程序设计(第2版) 实验三、C++类和对象.docx(4页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验三、类和对象(10学时)一、实验目的1 .掌握声明类的方法,类和类的成员的概念以及定义对象的方法。2 .初步掌握用类和对象编制基于对象的程序。3 .学习检查和调试基于对象的程序。4 .掌握类的构造函数和析构函数的概念和使用方法。5 .掌握对象数组、对象的指针及其使用方法。6 .掌握共用数据的保护方法二、实验内容1 .检查下面的程序,找出其中的错误,并改正之。然后上机调试,使之能正常运行。 (1)#include using namespace std; class Clock void set_clock(void);void show_clock(void);int hour;int m
2、inute;int second;);Clock clock;int main()(set_clock();show_clock ();int set_clock(void)(cint.hour;cint.month;cint.second;)coutt.hour,:,t.minute,:nt.secondendl;int show_clock(void) )(2)#include using namespace std; class Apublic:void A(int i=0) m=i;void show()coutm;void A()private:int m;int main() A
3、a(5);a.m+=10;a.show();return 0;)(3)class X private:int a=0;int &b;const int c;void setA(int i)a=i;X(int i) a=i;public:int X() a=b=0;X(int i, int j, int k) a=i; b=j; c=k; setC(int k) const c=c+k;);int mainQ(Xxl;X x2(2);Xx3(l,2,3);xl.setA(3);return 0 ;)2.请先阅读下面的程序,写出程序运行的结果,然后再上机运行程序,验证自己分析的结 果是否正确。#i
4、nclude using namespace std;class test public:test();int getint()return num; float getfloat()return fl; test();private:int num;float fl;);test:test() cout nInitalizing default1 endl; num=0;fl=0.0;)test二test() cout Destructor is active* endl; int main() (test array2;cout arrayl.getint() ” “ arrayl.get
5、float() endl; return 0;)#include using namespace std;#includeclass X public:X(int xl,char *x2,float x3):a(xl),c(x3) b=new charsizeof(x2)+1;strcpy(b,x2);)X():a(0),b(X:X()c(10) X(int xl,char *x2=X:X(x3=10):a(xl),b(x2),c(x3) X(const X &other) a=other.a;b=nX:X(const X &other)n; c=other.c;void print()cou
6、tna=na,tn,b=,b,tn,c=nccndl;private :int a;char *b;float c;);int main()X *A=new X(4;X:X(int,char,float),32);XB,C(10),D(B);A-print();B.print();C.print();D.print();return 0;).某单位的职工工资包括基本工资Wage,岗位津贴Subsidy,房租Rent,水费WaterFee, 电费ElecFee。设计实习工资管理的类Salary,该类的形式如下:class Salarypublic:Salary。初始化工资数据的各分项数据为0Sa
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+程序设计第2版 实验三、C+类和对象 C+ 程序设计 实验 对象
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内