C++第八章 类和对象(一)
来源:互联网 发布:巫师3优化补丁 编辑:程序博客网 时间:2024/05/16 17:47
【项目2 - 修旱冰场】(Circle类)一个圆形的旱冰场地,场地内抹水泥,造价为每平方米20元,围栏用木条围成,每米造价35元。设计一个Circle类,可以求出圆的面积和边长,进而支持求出旱冰场的等价。请在下面提示的基础上完成程序,其中需要做的工作包括:(1)在类声明中声明相关的公共成员函数;(2)在类声明和main()函数之间,定义声明的成员函数。
【项目3 - 时间类】阅读、运行程序后,按要求增加类的功能
要求:请在原类基础上,增加下列成员函数,要求前三个设计成内置函数,在main()数中增加适当的调用以展示扩充类定义后的功能(最好能一次运行)。
(1)由键盘输入3个长方柱的长、宽、高;
(2)计算长方柱的体积(volume)和表面积(areas);
#include <iostream>using namespace std;#define cementprice 20#define fenceprice 35#define pi 3.1415926class Circle{ public: double totalcost; void setradius(double); void cost(); private: double radius;};void Circle::setradius(double r){ radius=r;}void Circle::cost(){ totalcost=pi*radius*radius*cementprice+2*pi*radius*fenceprice; cout<<"The total cost of the project is:"<<totalcost;}int main(){ Circle c1; double r; cout<<"Please input the length of radius:"; cin>>r; c1.setradius(r); c1.cost(); return 0;}/*体会:感受了一下公有成员函数调用私有数据成员*/
【项目3 - 时间类】阅读、运行程序后,按要求增加类的功能
- /*
- *文件头部的注释请自行加上
- */
- #include <iostream>
- using namespace std;
- class Time
- {
- public:
- void set_time( );
- void show_time( );
- private:
- bool is_time(int, int, int);
- int hour;
- int minute;
- int sec;
- };
- void Time::set_time( )
- { char c1,c2;
- cout<<"请输入时间(格式hh:mm:ss)";
- while(1)
- { cin>>hour>>c1>>minute>>c2>>sec;
- if(c1!=':'||c2!=':')
- cout<<"格式不正确,请重新输入"<<endl;
- else if (!is_time(hour,minute,sec))
- cout<<"时间非法,请重新输入"<<endl;
- else
- break;
- }
- }
- void Time::show_time( )
- { cout<<hour<<":"<<minute<<":"<<sec<<endl;
- }
- bool Time::is_time(int h,int m, int s)
- { if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
- return false;
- return true;
- }
- int main( )
- { Time t1;
- Time &t2=t1;
- t1.set_time( );
- t2.show_time( );
- return 0;
- }
下为自己的代码
#include <iostream>using namespace std;class Time { private: int hh,mm,ss; bool istime(int,int,int); public: void settime(); void showtime();};bool Time::istime(int h,int m,int s){ if(h<0||h>24||m<0||m>60||s<0||s>60) return false; else return true;}void Time::settime(){ while(1){ cout<<"请输入时间,格式为(hh:mm:ss):"; char c1,c2; int h,m,s; cin>>h>>c1>>m>>c2>>s; if(c1!=':'||c2!=':')cout<<"您输入的时间格式不正确,请重新输入!"<<endl; else if(!istime(h,m,s))cout<<"您输入的时间非法,请重新输入!"<<endl; else{ hh=h;mm=m;ss=s; break;} }}void Time::showtime(){ cout<<"The time is"<<hh<<":"<<mm<<":"<<ss<<endl;}int main(){ /*Time *t1,*t2; t1=new Time;//定义类的指针时需要类的实例化,定义类的对象则不需要如下 t2=new Time; t1->settime(); t1->showtime(); t2->settime(); t2->showtime();*/ Time t1,t2; t1.settime(); t1.showtime(); t2.settime(); t2.showtime(); return 0;}
要求:请在原类基础上,增加下列成员函数,要求前三个设计成内置函数,在main()数中增加适当的调用以展示扩充类定义后的功能(最好能一次运行)。
- add_a_sec() //增加1秒钟
- add_a_minute() //增加1分钟
- add_an_hour() //增加1小时
- add_seconds(int) //增加n秒钟
- add_minutes(int) //增加n分钟
- add_hours(int) //增加n小时
#include <iostream>using namespace std;class Time { private: int hh,mm,ss; bool istime(int,int,int); public: void add_a_sec(); void add_a_min(); void add_a_hou(); void add_secs(int); void add_mins(int); void add_hous(int); void settime(); void showtime();};inline void Time::add_a_sec(){ if(ss==59){ ss=0; if(mm==59){ mm=0; if(hh==23)hh=0; else hh=hh+1; } else mm=mm+1; } else ss=ss+1;}inline void Time::add_a_min(){ if(mm==59){ mm=0; if(hh==23)hh=0; else hh=hh+1; } else mm=mm+1;}inline void Time::add_a_hou(){ if(hh==23)hh=0; else hh=hh+1;}void Time::add_secs(int secs){ int tempss=ss+secs; ss=tempss%60; int tempmm=mm+tempss/60; mm=tempmm%60; int temphh=hh+tempmm/60; hh=temphh%24;}void Time::add_mins(int mins){ mm=(mm+mins)%60; int temphh=hh+(mm+mins)/60; hh=temphh%24;}void Time::add_hous(int hous){ hh=(hh+hous)%24;}bool Time::istime(int h,int m,int s){ if(h<0||h>24||m<0||m>60||s<0||s>60) return false; else return true;}void Time::settime(){ while(1){ cout<<"请输入时间,格式为(hh:mm:ss):"; char c1,c2; int h,m,s; cin>>h>>c1>>m>>c2>>s; if(c1!=':'||c2!=':')cout<<"您输入的时间格式不正确,请重新输入!"<<endl; else if(!istime(h,m,s))cout<<"您输入的时间非法,请重新输入!"<<endl; else{ hh=h;mm=m;ss=s; break;} }}void Time::showtime(){ cout<<"The time is"<<hh<<":"<<mm<<":"<<ss<<endl;}int main(){ Time t1; t1.settime(); t1.showtime(); while(1){ cout<<"您要修改时间吗:1-是,0-否"<<endl; char judge; cin>>judge; if(judge!='1'&&judge!='0'){cout<<"您选择的代号有误,请重新选择!"<<endl;} else if(judge=='1'){ cout<<"请输入您要增加的小时数,分钟数和秒数(均为整形):"; int hous,mins,secs; cin>>hous>>mins>>secs; t1.add_hous(hous); t1.add_mins(mins); t1.add_secs(secs); break; } else if(judge=='0'){cout<<"您选择的是不修改时间!";break;} } cout<<"修改后的时间是:"; t1.showtime(); //下面测试增加一时一分一秒 Time t2; t2.settime(); t2.showtime(); cout<<"给现在的时间增加一个小时,一分钟,一秒钟!"<<endl; t2.add_a_hou(); t2.add_a_min(); t2.add_a_sec(); t2.showtime(); return 0;}
【项目4 - 长方柱类】(改自教材P262第6题)仿照你阅读过的程序,编写基于对象的程序,求3个长方柱(Bulk)的体积。数据成员包括长(length)、宽(width)、高(heigth)、体积,要求用成员函数实现下面的功能:(1)由键盘输入3个长方柱的长、宽、高;
(2)计算长方柱的体积(volume)和表面积(areas);
(3)输出这3个长方柱的体积和表面积;
#include <iostream>using namespace std;class bulk{ public: void setdata(); void showvolandare(); private: double length,width,heigth,volume,areas; double vol(); double are();};void bulk::setdata(){ cout<<"请输入该长方柱的长,宽,高(三者都是double型):"; double ll,ww,hh; cin>>ll>>ww>>hh; length=ll;width=ww;heigth=hh;}double bulk::vol(){ return length*width*heigth;}double bulk::are(){ return 2*(heigth+length+width);}void bulk::showvolandare(){ cout<<"The volume of the bulk is "<<vol()<<endl<<"the areas of the bulk is "<<are()<<endl;}int main(){ bulk *b1,*b2,b3; b1=new bulk; b2=new bulk; b1->setdata();b1->showvolandare(); b2->setdata();b2->showvolandare(); b3.setdata();b3.showvolandare(); return 0;}
- C++第八章 类和对象(一)
- 【C和指针】第八章(1)-->一维数组
- C++第八章 类和对象(二)
- C和指针 第八章 数组 问题一
- [C++]类和对象(一)
- C和指针读书笔记(第八章)
- C++学习笔记(第八章 类和对象 初识对象 之一)
- 第八章:java常用类(一)
- C++高级编程 第八章: 掌握类和对象
- javascript笔记--(第八章)对象和数组
- Objective-C对象之类对象和元类对象(一)
- Objective-C对象之类对象和元类对象(一)
- Objective-C对象之类对象和元类对象(一)
- 第八章 用户界面(一)
- C和指针课后习题(第八章)
- 第八章 数组 [C和指针]
- 《C和指针》第八章 数组
- 记录-C和指针第八章-数组
- CXF配置环境变量
- SVN报错SVN Path is not a working copy directory
- Android推送方案分析(MQTT/XMPP/GCM)
- SVN插件提交报错Attempted to lock an already-locked dir svn: Working copy
- 如何在freemarker模板里面使用struts2的标签
- C++第八章 类和对象(一)
- 5.2.1使用Ant自动生成CHM格式的javadoc失败
- eclipse 配置tomcat
- 关于位运算
- 2013.8.6 学习笔记《windows核心编程》(九) 消息获取,消息派发与消息分类
- python 笔记
- ListView的使用
- Linux内核2.6的进程调度
- HDU-4647