const在函数中的作用

   1.修饰参数,说明在函数中是不能修改它的值

   2.修饰返回值,说明函数的返回值是不能被修改的

   3.修饰类成员函数体

实例:

#include<iostream>

#include <assert.h>

using namespace std;

//const输入修饰输入参数

char StringCopy(char *strDestination, const char *strSource)

{

   assert((strDestination!=NULL)&&(strSource!=NULL));

   char * address=strDestination;

   while((*strDestination++ = *strSource++)!='\0')

   NULL;

   return *address;

}

//修饰返回值

const char *GetString(void)

{

   char *szOut="日期输入结束";

   return szOut;

}

class DTime

{

public:

    DTime(int iYear,int iMonth,int iDay)

{

    m_nYear=iYear;

    m_nMonth=iMonth;

m_nDay=iDay;

}

int GetYear() constreturn m_nYear;}//修饰函数体

int GetMonth() constreturn m_nMonth;}

int GetDay() constreturn m_nDay;}

public:

int m_nYear;

int m_nMonth;

int m_nDay;

};

int main()

{

   char szOutStr[256];

   char *szInStr="输入年月日";

   StringCopy(szOutStr,szInStr);

   cout<<szOutStr<<endl;

   int nYear;

   int nMonth;

   int nDay;

   cin>>nYear;

   cin>>nMonth;

   cin>>nDay;

   DTime dt(nYear,nMonth,nDay);

   cout<<dt.GetYear()<<"-"<<dt.GetMonth()<<"-"<<dt.GetDay()<<endl;

   const char *str=GetString();

   cout<<str<<endl;

   system("pause");

   return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值