类和对象例子.docx

上传人:b****1 文档编号:2420114 上传时间:2022-10-29 格式:DOCX 页数:23 大小:50.08KB
下载 相关 举报
类和对象例子.docx_第1页
第1页 / 共23页
类和对象例子.docx_第2页
第2页 / 共23页
类和对象例子.docx_第3页
第3页 / 共23页
类和对象例子.docx_第4页
第4页 / 共23页
类和对象例子.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

类和对象例子.docx

《类和对象例子.docx》由会员分享,可在线阅读,更多相关《类和对象例子.docx(23页珍藏版)》请在冰豆网上搜索。

类和对象例子.docx

类和对象例子

例10.4定义并测试长方形类CRect,长方形是由左上角坐标(left,top)和右下角坐标(right,bottom)组成。

#include

#include

usingnamespacestd;

classCRect//定义长方形类

{

private:

intleft,top,right,bottom;

public:

voidsetcoord(int,int,int,int);

voidgetcoord(int*L,int*T,int*R,int*B)

//注意:

形参为指针变量

{

*L=left;*T=top;

*R=right;*B=bottom;

}

voidprint(void)

{

cout<<"Area=";

cout<

abs(bottom-top)<

}

};

voidCRect:

:

setcoord(intL,intT,intR,intB)

{

left=L;top=T;right=R;bottom=B;

}

intmain(void)

{

CRectr,rr;

inta,b,c,d;

r.setcoord(100,300,50,200);

r.getcoord(&a,&b,&c,&d);

//用变量的指针做参数,带回多个结果

cout<<"left="<

cout<<"top="<

cout<<"right="<

cout<<"bottom="<

r.print();

rr=r;//对象可整体赋值

rr.print();

return0;

}

运行结果:

left=100top=300

right=50bottom=200

Area=5000

Area=5000

返回ppt讲稿

例10.5定义日期类,利用构造函数初始化数据成员。

程序放在头文件date.h中,如下:

#include

usingnamespacestd;

classDate

{

intYear,Month,Day;

public:

Date()//重载构造函数1

{

Year=2016;Month=5;Day=1;

}

Date(inty)//重载构造函数2

{

Year=y;Month=5;Day=1;

}

Date(inty,intm)//重载构造函数3

{

Year=y;Month=m;Day=1;

}

Date(inty,intm,intd)//重载构造函数4

{

Year=y;Month=m;Day=d;

}

voidShowDate()

{

cout<

}

};

主函数源文件为Li1005.cpp,内容如下:

#include"date.h"

intmain()

{

Dated1;//自动调用构造函数1

Dated2(2016);//自动调用构造函数2

Dated3(2016,10);//自动调用构造函数3

Dated4(2016,10,6);//自动调用构造函数4

d1.ShowDate();

d2.ShowDate();

d3.ShowDate();

d4.ShowDate();

return0;

}

运行结果是:

2016.5.1

2016.5.1

2016.10.1

2016.10.6

当然我们可以定义带缺省值的构造函数,将上述构造函数简化,下述程序的功能与上述程序相当:

#include

usingnamespacestd;

classDate

{

intYear,Month,Day;

public:

Date(inty=2016,intm=5,intd=1)

//带参数缺省值的构造函数

{

Year=y;Month=m;Day=d;

}

voidShowDate()

{

cout<

}

};

intmain()

{

Dated1,d2(2016),d3(2016,10),d4(2016,10,6);

d1.ShowDate();

d2.ShowDate();

d3.ShowDate();

d4.ShowDate();

return0;

}

运行结果与上例一样。

返回ppt讲稿

 

例10.6定义学生类,利用构造函数初始化数据成员,利用析构函数做清理工作。

#include

#include

usingnamespacestd;

classStudent

{

charNum[10];//学号,注意:

用数组实现

char*Name;//姓名,注意:

用指针实现

intScore;//成绩

public:

Student(char*nump,char*namep,intscore)

{

if(nump)//在构造函数中,

{//不需要动态申请Num成员的空间

strcpy(Num,nump);

}

else

strcpy(Num,"");

if(namep)//在构造函数中,

{//需动态申请Name成员的空间

Name=newchar[strlen(namep)+1];

strcpy(Name,namep);

}

elseName=NULL;

Score=score;

cout<<"ConstructorCalled!

\n";

}

~Student()//在析构函数中,

{//需释放Name成员的空间

if(Name)delete[]Name;

cout<<"DesturctorCalled!

\n";

}

voidShow()

{

cout<

cout<

cout<

}

};

intmain()

{

Studenta("040120518","George",80);

a.Show();

return0;

}

此程序运行结果是:

ConstructorCalled!

//调用构造函数时的输出

040120518George80

DesturctorCalled!

//调用析构函数时的输出

返回ppt讲稿

 

例10.7调用构造函数和析构函数的时机

#include

usingnamespacestd;

classDate

{

intYear,Month,Day;

public:

Date(inty=2000,intm=1,intd=1)//A,所有参数都有默认值

{

Year=y;Month=m;Day=d;

cout<<"Constructor:

";

ShowDate();

}

voidShowDate()

{

cout<

}

~Date()

{

cout<<"Destructor:

";

ShowDate();

}

};

Dated4(2016,4,4);//全局对象(静态的)

voidfun()

{

cout<<"进入fun()函数!

\n";

staticDated2(2016,2,2);//局部静态对象

Dated3(2016,3,3);//局部动态对象

cout<<"退出fun()函数!

\n";

}

intmain()

{

cout<<"进入main()函数!

\n";

Dated1(2016,1,1);//局部动态对象

fun();

fun();

cout<<"退出main()函数!

\n";

return0;

}

此程序运行结果是:

Constructor:

2016.4.4//调用构造函数,产生d4对象

进入main()函数!

Constructor:

2016.1.1//调用构造函数,产生d1对象

进入fun()函数!

//第1次进入fun()函数,产生下述d2,d3对象

Constructor:

2016.2.2

Constructor:

2016.3.3

退出fun()函数!

//退出fun()函数,撤消d3对象,不撤消d2对象

Destructor:

2016.3.3

进入fun()函数!

//第2次进入fun()函数,再次产生d3对象

Constructor:

2016.3.3

退出fun()函数!

Destructor:

2016.3.3//退出fun()函数,撤消d3对象

退出main()函数!

//退出main()函数,撤消d1,d2,d4对象

Destructor:

2016.1.1

Destructor:

2016.2.2

Destructor:

2016.4.4

返回ppt讲稿

例10.9定义一个“平面坐标点”类,测试拷贝构造函数的调用。

//头文件"point.h"

classPoint

{

intx,y;

public:

Point(inta=0,intb=0)//缺省构造函数

{

x=a;y=b;

}

Point(Point&p);//拷贝构造函数原型说明

~Point()//析构函数

{

cout<

}

voidShow()

{

cout<<"Point:

"<

}

intGetx()

{returnx;}

intGety()

{returny;}

};

Point:

:

Point(Point&p)//定义拷贝构造函数

{

x=p.x;y=p.y;

cout<

}

//文件Li1009.cpp

#include

u

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 面试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1