数据的共享和保护以及多态性Word格式文档下载.docx
- 文档编号:19528488
- 上传时间:2023-01-07
- 格式:DOCX
- 页数:14
- 大小:74.07KB
数据的共享和保护以及多态性Word格式文档下载.docx
《数据的共享和保护以及多态性Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《数据的共享和保护以及多态性Word格式文档下载.docx(14页珍藏版)》请在冰豆网上搜索。
#include<
iostream>
string>
usingnamespacestd;
classDog
{
public:
Dog()//默认构造函数
{
weight=0;
age=0;
name="
"
;
}
voidload(stringna,intag,floatwgh)//录入函数
weight=wgh;
age=ag;
name=na;
voidshow()
cout<
<
"
***************\n狗名字:
<
name<
\n狗年龄:
age<
\n狗的体重:
weight<
endl;
staticintget_amount()//返回狗的数目,静态成员amount
returnamount;
staticvoidadd_anmout()//调用静态成员amount,狗的数目+1
amount++;
~Dog(){}//析构函数
private:
floatweight;
intage;
stringname;
staticintamount;
};
intDog:
:
amount=0;
Dogd[2];
//定义3只狗
intadd()
cout<
********************\n请输入狗的名字:
cin>
>
name;
请输入狗的年龄:
age;
请输入狗的体重重:
weight;
d[Dog:
get_amount()].load(name,age,weight);
Dog:
add_anmout();
return0;
}
intmain()
while(Dog:
get_amount()<
2)
add();
d[Dog:
get_amount()-1].show();
//此时狗的数目已加,要显示上一只信息需-1
*********\n狗的总数:
(二)XYZ的私有成员访问
头文件中:
classX
X(inta=0){x=a;
}//内联函数应该写在头文件里
~X(){}
friendclassY;
//友元类,Y可以访问X的私有数据成员
friendclassZ;
//友元函数,Z中只有Zget_x()函数可以访问X私有成员
intXget_x(){returnx;
intx;
classY
Y(){}
~Y(){}
intYget_x(constX&
yx){returnyx.x;
}//应该养成常引用的习惯
classZ
Z(){}
~Z(){}
intZget_x(X&
zx){returnzx.x;
}//友元函数成员
//由于都是内联函数,故未使用源文件实现函数功能和部分分配空间的变量的初始化;
主函数源文件:
#include"
XYZ.h"
//自定义头文件最好用“”书写方式
intmain()
XA
(1);
YB;
ZC;
X类对象A中的私有成员\nx="
A.Xget_x()<
\nY类对象B访问A的私有成员\nx="
B.Yget_x(A)<
\nZ类对象C访问A的私有成员\nx="
C.Zget_x(A)<
(三)虚基类和虚函数的使用
classvehicle
{
vehicle(floats):
speed(s){}
virtualvoidRun()
{
cout<
车启动"
}
virtualvoidStop()
车已停"
}
~vehicle(){}
protected:
floatspeed;
classbicycle:
virtualpublicvehicle
bicycle(floats,floatw):
vehicle(s),weight(w){}
voidRun()
{
***********\n自行车速度:
speed<
\n自行车重:
weight<
\n自行车启动"
endl;
}
voidStop()
自行车已停"
~bicycle(){}
classmotorcar:
motorcar(floats,intn):
vehicle(s),seatamount(n){}
***********\n汽车速度:
\n汽车核载人数:
seatamount<
\n汽车启动"
汽车已停"
~motorcar(){}
intseatamount;
classmotorcycle:
publicbicycle,publicmotorcar
motorcycle(floats,floatw,intn):
vehicle(s),bicycle(s,w),motorcar(s,n){}
***********\n摩托车速度:
\n摩托车重:
\n摩托车核载人数:
\n摩托车启动"
摩托车已停"
~motorcycle(){}
intmain()
vehiclea(15.5);
bicycleb(15.5,10.5);
motorcarc(60,4);
motorcycled(30.5,200,2);
vehicle*p[4]={&
a,&
b,&
c,&
d};
for(inti=0;
i<
4;
i++)
p[i]->
Run();
Stop();
(四)People类,运算符重载
#include<
enumSEX{man,woman};
//性别属性为枚举类型
classDate
Date(unsignedintdy,unsignedintmon,unsignedintyr)//内联构造函数
day=dy;
month=mon;
year=yr;
Date()//默认构造函数
day=0;
month=0;
year=0;
Date(Date&
d)
day=d.day;
month=d.month;
year=d.year;
voidoperator=(Dated){
intshow(void)//内联成员函数
year<
年"
month<
月"
day<
日"
return0;
~Date(){}//析构函数
unsignedintday,month,year;
classPeople
People(stringna,stringnum,SEXsx,Datebirth,charid_num[19]);
//构造函数
People(){}
People(People&
p);
intshow(void);
voidoperator=(People&
//复制构造函数的功能一样,实现类的复制,且设为常成员函数,避免改变参数
intoperator==(People&
voidshow_name(){cout<
name;
~People(){}
stringname,number;
//类的数据成员
char*id;
Datebirthday;
//内嵌对象生日
SEXsex;
//性别
People:
People(stringna,stringnum,SEXsx,Datebirth,charid_num[19]):
birthday(birth)//组合类构造函数
name=na;
number=num;
sex=sx;
id=id_num;
birthday=birth;
People(People&
p):
birthday(p.birthday)//组合类复制构造函数
name=p.name;
number=p.number;
sex=p.sex;
id=p.id;
voidPeople:
operator=(People&
p)//运算符重载的实现,同复制构造函数
birthday=p.birthday;
intPeople:
operator==(People&
p)
if(strcmp(id,p.id)>
0)return0;
if(strcmp(id,p.id)==0)return1;
if(strcmp(id,p.id)<
0)return2;
show(void)
stringtemp;
if(sex==man)temp="
男"
elsetemp="
女"
************************\n姓名:
\n编号:
number<
\n性别:
temp<
\n出生日期:
birthday.show();
身份证号:
id<
\n************************"
intmain(void)
{Datebirthday1(1,1,1995);
Peoplep1("
A"
"
001"
man,birthday1,"
4201061995907361x"
);
p1.show();
Peoplep2("
B"
002"
42010619959073610"
p2.show();
Peoplep3;
p3=p2;
p3.show();
switch(p3==p1)
case0:
p3.show_name();
的编号较大"
break;
case1:
cout<
两者编号一样大"
case2:
p1.show_name();
五、实验运行结果
(二)XYZ的私有成员访问
(三)虚基类和虚函数的实现
(四)People类和运算符的重载
六、实验心得与体会
本次试验所获颇多,很多收获都写在了程序的注释里,比如内联函数最好放在头文件里,方便声明,而在声明时内存会分配空间的变量或函数不能在,头文件里声明,以免在多个源文件里调用头文件时,一个变量或函数分配多个空间,造成连接错误,虚基类和虚函数在引用时的对应关系,隐藏规则等等等等,这里不再一一列举。
在完成第四题的过程中,由于对运算符重载的错误理解,导致return空的类,而卡在这个BUG上好久。
用与复制构造函数一样的引用方法解决之后,身心为之畅然。
总之,这次试验让我理解到了C++抽象类对象的强大之处。
C++是一门更加符合人类思维逻辑的语言,在以后的学习中,我努力学习,掌握好这门强大的语言。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据 共享 保护 以及 多态性