构造函数.docx
- 文档编号:11391576
- 上传时间:2023-02-28
- 格式:DOCX
- 页数:12
- 大小:182.61KB
构造函数.docx
《构造函数.docx》由会员分享,可在线阅读,更多相关《构造函数.docx(12页珍藏版)》请在冰豆网上搜索。
构造函数
南昌航空大学实验报告
二00七年月日
课程名称:
面向对象程序设计实验名称:
构造函数与析构函数
班级:
090453学生姓名:
余圣波学号:
09045329
指导教师评定:
签名:
1、实验目的
·理解类与对象的区别。
·学习掌握定义构造函数与析构函数的方法。
·学习把握默认构造函数的意义。
·了解类成员初始化,掌握构造与析构类成员的方式。
2、实验内容
(1)
创建一个Employee类,该类中用字符数组存放Employee的信息,如姓名、地址、市、省、及邮政编码;每个成员函数的定义放在类定义之外;成员函数包括改变姓名数据成员等;构造函数完成成员数据的初始化;用Display()函数将完整的对象数据打印出来;其中数据成员是保护的,成员函数是公共的。
#include
#include
#include
usingnamespacestd;
classEmployee
{
public:
Employee(charn[20],chara[50],chars[20],charsh[20],charp[7]);
voidChangeName(charch_name[20]);
voidDisplay();
protected:
charname[20];
charaddress[50];
charshi[20];
charsheng[20];
charpost[7];
};
Employee:
:
Employee(charn[20],chara[50],chars[20],charsh[20],charp[7])
{
if(strlen(n)<=19&&strlen(a)<=49&&strlen(s)<=19&&strlen(sh)<=19&&strlen(p)==6)
strcpy(name,n),strcpy(address,a),strcpy(shi,s),strcpy(sheng,sh),strcpy(post,p);
}
voidEmployee:
:
ChangeName(charch_name[20])
{
if(strlen(ch_name)<=19)
strcpy(name,ch_name);
}
voidEmployee:
:
Display()
{
char*p;
cout<<"姓名:
"< for(p=name;*p! ='\0';p++) cout<<*p; cout< cout<<"地址: "< for(p=address;*p! ='\0';p++) cout<<*p; cout< cout<<"市: "< for(p=shi;*p! ='\0';p++) cout<<*p; cout< cout<<"省份: "< for(p=sheng;*p! ='\0';p++) cout<<*p; cout< cout<<"邮政编码: "< for(p=post;*p! ='\0';p++) cout<<*p; cout< } voidmain() { charn[20],a[50],s[20],sh[20],p[7]; cout<<"请输入姓名: "< gets(n); cout<<"请输入地址: "< gets(a); cout<<"请输入市区: "< gets(s); cout<<"请输入省份: "< gets(sh); cout<<"请输入邮政编码: "< gets(p); Employeeyu(n,a,s,sh,p); yu.Display(); cout<<"请输入您需要改变的名字: "< charnameChange[20]; gets(nameChange); yu.ChangeName(nameChange); yu.Display(); } (2)设计一个表示矩形的类Rect,其矩形成员长float*Length,宽float*Width为指针变量,设计相应成员函数,实现下列功能: ①构造函数设置长和宽(默认为1)。 ②复制构造函数用老对象生成新对象。 ③set()函数设置长和宽(默认为0)。 ④计算并返回长方形的周长。 ⑤计算并返回长方形的面积。 ⑥析构函数释放动态分配的长和宽。 编制主程序应用指针建立对象测试类。 #include classRect{ float*Length,*Width; public: Rect(floata=1,floatb=1); Rect(Rect&); voidset(floata=0,floatb=0); floatperi(); floatarea(); ~Rect(){deleteLength;deleteWidth;} }; Rect: : Rect(floata,floatb) { if(a>0&&b>0) { Length=newfloat(a),Width=newfloat(b); } else cout<<"您输入的长宽不符规则: "< } Rect: : Rect(Rect&p) { Length=newfloat(*(p.Length)),Width=newfloat(*(p.Width)); } voidRect: : set(floata,floatb) { if(a>0&&b>0) { Length=newfloat(a),Width=newfloat(b); } else cout<<"您输入的长宽不符规则: "< } floatRect: : peri() { return(2*((*Length)+(*Width))); } floatRect: : area() { return((*Length)*(*Width)); } voidmain() { cout<<"利用构造函数构造对象"< floatlength,width,peri,area; cout<<"请输入长: "< cin>>length; cout<<"请输入宽: "< cin>>width; Rectyu(length,width); cout<<"该矩形的周长为: "< peri=yu.peri(); cout< cout<<"该矩形的面积为: "< area=yu.area(); cout< cout<<"利用set函数构造对象"< Rectysb; cout<<"请输入长: "< cin>>length; cout<<"请输入宽: "< cin>>width; ysb.set(length,width); cout<<"该矩形的周长为: "< peri=ysb.peri(); cout< cout<<"该矩形的面积为: "< area=ysb.area(); cout< cout<<"利用拷贝构造函数构造对象"< Rectt(ysb); cout<<"该矩形的周长为: "< peri=t.peri(); cout< cout<<"该矩形的面积为: "< area=t.area(); cout< }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 构造 函数
![提示](https://static.bdocx.com/images/bang_tan.gif)