最新天津理工大学C++实验三.docx
《最新天津理工大学C++实验三.docx》由会员分享,可在线阅读,更多相关《最新天津理工大学C++实验三.docx(22页珍藏版)》请在冰豆网上搜索。
最新天津理工大学C++实验三
天津理工大学
计算机科学与技术学院
实验报告
至学年第学期
课程名称
C++程序设计
学号
学生姓名
年级
专业
教学班号
实验地点
实验时间
年月日第节至第节
主讲教师
辅导教师
实验(三)
实验名称
派生与继承
软件环境
C++visual
硬件环境
无
实验目的
(1)理解继承的含义,掌握派生类的定义方法和实现;
(2)理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;
(3)理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;
实验内容(应包括实验题目、实验要求、实验任务等)
1.#include
#definePI3.14159
classPoint//定义“点”类
{
intx,y;
public:
Point(inta=0,intb=0)
{x=a;y=b;}
voidShowPoint()
{cout<<"Point:
("<intGetx()
{returnx;}
intGety()
{returny;}
voidSetxy(inta,intb)
{x=a;y=b;}
};
classCircle:
publicPoint//定义“圆”类,公有继承
{
intr;//“圆”的半径
public:
Circle(intx,inty,intra):
Point(x,y)//B
{r=ra;}
voidSetr(intra)
{r=ra;}
doubleArea()//求圆的面积
{returnPI*r*r;}
voidMove(intx_offset,inty_offset)//将圆心坐标平移
{
intx1=Getx();//存取基类的私有成员
inty1=Gety();//D
x1+=x_offset;y1+=y_offset;
Setxy(x1,y1);//E
}
voidShowCircle()
{
ShowPoint();//F
cout<<"Radius:
"<cout<<"Area:
"<
}
};
voidmain()
{
Circlec(1,1,1);
c.ShowCircle();
c.Move(1,2);
c.ShowCircle();
c.Setxy(4,5);//重新置圆心坐标
c.Setr
(2);//重新置半径值
c.ShowCircle();
}
实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)
(1)记录程序的运行结果
Point(1,1)
Radius:
1Area:
3.14159
Point(2,3)
Radius:
1Area:
3.14159
Point(4,5)
Radius:
2Area:
12.5664
(2)测试能否将move函数体改写为
x=x+x_offset;
y=y+y_offset;
不可以,因为x和y是Point类的私有(private)成员。
2.#include
#definePI3.14159
classPoint
{protected:
//A
intx,y;
public:
Point(inta=0,intb=0){x=a;y=b;}
voidShowPoint()
{cout<<"Point:
("<intGetx(){returnx;}
intGety(){returny;}
voidSetxy(inta,intb){x=a;y=b;}
};
classRadius
{protected:
intr;
public:
Radius(intra=0){r=ra;}
voidSetr(intra){r=ra;}
intGetr(){returnr;}
};
classCircle:
publicPoint,publicRadius
{
public:
Circle(intx,inty,intra):
Point(x,y),Radius(ra)//D
{}
doubleArea()
{returnPI*r*r;}//直接访问基类的保护成员
voidMove(intx_offset,inty_offset)
{x+=x_offset;y+=y_offset;}
voidShowCircle()
{
ShowPoint();
cout<<"Radius:
"<cout<<"Area:
"<
}
};
voidmain()
{
Circlec(1,1,1);
c.ShowCircle();
c.Move(1,2);
c.ShowCircle();
c.Setxy(4,5);
c.Setr
(2);
c.ShowCircle();
}
(1)记录程序的运行结果
Point:
(1,1)
Radius:
1Area:
3.14159
Point:
(2,3)
Radius:
1Area:
3.14159
Point:
(4,5)
Radius:
2Area:
12.5664
(2)为什么可以直接使用x,y,
x和y是Point类的保护(protected)成员
(3)如果x,y在基类中是私有的行吗?
不行
#include
classBase1
{protected:
intdata1;
public:
Base1(inta=0)
{data1=a;
cout<<"BaseConstructor1\n";}
~Base1()
{cout<<"BaseDestructor1\n";}};
classBase2
{protected:
intdata2;
public:
Base2(inta=0)
{data2=a;
cout<<"BaseConstructor2\n";
}
~Base2()
{cout<<"BaseDestructor2\n";}
};
classDerived:
publicBase1,publicBase2
{
intd;
public:
Derived(intx,inty,intz):
Base1(x),Base2(y)
{d=z;cout<<"DerivedConstructor\n";}
~Derived()
{cout<<"DerivedDestructor\n";}
voidShow()
{cout<};
voidmain()
{Derivedc(1,2,3);
c.Show();
}
(1)记录程序的运行结果
BaseConstructor1
BaseConstructor2
DerivedConstructor
1,2,3
DerivedDestructor
BaseDestructor2
BaseDestructor1
#include
classBase1
{
protected:
intdata1;
public:
Base1(inta=8)
{data1=a;
cout<}
~Base1()
{cout<};
classBase2
{
protected:
intdata2;
public:
Base2(inta=9)
{data2=a;
cout<}
~Base2()
{cout<};
classDerived:
publicBase1,publicBase2
{intd;
Base1b1,b2;
public:
Derived(intx,inty,intz):
Base1(x),Base2(y),b1(x+y),b2(x+z)
{d=z;cout<<"DerivedConstructor\n";}
~Derived()
{cout<<"DerivedDestructor\n";}
voidShow()
{cout<};
voidmain()
{Derivedc(1,2,3);
c.Show();
}
(1)记录程序的运行结果
1,BaseConstructor1
2,BaseConstructor2
3,BaseConstructor1
4,BaseConstructor1
DerivedConstructor
1,2,3
DerivedDestructor
4,BaseDestructor1
3,BaseDestructor1
2,BaseDestructor2
1,BaseDestructor1
5.#include
#include
usingnamespacestd;
classbase{
protected:
floatprice;//单价
stringplace;//产地
intcount;//库存量
public:
base(float&p,int&c,string&pl)
{price=p;count=c;place=pl;}
voidin_something(intadd_cnt)
{count+=add_cnt;}
voidout_something(intdel_cnt)
{
if(del_cnt>count)count=0;//变0保护小于0的按0计算
elsecount-=del_cnt;
}
doubletotal_price()
{returnprice*count;}
intr_count()
{returncount;}
};
classshirt:
publicbase{
protected:
stringmaterial;
public:
shirt(float&p,int&c,string&pl,string&m):
base(p,c,pl)
{material=m;}
voidprint()
{
cout<<"\n衬衣"<<"\t产地:
"<"<<<"\n库存量:
"<"<}
};
classwardrobe:
publicbase{
protected:
stringwood;
stringcolor;
public:
wardrobe(float&p,int&c,string&pl,string&w,string&co):
base(p,c,pl)
{
wood=w;color=co;
}
voidprint()
{
cout<<"\n立柜"<<"\t产地:
"<"<"<"
<"<}
};
classcap:
publicshirt{
protected:
stringstyle;
public:
cap(float&p,int&c,string&pl,string&m,string&st):
shirt(p,c,pl,m)
{
style=st;
}
voidprint()
{
cout<<"\n帽子"<<"\t产地:
"<"<<<"样式:
"<