标题:帮忙看一下这里常成员变量怎么可以更新了
只看楼主
rainy919
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-11-25
 问题点数:0 回复次数:0 
帮忙看一下这里常成员变量怎么可以更新了
为什么常成员变量也可以更新了啊
*a.h
class TDate1
{
public:
TDate1 (int y=2005, int m=10);
TDate1 (int y, int m, int d);
~TDate1 ();
void print ();
void print()const;
static void GetNum();
friend void SetNum(int n);
private:
const int a;
int year,month,day;
static int num;
};
*a.cpp
#include <iostream.h>
#include "a.h"

int TDate1::num=2;
TDate1::TDate1(int y, int m):a(y)
{
year =y;
month =m;
cout << "Constructor1 called "<<endl;
}

TDate1::TDate1(int y, int m, int d):a(y)
{
year =y;
month =m;
day =d;
cout << "Constructor2 called "<<endl;
}


TDate1::~TDate1()
{
cout << "Destructor called "<<endl;
}

void TDate1::print()
{
cout <<year<<"."<< month <<"."<<a<<endl;
}

void TDate1::print()const
{
cout<<year<<"."<<month<<"."<<day<<"."<<a <<endl;
}

void TDate1:: GetNum()
{ cout<<num<<endl;}

void SetNum(int n)
{ TDate1::num=n;}
*main.cpp
#include <iostream.h>
#include "a.h"

void main ()
{
TDate1 today;
today.print();
TDate1 tomorrow(2008);
tomorrow.print();
TDate1::GetNum();
SetNum(4);
TDate1::GetNum();
SetNum(8);
TDate1::GetNum();
TDate1 const Next(2005,11,11);
Next.print();
}

结果

Constructor1 called
2005.10.2005
Constructor1 called
2008.10.2008
2
4
8
Constructor2 called
2005.11.11.2005
Destructor called
Destructor called
Destructor called
Press any key to continue




搜索更多相关主题的帖子: private public friend border 
2005-11-26 14:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-35236-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.564528 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved