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  。运行的时候,是不是有异常提示?解决方法?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值