天津理工大学计算机科学与技术学院实验报告至学年第学期课程名称C++程序设计学号学生姓名年级专业教学班号实验地点实验时间年月日第节至第节主讲教师辅导教师实验实验名称派生与继承(三)C++visual软件环境无硬件环境实验目的(1)理解继承的含义,掌握派生类的定义方法和实现;(2)理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;(3)理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;计算机科学与技术学院int y1=Gety( ); // Dx1 += x_offset; y1 += y_offset;Setxy(x1, y1); // E }void ShowCircle( ){ShowPoint( ); // Fcout<<" Radius: "<<r<<'\t';cout<<"Area: "<<Area( )<<endl; //G }};void main( ){Circle c(1, 1, 1);c.ShowCircle( );c.Move(1, 2);c.ShowCircle( );c.Setxy(4, 5); //重新置圆心坐标c.Setr(2); //重新置半径值c.ShowCircle( );}{protected: int r;public:Radius(int ra=0){ r = ra; }void Setr(int ra){ r = ra; }int Getr( ){ return r; }};class Circle : public Point, public Radius{public:Circle(int x, int y, int ra) : Point(x, y), Radius(ra)//D { }double Area( ){ return PI*r*r; } //直接访问基类的保护成员void Move(int x_offset, int y_offset){ x += x_offset; y += y_offset; }void ShowCircle( ){ShowPoint( );cout<<"Radius: "<<r<<'\t';cout<<"Area: "<<Area( )<<endl;}};void main( ){Circle c(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: 1 Area: 3.14159Point:(2,3)Radius: 1 Area: 3.14159Point:(4,5)Radius: 2 Area: 12.5664(2)为什么可以直接使用x,y,x和y是Point类的保护(protected)成员(3)如果x,y在基类中是私有的行吗?不行#include <iostream.h>class Base1{protected:int data1;public:Base1(int a=0){ data1 = a;cout<<"Base Constructor1\n";}~Base1( ){ cout<<"Base Destructor1\n"; }};class Base2{protected:int data2;public:Base2(int a=0){ data2 = a;cout<<"Base Constructor2\n";}~Base2( ){ cout<<"Base Destructor2\n"; }};class Derived: public Base1, public Base2{int d;public:Derived(int x, int y, int z):Base1(x), Base2(y){ d=z; cout<<"Derived Constructor\n"; }~Derived( ){ cout<<"Derived Destructor\n"; }void Show( ){ cout<<data1<<','<<data2<<','<<d<<endl; } };void main( ){ Derived c(1, 2, 3);c.Show( );}(1)记录程序的运行结果Base Constructor1Base Constructor2Derived Constructor1,2,3Derived DestructorBase Destructor2Base Destructor1#include <iostream.h>class Base1{protected:int data1;public:Base1(int a=8){ data1 = a;cout<<data1<<", Base Constructor1\n";}~Base1( ){ cout<<data1<<", Base Destructor1\n"; }};class Base2{protected:int data2;public:Base2(int a=9){ data2 = a;cout<<data2<<", Base Constructor2\n";}~Base2( ){ cout<<data2<<", Base Destructor2\n"; }};class Derived:public Base1, public Base2{ int d;Base1 b1, b2;public:Derived(int x, int y, int z) : Base1(x), Base2(y), b1(x+y), b2(x+z){ d=z; cout<<"Derived Constructor\n"; }~Derived( ){ cout<<"Derived Destructor\n"; }void Show( ){ cout<<data1<<','<<data2<<','<<d<<endl; } };void main( ){ Derived c(1, 2, 3);c.Show( );}(1)记录程序的运行结果1, Base Constructor12, Base Constructor23, Base Constructor14, Base Constructor1Derived Constructor1,2,3Derived Destructor4, Base Destructor13, Base Destructor12, Base Destructor21, Base Destructor15.#include<iostream>#include<string>using namespace std;class base{protected:float price; //单价string place; //产地int count; //库存量public:base(float &p,int &c,string &pl){ price=p; count=c; place=pl; }void in_something(int add_cnt){ count+=add_cnt; }void out_something(int del_cnt){if(del_cnt>count)count=0; //变0保护小于0的按0计算else count-=del_cnt;}double total_price(){ return price*count; }int r_count(){ return count; }};class shirt:public base{protected:string material;public:shirt(float &p ,int &c ,string &pl ,string &m):base( p , c, pl){ material=m; }void print(){cout<<"\n衬衣"<<"\t产地:"<<place<<"\t材料:"<<material<<"\n库存量:"<<count<<"\t单价:"<<price<<"\t总价格"<<total_price()<<endl; }};class wardrobe:public base{protected:string wood;string color;public:wardrobe(float &p ,int &c ,string &pl ,string &w ,string &co ):base(p,c,pl){wood=w; color=co;}void print(){cout<<"\n立柜"<<"\t产地:"<<place<<"\t颜色:"<<color<<"\t木料:"<<wood<<"\n库存量:"<<count<<"\t单价:"<<price<<"\t总价格"<<total_price()<<endl;}};class cap:public shirt{protected:string style;public:cap(float &p ,int &c ,string &pl ,string &m,string &st ):shirt(p,c,pl,m){style=st;}void print(){cout<<"\n帽子"<<"\t产地:"<<place<<"\t材料:"<<material<<"样式:"<<style<<"\n库存量:"<<count<<"\t单价:"<<price<<"\t总价格"<<total_price()<<endl;}};int choose(string a) //用于判断下一步执行的命令0.结束1.衬衣2.帽子3.立柜4.查询5.入库6.出库7.出错重输{if(a=="0")return 0; //结束else if(a=="衬衣"||a=="shirt")return 1;else if(a=="帽子"||a=="cap")return 2;else if(a=="立柜"||a=="wardrobe")return 3;else if(a=="查询"||a=="?"||a=="about")return 4;else if(a=="入库"||a=="+"||a=="in")return 5;else if(a=="出库"||a=="-"||a=="out")return 6;else if(a=="帮助"||a=="?"||a=="help")return 8;else return 7;}int total_count(cap &a,shirt &b,wardrobe &c){ return a.r_count()+b.r_count()+c.r_count(); }//Powered by X.Dukeint main(void){//==========输入部分int cho=1,i; //cho为选择计数器接收choose函数的返回值通过if语句判断要执行的语句int ci=0,wi=0,si=0; //标记角标float pci[5]={0.00},pwi[5]={0.00},psi[5]={0.00}; //单价int cou_cap[5]={0,0,0,0,0},cou_shi[5]={0,0,0,0,0},cou_war[5]={0,0,0,0,0}; //依次记录帽子衬衣立柜的库存量string pla_cap[5]={"0","0","0","0","0"}; //保存帽子产地信息string pla_war[5]={"0","0","0","0","0"}; //保存立柜产地信息string pla_shi[5]={"0","0","0","0","0"}; //保存衬衣产地信息string col[5]={"0","0","0","0","0"}; //保存颜色信息string s_mat[5]={"0","0","0","0","0"}; //保存衬衣_布料信息string c_mat[5]={"0","0","0","0","0"}; //保存帽子_布料信息string woo[5]={"0","0","0","0","0"}; //保存木料信息string sty[5]={"0","0","0","0","0"}; //保存样式信息string temp,temp1="0",temp2="0",temp3="0"; //临时寄存字符int cou_temp;float p_temp; //临时记录初始值int loop_sw=0; //事件标记点为0时不跳过非0时跳过//提示性语句cout<<"现在对3种商品\n衬衣(shirt),帽子(cap),立柜(wardrobe) \n实现基本管理功能"<<endl<<"\n===================首先进行初始化工作======================="<<endl;cout<<"即将录入商品名称(也可以是括号中的英文)输入0结束"<<endl;while(cho!=0) //分类输入{cout<<"输入商品名称";cin>>temp;cho=choose(temp);if(cho==1){ //shirt ->place materialif(si==5){cout<<"超过最大数量输入限制,跳出";continue;} //溢出保护cout<<"设置衬衣的单价:";cin>>p_temp; //设置帽子单价cout<<"输入产地布料初始数量并以空格隔开"<<endl;cin>>temp1>>temp2>>cou_temp;for(i=0;i<=si;i++) //用于判断是否与之前输入的有冲突if(temp1==pla_shi[i]&&temp2==s_mat[i]){if(p_temp!=psi[si]){cout<<"价格冲突,输入最终价格:";cin>>psi[si];}cout<<"与之前输入的商品重复,将数量叠加"<<endl;cou_shi[i]+=cou_temp; loop_sw=1;} //由于重复事件开关打开跳过赋值工作if(loop_sw==0){psi[si]=p_temp; pla_shi[si]=temp1; s_mat[si]=temp2; cou_shi[si++]=cou_temp; } //赋值工作cou_temp=0; //扫尾清零工作loop_sw=0; //同上}if(cho==2){ //cap ->place material styleif(ci==5){cout<<"超过最大数量输入限制,跳出";continue;}cout<<"设置帽子单价:";cin>>p_temp; //cout<<"输入产地布料样式初始数量并以空格隔开"<<endl;cin>>temp1>>temp2>>temp3>>cou_temp;for(i=0;i<=ci;i++)if(temp1==pla_cap[i]&&temp2==c_mat[i]&&temp3==sty[i]){if(p_temp!=pci[ci]){cout<<"价格冲突,输入最终价格:";cin>>pci[ci];}cout<<"与之前输入的商品重复,将数量叠加"<<endl;cou_cap[i]+=cou_temp; loop_sw=1;}if(loop_sw==0){pci[ci]=p_temp; pla_cap[ci]=temp1; c_mat[ci]=temp2; sty[ci]=temp3; cou_cap[ci++]=cou_temp; }cou_temp=0;loop_sw=0;}if(cho==3){ //wardrobe ->place wood colorif(wi==5){cout<<"超过最大数量输入限制,跳出";continue;}cout<<"设置立柜单价:";cin>>p_temp;cout<<"输入产地木料颜色初始数量并以空格隔开"<<endl;cin>>temp1>>temp2>>temp3>>cou_temp;for(i=0;i<=wi;i++)if(temp1==pla_war[i]&&temp2==woo[i]&&temp3==col[i]){if(p_temp!=pwi[wi]){cout<<"价格冲突,输入最终价格:";cin>>pwi[wi];}cout<<"与之前输入的商品重复,将数量叠加"<<endl;cou_war[i]+=cou_temp; loop_sw=1;}if(loop_sw==0){pwi[wi]=p_temp; pla_war[wi]=temp1; woo[wi]=temp2; col[wi]=temp3; cou_war[wi++]=cou_temp; }cou_temp=0;loop_sw=0;}if(cho==7)cout<<"输入有误重新";}cho=1; //选择计数器重新设置//开始初始化工作shirt s_[5]={shirt(psi[0],cou_shi[0],pla_shi[0],s_mat[0]),shirt(psi[1],cou_shi[1],pla_shi[1],s_mat[1]),shirt(psi[2],cou_shi[2],pla_shi[2],s_mat[2]),shirt(psi[3],cou_shi[3],pla_shi[3],s_mat[3]),shirt(psi[4],cou_shi[4],pla_shi[4],s_mat[4])};cap c_[5]={cap(pci[0],cou_cap[0],pla_cap[0],c_mat[0],sty[0]),cap(pci[1],cou_cap[1],pla_cap[1],c_mat[1],sty[1]),cap(pci[2],cou_cap[2],pla_cap[2],c_mat[2],sty[2]),cap(pci[3],cou_cap[3],pla_cap[3],c_mat[3],sty[3]),cap(pci[4],cou_cap[4],pla_cap[4],c_mat[4],sty[4])};wardrobe w_[5]={wardrobe(pwi[0],cou_war[0],pla_war[0],woo[0],col[0]),wardrobe(pwi[1],cou_war[1],pla_war[1],woo[1],col[1]),wardrobe(pwi[2],cou_war[2],pla_war[2],woo[2],col[2]),wardrobe(pwi[3],cou_war[3],pla_war[3],woo[3],col[3]),wardrobe(pwi[4],cou_war[4],pla_war[4],woo[4],col[4])};//输入部分结束//============================功能部分int total=0,totalcap=0,totalshi=0,totalwar=0; //用于累计库存总数float ptotal=0.00,stotal=0.00,ctotal=0.00,wtotal=0.00; //用于累计总价值int j=0;cout<<"初始化结束启用基本功能"<<endl<<"\n输入查询或about 查询当前库存情况"<<endl;cout<<"输入+或in或入库实现入库功能"<<endl;cout<<"输入-或out或出库实现出库功能"<<endl;while(cho!=0){if(loop_sw==0){cout<<"请输入相应功能:";cin>>temp;cho=choose(temp);}if(cho==7)cout<<"输入有误重新";if(cho==4){ //查询功能for(i=0;i<si;i++)s_[i].print();for(i=0;i<ci;i++)c_[i].print();for(i=0;i<wi;i++)w_[i].print();for(i=0;i<5;i++){total+=total_count(c_[i],s_[i],w_[i]);ptotal+=(s_[i].total_price()+c_[i].total_price()+w_[i].total_price());stotal+=s_[i].total_price();ctotal+=c_[i].total_price();wtotal+=w_[i].total_price();totalcap+=c_[i].r_count();totalshi+=s_[i].r_count();totalwar+=w_[i].r_count();}cout<<"\n衬衣总库存:"<<totalshi<<"\t价值:"<<stotal<<"\n帽子总库存:"<<totalcap<<"\t 价值:"<<ctotal<<"\n立柜总库存:"<<totalwar<<"\t价值:"<<wtotal<<"\n所有商品库存总量:"<<total<<"\t总价值:"<<ptotal<<endl;if(loop_sw==1)break;}if(cho==5){ //入库while(cho!=0){cout<<"输入要入库的商品种类(输入0结束):";cin>>temp;cho=choose(temp);if(cho)cout<<temp<<"中有以下小类输入编号进行入库操作";if(cho==1){for(i=0;i<si;i++)cout<<"\n编号"<<i<<"\t产地"<<pla_shi[i]<<"\t布料:"<<s_mat[i]<<"\t现有"<<s_[i].r_count();cout<<"\n输入商品编号:";cin>>j;if(si==0){cout<<"无商品"<<endl;}else{while(j<0||j>=si){cout<<"有误重新输入:";cin>>j;}cout<<"入库数量:";cin>>cou_temp;s_[j].in_something(cou_temp);}}if(cho==2){for(i=0;i<ci;i++)cout<<"\n编号"<<i<<"\t产地"<<pla_cap[i]<<"\t布料:"<<c_mat[i]<<"样式:"<<sty[i]<<"\t现有"<<c_[i].r_count();if(ci==0){cout<<"无商品"<<endl;}else{cout<<"\n输入商品编号:";cin>>j;while(j<0||j>=ci){cout<<"有误重新输入:";cin>>j;}cout<<"入库数量:";cin>>cou_temp;c_[j].in_something(cou_temp);}}if(cho==3){for(i=0;i<wi;i++)cout<<"\n编号"<<i<<"\t产地"<<pla_war[i]<<"\t木料:"<<woo[i] <<"颜色:"<<col[i]<<"\t现有"<<w_[i].r_count();if(ci==0){cout<<"无商品"<<endl;}else{cout<<"\n输入商品编号:";cin>>j;while(j<0||j>=wi){cout<<"有误重新输入:";cin>>j;}cout<<"入库数量:";cin>>cou_temp;w_[j].in_something(cou_temp);}}if(cho==7)cout<<"有误重新";}cho=5;}if(cho==6){ //出库while(cho!=0){cout<<"输入要出库的商品种类(输入0结束):";cin>>temp;cho=choose(temp);if(cho)cout<<temp<<"中有以下小类输入编号进行出库操作";if(cho==1){for(i=0;i<si;i++)cout<<"\n编程"<<i<<"\t产地"<<pla_shi[i]<<"\t布料:"<<s_mat[i]<<"\t现有"<<s_[i].r_count();cout<<"\n输入商品编号:";cin>>j;while(j<0||j>=si){cout<<"有误重新输入:";cin>>j;}cout<<"出库数量:";cin>>cou_temp;s_[j].out_something(cou_temp);}if(cho==2){for(i=0;i<ci;i++)cout<<"\n编程"<<i<<"\t产地"<<pla_cap[i]<<"\t布料:"<<c_mat[i] <<"样式:"<<sty[i]<<"\t现有"<<c_[i].r_count();cout<<"\n输入商品编号:";cin>>j;while(j<0||j>=ci){cout<<"有误重新输入:";cin>>j;}cout<<"出库数量:";cin>>cou_temp;c_[j].out_something(cou_temp);}if(cho==3){for(i=0;i<wi;i++)cout<<"\n编程"<<i<<"\t产地"<<pla_war[i]<<"\t木料:"<<woo[i] <<"颜色:"<<col[i]<<"\t现有"<<w_[i].r_count();cout<<"\n输入商品编号:";cin>>j;while(j<0||j>=wi){cout<<"有误重新输入:";cin>>j;}cout<<"出库数量:";cin>>cou_temp;w_[j].out_something(cou_temp);}if(cho==7)cout<<"有误重新";}cho=6;}if(cho==0){loop_sw=1;cho=4;cout<<"=============信息总览==============";} }return 0;}附录(可包括源程序清单或其它说明)。