您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 含参默认构造函数多文件结构读写文件
《C++程序设计》实验报告题目含参默认构造函数.多文件结构.读写文件重载提取运算符.重载插入运算计算机科学与技术学院2015年4月30日[实验报告]0目录1.题目功能…………………………………………………12.程序所达到的功能………………………………………13.类中各函数的功能作用…………………………………14.详细设计…………………………………………………35.调试分析…………………………………………………36.用户使用说明……………………………………………47.测试结果…………………………………………………5[实验报告]11、题目功能创建Goods类的头文件Goods.h,声明私有数据,并进行函数的类内声明,在Goods.cpp文件中进行类外定义。含参的默认构造;函数在类内声明时同时进行类内定义赋予初值。用友元函数进行流重载,分别重载提取运算符和插入运算符。声明get函数,用于获取封装在类内的私有数据成员,注意返回值类型。声明set函数,通过形参对私有数据进行赋值。2、程序所能达到的功能对商品信息进行处理,对商品的编号、名称、进价、库存数量、售价、销售数量、供应商和进货日期进行系统化的统计,便于销售这的查询,以利于销售。3、类中各函数的作用1.默认含参构造函数Goods.h中Goods(stringid100,stringna=TM,floatip=0,intam=0,floatop=0,intsa=0,stringgys1=TM,Stringda=2015-1-1);对封装在类内的私有数据成员进行初始化。Goods.cpp中Goods::Goods(stringid1stringna,floatip,intam,floatop,intsa,stringgys1,stringda){id=id1name=na;iprice=ip;amount=am;oprice=op;salecount=sa;gys=gys1;date=da;//通过形参对私有数据进行赋值。}::为作用域运算符,表明函数所属的类别。2.友元函数对运算符的重载Goods.h中friendistream&operator(istream&is,Goods&go);Goods.cpp中istream&operator(istream&is,Goods&go){cout请输入商品编号名称进价库存数量售价销售数量供应商进货时期endl;isgo.idgo.namego.ipricego.amountgo.opricego.salecountgo.gysgo.date;//接受用户所输入的信息。returnis;}[实验报告]22.友元函数对运算符的重载Goods.h中friendostream&operator(ostream&os,constGoods&go);Goods.cpp中ofstream&operator(ofstream&ofs,Goods&gof){cout商品编号setw(10)名称setw(10)进价setw(10)库存数量setw(10)售价setw(10)销售数量setw(10)供应商setw(10)进货时期endl;ofsgof.idsetw(10)gof.namesetw(10)gof.ipricesetw(10)gof.amountsetw(10)gof.opricesetw(10)gof.salecountsetw(10)gof.gysgof.;returnofs;}输出用户所输入的信息,作用是保护被引用的对象,使其不被随意篡改。3、get函数的应用stringgetid(){returnid;}//获取封装在类内的私有数据成员,注意返回值类型stringgetname(){returnname;}floatgetprice(){returniprice;}intgetamount(){returnamount;}floatgetoprice(){returnoprice;}intgetsalecount(){returnsalecount;}stringgetgys(){returngys;}stringgetdate(){returndate;}4、set函数的应用voidsetid(stringid1){id=id1;}//set函数用于对类内的私有数据成员进行赋值voidsetname(stringna){name=na;}//通过形参改变封装在类内的私有数据voidsetiprice(floatip){iprice=ip;}voidsetamount(intam){amount=am;}voidsetoprice(floatop){oprice=op;}voidsetsalecount(intsa){salecount=sa;}voidsetgys(stringgys1){gys=gys1;}voidsetdate(stringda){date=da;}[实验报告]3多文件结构:处理某个.h文件被多个源文件重复包含的问题。Goods.h文件在其中加入了宏名为GOODS_H的条件编译命令,这样程序中即使重复包含该头文件多次,也不会引发重复定义的错误。#ifndefGOODS_H#defineGOOGS_HclassGoods{...//类内声明};...//类外定义#endif4、详细设计整个流程的设计都遵循类内声明,类外定义的原则或类内声明时直接定义。在主函数中创建类对象,进行测试,使程序完成相应的输入和输出功能。5、调试分析[实验报告]45.用户说明Goods.h:定义八个相关的私有数据stringid(商品编号);stringname(商品名称);floatiprice(商品进价);intamount(商品进价);floatoprice(商品售价);intsalecount(商品的销售量);stringgys(供应商);stringdate(进货日期);Goods.cpp:istream&operator(istream&is,Goods&go){cout请输入商品编号商品名字商品进价商品数量商品售价销售数量供应商进货日期endl;//提醒用户输入商品的各个信息isgo.idgo.namego.ipricego.amountgo.opricego.salecountgo.gysgo.date;//接受用户输入的各个商品信息returnis;}ostream&operator(ostream&os,constGoods&go){cout商品编号setw(10)名称setw(10)进价setw(10)库存数量setw(10)售价setw(10)销售数量setw(10)供应商setw(10)进货时期endl;//输出标题行。osgo.idsetw(10)go.namesetw(10)go.ipricesetw(10)go.amountsetw(10)go.opricesetw(10)go.salecountsetw(10)go.gysgo.date;//输出用户所定义的商品的各个信息returnos;}6.测试结果Goods.h的正确代码:#includeiostream#ifndefGOODS_H#defineGOOGS_H#includestringusingnamespacestd;[实验报告]5classGoods{private:stringid;stringname;floatiprice;intamount;floatoprice;intsalecount;stringgys;stringdate;public:Goods(stringid1=00,stringna=Nothing,floatip=0,intam=0,floatop=0,intsa=0,stringgys1=111,stringda=2015);//含参默认构造函数friendistream&operator(istream&is,Goods&go);友元函数对运算符的重载friendostream&operator(ostream&os,constGoods&go);友元函数对运算符的重载stringgetid(){returnid;}//获取封装在类内的私有数据成员,注意返回值类型stringgetname(){returnname;}floatgetiprice(){returniprice;}intgetamount(){returnamount;}floatgetoprice(){returnoprice;}intgetsalecount(){returnsalecount;}stringgetgys(){returngys;}stringgetdate(){returndate;}voidsetid(stringid1){id=id1;}//set函数用于对类内的私有数据成员进行赋值voidsetname(stringna){name=na;}//通过形参改变封装在类内的私有数据voidsetiprice(floatip){iprice=ip;}voidsetamount(intam){amount=am;}[实验报告]6voidsetoprice(floatop){oprice=op;}voidsetsalecount(intsa){salecount=sa;}voidsetgys(stringgys1){gys=gys1;}voidsetdate(stringda){date=da;}};#endifGoods.cpp:#includeiostreamusingnamespacestd;#includestring#includegoods.hGoods::Goods(stringid1=00,stringna=Nothing,floatip=0,intam=0,floatop=0,intsa=0,stringgys1=111,stringda=2015);{id=id1;name=na;iprice=ip;//形参赋值给私有数据amount=am;oprice=op;salecount=sa;gys=gys1;date=da;}istream&operator(istream&is,Goods&go){cout请输入商品编号商品名字商品进价商品数量商品售价销售数量供应商进货日期endl;isgo.idgo.namego.ipricego.amountgo.opricego.salecountgo.gysgo.date;returnis;}ostream&operator(ostream&os,constGoods&go){[实验报告]7cout商品编号setw(10)名称setw(10)进价setw(10)库存数量setw(10)售价setw(10)销售数量setw(10)供应商setw(10)进货时期endl;osgo.idsetw(10)go.namesetw(10)go.ipricesetw(10)go.amountsetw(10)go.opricesetw(10)go.salecountsetw(10)go.gysgo.dat;returnos;}voidmain(){Goodsg;//定义类对象gcing;//调用友元函数重载的运算符co
本文标题:含参默认构造函数多文件结构读写文件
链接地址:https://www.777doc.com/doc-2579566 .html