




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
WIN32控制台的若干程序:1. 源文件中:这个例子着重注意枚举类型的联合类型的应用。枚举和联合都是类的成员变量。#include using namespace std;const float PI=3.1416;#include class datatypeprivate:enumcharacter,interger,float_pointdtype;unionchar c;int i;float f;public:datatype(char cc)c=cc;dtype=character;datatype(int ii)i=ii;dtype=interger;datatype(float ff)f=ff;dtype=float_point;void printf();void datatype:printf()switch (dtype)case character:coutcharacter:cendl;break;case interger:coutinterger:iendl;break;case float_point:coutfloat_point:fendl;break;default:break;void main() datatype A(n),B(5),C(4.55f);A.printf();B.printf();C.printf();2. 这个例子注意友元函数应用和引用应用。为了测试引用成员变量调用,我自己做了一个测试程序,如下:#include using namespace std;void swap(int &x,int &y)int temp;temp=x;x=y;y=temp;void main()int a=10,b=20;couta=ab=bendl;swap(a,b);couta=ab=bendl;由此例可以看出,引用是变量的别名,两个要变一起变。若此例没用引用,即代码为:#include using namespace std;void swap(int x,int y)int temp;temp=x;x=y;y=temp;void main()int a=10,b=20;couta=ab=bendl;swap(a,b);couta=ab=bendl;则结果为:可以看到,交换的功能没有实现。为了测试会发生这样的结果,我们的代码如下:#include using namespace std;void swap(int x,int y)coutx=xy=yendl;int temp;temp=x;x=y;y=temp;coutx=xy=yendl;void main()int a=10,b=20;couta=ab=bendl;swap(a,b);couta=ab=bendl;此时结果如下:由上图可以看到,X 和Y值确实做了交换,但是交换后的参数没有传回来。所以用引用的话,就能保证交换后的参数传回来。#include using namespace std;class boat;class carprivate: int weight;public:car(int j) weight=j;friend int totalweight(car &car1,boat &boat1);class boatprivate: int weight;public: boat(int j)weight=j;friend int totalweight(car &car1,boat &boat1); ;int totalweight(car &car1,boat &boat1)return car1.weight+boat1.weight;void main() car c1(4); boat b1(5); couttotalweight(c1,b1)endl;3. 排序功能:int main()int a10=28,6,99,7,0,34,67,33,25,84,i=0,j=0,temp=0; for (i=0;i10;i+) for (j=i;j10;j+) if (aiaj) temp=ai;ai=aj;aj=temp; for (i=0;i10;i+) coutai的值:ai ; 4,类的构造函数和析构函数调用:#include using namespace std;class Pointpublic:Point();Point(int x,int y);Point();void move(int newx,int newy);int getx() const return x;int gety() const return y;static void showcount();private:int x,y;Point:Point()x=y=0;coutdefault construct calledendl;Point:Point(int x,int y):x(x),y(y) coutconstruct calledendl;Point:Point()coutdeconstruct calledendl;void Point:move(int newx,int newy)coutmoving the point to(newx,newy)endl;x=newx;y=newy;void main()coutentering main.endl;Point a2;for (int i=0;i2;i+) ai.move(i+10,i+20);coutexiting main.endl;结果如下:5,指针操作:#include using namespace std;void main()int *p; 应该为:int *p=new int;错误原因:指针没有初始化,也就是没有指向某个确定的内存单元,它指向内存中的一个随机地址,给这个随机地址赋值时是非常危险的。(也就在在使用指针的时候,必须要初始化。)*p=9;cout*p;代码是错误的。6.关于字符串操作的例子:#include #include using namespace std;class Employeepublic:Employee(char *n,char *s,char *c,char *y)strcpy(name,n);strcpy(shreet,s);strcpy(city,c);strcpy(youbian,y);void change_name(char *newname)strcpy(name,newname);void display()coutname is:nameendl;coutshreet is:shreetendl;coutcity is:cityendl;coutyoubian is:youbianendl;private:char name20;char shreet50;char city20;char youbian10; void main() Employee employe1(李+,金色月亮6幢4单元901,北京市,089231); employe1.change_name(刘+); employe1.display(); 7.类的继承问题:#include using namespace std;class Shapepublic:Shape()Shape()virtual float GetArea()return -1;class Circle: public Shape public:Circle(float radius):itsRadius(radius)Circle()float GetArea()return 3.14*itsRadius*itsRadius;private:float itsRadius;class Rectangle: public Shape public:Rectangle(float len,float width):itsLength(len),itsWidth(width)Rectangle()virtual float GetArea()return itsWidth*itsLength; private:float itsWidth;float itsLength;class Square: public Rectangle public:Square(float len);Square();Square:Square(float len):Rectangle(len,len)int main()Shape *sp;sp=new Circle(5);coutThe area of the Circle is GetArea()endl;delete sp;sp=new Rectangle(4,6);coutThe area of the Rectangle is GetArea()endl;delete sp;sp=new Square(5);coutThe area of the Square is GetArea()endl;delete sp;return 0;7.#include using namespace std;class objectprivate:int Weight;public:object()cout 构造object对象endl;Weight=0;int GetWidth()return Weight;void SetWeight(int n)Weight=n;object()cout析构object对象endl;class box:public objectprivate:int Height,Width;public:box()cout构造box对象 endl;Height=Width=0;int GetHeight()return Height;void SetHeight(int n)Height=n;int GetWidth()return Width;void SetWidth(int n)Width=n;box()cout析构box对象endl;int main()box a;return 0;8.#include using namespace std;class BaseClasspublic:void fn1();void fn2();void BaseClass:fn1()cout调用基类的函数fn1()endl;void BaseClass:fn2()cout调用基类的函数fn2()endl;class DerivedClass:public BaseClasspublic:void fn1();void fn2();void DerivedClass:fn1()cout调用派生类的函数fn1()endl;void DerivedClass:fn2()cout调用派生类的函数fn2()fn1();pBaseClass-fn2();pDerivedClass-fn1();pDerivedClass-fn2();return 0;9.#include #include using namespace std;class Documentpublic:Document();Document(char *name);char *Name;void PrintNameOf();Document:Document(char *name)Name=new charstrlen(name)+1;strcpy(Nam
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 测绘保密考试题库及答案
- 北京市门头沟区2023-2024学年八年级上学期期中考试道德与法制考题及答案
- 北京市朝阳区2023-2024学年七年级上学期期末质量监测数学试卷及答案
- 心理反转测试题目及答案
- 校务办面试题目及答案
- 观后感复兴之路观后感二450字(10篇)
- 业务代理授权合同
- 诗歌与散文鉴赏能力培养方案
- 人教版七年级下册二单元作文母亲河抒怀11篇
- 时尚的鸭子哦课件
- 绿色能源项目投资可行性分析报告范文
- 运输公司值班管理制度
- 血透室护理不良事件
- 大客户服务定制化策略方案
- 编译原理教案
- 电梯使用单位日管控、周排查、月调度电梯安全检查记录表
- 资产评估机构质量控制制度
- 股份制公司章程样本
- TCTBA 005-2024 TCECA-G 0326-2024 合同能源管理招标规范 轨道交通
- 中国职业教育发展前景
- 中小企业数字化转型路径与实施指南
评论
0/150
提交评论