欢迎来到天天文库
浏览记录
ID:38717216
大小:96.00 KB
页数:11页
时间:2019-06-18
《河北工业大学-C++实验报告实验二》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验内容1.下面是一个计算器类的定义,请完成该类成员函数的实现。#includeusingnamespacestd;classCounter{public:Counter(intnumber);voidincrement();//给原值加1voiddecrement();//给原值减1intgetValue();//取得计数器值intprint();//显示计数private:intvalue;};Counter::Counter(intnumber){value=number;}voidCounter::increment(){value+=1;}voi
2、dCounter::decrement(){value-=1;}intCounter::getValue(){returnvalue;}intCounter::print(){cout<usingnamespacestd;classDate{public:voidprintDate();//显示日期voidsetDay(intd);//设置日的值voidsetMonth(intm);//设置月的值voidsetYear(inty);//设置年的值private
3、:intday,month,year;};intmain(){DatetestDay;testDay.setDay(5);testDay.setMonth(10);testDay.setYear(2014);testDay.printDate();return0;}voidDate::printDate(){cout<4、3.建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一个double变量中。在类cylinder中包含一个成员函数vol(),用来显示每个cylinder对象的体积。#includeusingnamespacestd;classcylinder{public:cylinder(doublea,doubleb);voidcal();voidvol();private:doubler,h,v;};cylinder::cylinder(doublea,doub5、leb){r=a;h=b;v=0;}voidcylinder::cal(){v=h*r*r*3.14;}voidcylinder::vol(){cout<usingnamespacestd;classbook{public:book(intn);intprintf();private:intqu,price;};book::book(intn){qu6、=n;price=10*n;}intbook::printf(){cout<usingnamespacestd;classbook{public:book(intn);intprintf();private:intqu,price;};book::7、book(intn){qu=n;price=10*n;}intbook::printf(){cout<printf();p--;}}6构建一个类Stock,含字符数组stockcode[]及整型数据成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock
4、3.建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一个double变量中。在类cylinder中包含一个成员函数vol(),用来显示每个cylinder对象的体积。#includeusingnamespacestd;classcylinder{public:cylinder(doublea,doubleb);voidcal();voidvol();private:doubler,h,v;};cylinder::cylinder(doublea,doub
5、leb){r=a;h=b;v=0;}voidcylinder::cal(){v=h*r*r*3.14;}voidcylinder::vol(){cout<usingnamespacestd;classbook{public:book(intn);intprintf();private:intqu,price;};book::book(intn){qu
6、=n;price=10*n;}intbook::printf(){cout<usingnamespacestd;classbook{public:book(intn);intprintf();private:intqu,price;};book::
7、book(intn){qu=n;price=10*n;}intbook::printf(){cout<printf();p--;}}6构建一个类Stock,含字符数组stockcode[]及整型数据成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock
此文档下载收益归作者所有