⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plusair.cpp

📁 本课程主要介绍面向对象程序设计的方法和c++语言的基本概念。以c++语言中的面向对象机制为主。学习者在学习过程中可以通过大量的程序实例和相关练习
💻 CPP
字号:
//plusair.cpp
//uses accumulate() algorithm and plus() function object
#include <iostream>
#include <list>
#include <numeric>          //for accumulate()
using namespace std;
////////////////////////////////////////////////////////////////
class airtime
   {
   private:
      int hours;            //0 to 23
      int minutes;          //0 to 59
   public:
                            //default constructor
      airtime() : hours(0), minutes(0)
         {  }
                            //2-arg constructor
      airtime(int h, int m) : hours(h), minutes(m)
         {  }
      void display() const  //output to screen
         { cout << hours << ':' << minutes; }

      void get()            //input from user
         {
         char dummy;
         cout << "\nEnter airtime (format 12:59): ";
         cin >> hours >> dummy >> minutes;
         }
                            //overloaded + operator
      airtime operator + (const airtime right) const
         {                  //add members
         int temph = hours + right.hours;
         int tempm = minutes + right.minutes;
         if(tempm >= 60)    //check for carry
            { temph++; tempm -= 60; }
         return airtime(temph, tempm); //return sum
         }
                            //overloaded == operator
      bool operator == (const airtime& at2) const
         { return (hours == at2.hours) && 
                  (minutes == at2.minutes); }
                            //overloaded < operator
      bool operator < (const airtime& at2) const
         { return (hours < at2.hours) || 
                  (hours == at2.hours && minutes < at2.minutes); }
                            //overloaded != operator
      bool operator != (const airtime& at2) const
         { return !(*this==at2); }
                            //overloaded > operator
      bool operator > (const airtime& at2) const
         { return !(*this<at2) && !(*this==at2); }
   };  //end class airtime
////////////////////////////////////////////////////////////////
int main()
   {
   char answer;
   airtime temp, sum;
   list<airtime> airlist;   //list of airtimes

   do {                     //get airtimes from user
      temp.get();
      airlist.push_back(temp);
      cout << "Enter another (y/n)? ";
      cin >> answer;
      } while (answer != 'n');
                            //sum all the airtimes
   sum = accumulate( airlist.begin(), airlist.end(),
                     airtime(0, 0), plus<airtime>() );
   cout << "\nsum = ";
   sum.display();           //display sum
   cout << endl;
   return 0;
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -