欢迎来到课桌文档! | 帮助中心 课桌文档-建筑工程资料库
课桌文档
全部分类
  • 党建之窗>
  • 感悟体会>
  • 百家争鸣>
  • 教育整顿>
  • 文笔提升>
  • 热门分类>
  • 计划总结>
  • 致辞演讲>
  • 在线阅读>
  • ImageVerifierCode 换一换
    首页 课桌文档 > 资源分类 > DOC文档下载  

    天津理工大学C实验三.doc

    • 资源ID:7109       资源大小:511.50KB        全文页数:19页
    • 资源格式: DOC        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    天津理工大学C实验三.doc

    #理工大学计算机科学与技术学院实验报告至 学年 第 学期课程名称C+程序设计学号学生#年级专业教学班号实验地点实验时间 年 月 日 第 节 至 第 节主讲教师辅导教师实验 三 实验名称派生与继承软件环境C+visual硬件环境无实验目的(1) 理解继承的含义,掌握派生类的定义方法和实现;(2) 理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;(3) 理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;实验内容应包括实验题目、实验要求、实验任务等1.#include <iostream.h>#define PI 3.14159class Point / 定义"点"类int x, y;public:Point<int a=0, int b=0>x=a; y=b;void ShowPoint< >cout<<"Point:<"<<x<<','<<y<<">n"int Getx< > return x; int Gety< >return y;void Setxy<int a, int b>x=a; y=b;class Circle: public Point / 定义"圆"类,公有继承int r; / "圆"的半径 public:Circle<int x, int y, int ra> : Point<x, y> / B r = ra; void Setr<int ra>r = ra; double Area< > /求圆的面积return PI*r*r;void Move<int x_offset, int y_offset> /将圆心坐标平移int x1=Getx< > /存取基类的私有成员int y1=Gety< > / Dx1 += x_offset; y1 += y_offset;Setxy<x1, y1> / Evoid 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< >实验过程与实验结果可包括实验实施的步骤、算法描述、流程、结论等(1) 记录程序的运行结果Point<1, 1>Radius: 1Area: 3.14159Point<2, 3>Radius: 1Area: 3.14159Point<4, 5>Radius: 2Area: 12.5664(2) 测试能否将move函数体改写为x=x+x_offset;y=y+y_offset;不可以,因为x和y是Point类的私有private成员.2.#include <iostream.h>#define PI 3.14159class Point protected: /A int x, y;public:Point<int a=0, int b=0> x=a; y=b; void ShowPoint< > cout<<"Point:<"<<x<<','<<y<<">n" int Getx< > return x; int Gety< > return y; void Setxy<int a, int b> x=a; y=b; ;class Radius 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 Base1protected: int data1;public:Base1<int a=0>data1 = a; cout<<"Base Constructor1n"Base1< >cout<<"Base Destructor1n" ;class Base2protected: int data2;public:Base2<int a=0>data2 = a; cout<<"Base Constructor2n"Base2< >cout<<"Base Destructor2n" ;class Derived: public Base1, public Base2 int d;public:Derived<int x, int y, int z>:Base1<x>, Base2<y> d=z; cout<<"Derived Constructorn"Derived< > cout<<"Derived Destructorn" 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 Base1protected: int data1; public:Base1<int a=8>data1 = a; cout<<data1<<", Base Constructor1n"Base1< >cout<<data1<<", Base Destructor1n"class Base2protected: int data2;public:Base2<int a=9>data2 = a; cout<<data2<<", Base Constructor2n"Base2< >cout<<data2<<", Base Destructor2n"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 Constructorn"Derived< > cout<<"Derived Destructorn"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 baseprotected: 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_t> count+=add_t; void out_something<int del_t> if<del_t>count>count=0; /变0保护 小于0的按0计算 else count-=del_t; double total_price<> return price*count; int r_count<> return count; ;class shirt:public baseprotected: 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 baseprotected: 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 shirtprotected: 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.Duke int main<void>/=输入部分 int cho=1,i; /cho为选择计数器 接收choose函数的返回值 通过if语句判断要执行的语句 int ci=0,wi=0,si=0; /标记 角标 float pci5=0.00,pwi5=0.00,psi5=0.00; /单价 int cou_cap5=0,0,0,0,0,cou_shi5=0,0,0,0,0,cou_war5=0,0,0,0,0; /依次记录帽子 衬衣 立柜的库存量 string pla_cap5="0","0","0","0","0" /保存帽子产地信息 string pla_war5="0","0","0","0","0" /保存立柜产地信息 string pla_shi5="0","0","0","0","0" /保存衬衣产地信息 string col5="0","0","0","0","0" /保存颜色信息 string s_mat5="0","0","0","0","0" /保存衬衣_布料信息 string c_mat5="0","0","0","0","0" /保存帽子_布料信息 string woo5="0","0","0","0","0" /保存木料信息 string sty5="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 material if<si=5>cout<<"超过最大数量输入限制,跳出"continue; /溢出保护 cout<<"设置衬衣的单价:"cin>>p_temp; /设置帽子单价 cout<<"输入产地 布料 初始数量 并以空格隔开"<<endl;cin>>temp1>>temp2>>cou_temp; for<i=0;i<=si;i+> /用于判断是否与之前输入的有冲突 if<temp1=pla_shii&&temp2=s_mati> if<p_temp!=psisi>cout<<"价格冲突,输入最终价格:"cin>>psisi; cout<<"与之前输入的商品重复,将数量叠加"<<endl;cou_shii+=cou_temp; loop_sw=1; /由于重复 事件开关打开 跳过赋值工作 if<loop_sw=0> psisi=p_temp; pla_shisi=temp1; s_matsi=temp2; cou_shisi+=cou_temp; /赋值工作 cou_temp=0; /扫尾清零工作 loop_sw=0; /同上 if<cho=2> /cap ->place material style if<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_capi&&temp2=c_mati&&temp3=styi> if<p_temp!=pcici>cout<<"价格冲突,输入最终价格:"cin>>pcici; cout<<"与之前输入的商品重复,将数量叠加"<<endl;cou_capi+=cou_temp; loop_sw=1; if<loop_sw=0> pcici=p_temp; pla_capci=temp1; c_matci=temp2; styci=temp3; cou_capci+=cou_temp; cou_temp=0; loop_sw=0; if<cho=3> /wardrobe ->place wood color if<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_wari&&temp2=wooi&&temp3=coli> if<p_temp!=pwiwi>cout<<"价格冲突,输入最终价格:"cin>>pwiwi; cout<<"与之前输入的商品重复,将数量叠加"<<endl;cou_wari+=cou_temp; loop_sw=1; if<loop_sw=0> pwiwi=p_temp; pla_warwi=temp1; woowi=temp2; colwi=temp3; cou_warwi+=cou_temp; cou_temp=0; loop_sw=0; if<cho=7>cout<<"输入有误 重新" cho=1; /选择计数器重新设置 /开始初始化工作 shirt s_5= shirt<psi0,cou_shi0,pla_shi0,s_mat0>, shirt<psi1,cou_shi1,pla_shi1,s_mat1>, shirt<psi2,cou_shi2,pla_shi2,s_mat2>, shirt<psi3,cou_shi3,pla_shi3,s_mat3>, shirt<psi4,cou_shi4,pla_shi4,s_mat4> ; cap c_5= cap<pci0,cou_cap0,pla_cap0,c_mat0,sty0>, cap<pci1,cou_cap1,pla_cap1,c_mat1,sty1>, cap<pci2,cou_cap2,pla_cap2,c_mat2,sty2>, cap<pci3,cou_cap3,pla_cap3,c_mat3,sty3>, cap<pci4,cou_cap4,pla_cap4,c_mat4,sty4> ; wardrobe w_5= wardrobe<pwi0,cou_war0,pla_war0,woo0,col0>, wardrobe<pwi1,cou_war1,pla_war1,woo1,col1>, wardrobe<pwi2,cou_war2,pla_war2,woo2,col2>, wardrobe<pwi3,cou_war3,pla_war3,woo3,col3>, wardrobe<pwi4,cou_war4,pla_war4,woo4,col4> ;/输入部分结束/=功能部分 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_shii<< "t布料:"<<s_mati<<"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_capi<<"t布料:"<<c_mati <<"样式:"<<styi<<"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_wari<<"t木料:"<<wooi <<"颜色:"<<coli<<"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_shii<< "t布料:"<<s_mati<<"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_capi<<"t布料:"<<c_mati <<"样式:"<<styi<<"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_wari<<"t木料:"<<wooi <<"颜色:"<<coli<<"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;附录可包括源程序清单或其它说明19 / 19

    注意事项

    本文(天津理工大学C实验三.doc)为本站会员(夺命阿水)主动上传,课桌文档仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知课桌文档(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-1

    经营许可证:宁B2-20210002

    宁公网安备 64010402000986号

    课桌文档
    收起
    展开