| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 525 人关注过本帖
标题:帮忙看一下这里常成员变量怎么可以更新了
只看楼主 加入收藏
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
快速回复:帮忙看一下这里常成员变量怎么可以更新了
数据加载中...
 
   



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

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