C++類常函數以及內聯函數

 1。請將下列代碼,在2008VC環境下編譯運行。

/*
 Date : 20091109
 For  : class const function and inline function
 Book : qianNeng
 Page : 276/572
 
*/
#include <iostream>
#include <iomanip>
//.............................

using namespace std;
//=============================

class Date
{
 int year,month,day;

public:
 void set(int y, int m, int d);
 bool isLeapYear()const;    // const function
 void print()const;     // conse function
};
//----------------------------------

//inline void Date::set(int y, int m, int d)
void Date::set(int y, int m, int d)
{
 year = y;
 month = m;
 day = d;
}
//KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK

//inline bool Date::isLeapYear()const   // const function
bool Date::isLeapYear()const
{
 return (((0 == year % 4)
    && (0 != year % 100))
    || (0 == year % 400));
}
//ppppppppppppppppppppppppppppppppppp

//inline void Date::print()const    // const function
void Date::print()const
{
 cout << setfill('0') << endl;
 
 /*
 cout << setw( 4 ) << year << '-'
      setw( 2 ) << month << '-'  //question 1
    setw( 2 ) << day << "/n";
 */
 cout << "The date: " << year << '-'
   << month << '-' << day << " is leap year." << endl;

 cout << setfill(' ') << endl;
}
//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

void main()
{
 char cEnd;
 int y1 = 2008,m1 = 8,d1 = 8;
 //Date D1;
 Date *ptrD1 = new Date;

 cout << "Please input year."<< endl;
 cin >> y1;
 if(0 >= y1)
 {
  y1 = 2009;
 }
 cout << "Please input month."<< endl;
 cin >> m1;
 if(0 >= m1)
 {
  m1 = 1;
 }
 cout << "Please input day."<< endl;
 cin >> d1;
 if(0 >= d1)
 {
  d1 = 1;
 }

 //D1.set(y1,m1,d1);
 ptrD1->set(y1,m1,d1);      //one method

 if(ptrD1->isLeapYear())
 {
  (*ptrD1).print();      //other method
 }

 delete ptrD1;

 cout << "Please intput any key to end." <<endl;
 cin >> cEnd;
 cout << " " << endl;
2  。運行的時候,是不是有異常提示?解決方法?

發佈了48 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章