C++第八章 类和对象(一)

本文介绍了三个C++项目案例,包括旱冰场成本计算、时间类的实现与拓展、长方柱体积及表面积计算。通过这些项目,展示了如何设计类、实现成员函数,并进行对象的创建与使用。
【项目2 - 修旱冰场】(Circle类)一个圆形的旱冰场地,场地内抹水泥,造价为每平方米20元,围栏用木条围成,每米造价35元。设计一个Circle类,可以求出圆的面积和边长,进而支持求出旱冰场的等价。请在下面提示的基础上完成程序,其中需要做的工作包括:(1)在类声明中声明相关的公共成员函数;(2)在类声明和main()函数之间,定义声明的成员函数。
#include <iostream>

using namespace std;
#define cementprice 20
#define fenceprice 35
#define pi 3.1415926

class 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 - 时间类】阅读、运行程序后,按要求增加类的功能
  1. /* 
  2.  *文件头部的注释请自行加上 
  3. */  
  4. #include <iostream>  
  5. using namespace std;  
  6. class Time  
  7. {  
  8. public:  
  9.     void set_time( );     
  10.     void show_time( );    
  11. private:   
  12.     bool is_time(intintint);  
  13.     int hour;  
  14.     int minute;  
  15.     int sec;  
  16. };  
  17. void Time::set_time( )   
  18. {   char c1,c2;  
  19.     cout<<"请输入时间(格式hh:mm:ss)";  
  20.     while(1)  
  21.     {   cin>>hour>>c1>>minute>>c2>>sec;  
  22.         if(c1!=':'||c2!=':')  
  23.             cout<<"格式不正确,请重新输入"<<endl;  
  24.         else if (!is_time(hour,minute,sec))  
  25.             cout<<"时间非法,请重新输入"<<endl;  
  26.         else   
  27.             break;  
  28.     }  
  29. }  
  30. void Time::show_time( )        
  31. {   cout<<hour<<":"<<minute<<":"<<sec<<endl;  
  32. }  
  33. bool Time::is_time(int h,int m, int s)  
  34. {   if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)  
  35.         return false;  
  36.     return true;  
  37. }  
  38. int main( )  
  39. {   Time t1;    
  40.     Time &t2=t1;  
  41.     t1.set_time( );     
  42.     t2.show_time( );  
  43.     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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值