一个c++静态数据成员的问题

来源:百度知道 编辑:UC知道 时间:2024/09/26 10:51:53
请看下面的程序:
#include "stdafx.h"
#include<iostream.h>
#include<iomanip.h>
class lamp
{
private:
static int n;
bool mark;
// static int m;
public:
lamp();
void display();
void on();
void off();
int getn();
};
lamp::lamp()
{
// n=m+1;
// m++;
n++;
mark=false;
}
void lamp::display()
{
cout<<"number="<<n;
if(mark==true)
cout<<","<<setw(4)<<"on"<<endl;
else
cout<<","<<setw(3)<<"off"<<endl;
}
void lamp::on()
{
mark=true;
}
void lamp::off()
{
mark=false;
}
int lamp::getn()
{
return n;
}

class classroom
{
private:
int roomnumber;
lamp *p;
int cm;
public:
classroom();
classroom(int,in

我觉得这个程序有点问题
lamp没有自己的编号, 我增加了变量m作为编号,getm获取m的值,你查询指定lamp的地方也不对.应该getm,不是getn

#include<iostream.h>
#include<iomanip.h>
class lamp
{
private:
static int n;
bool mark;
int m;
public:
lamp();
void display();
void on();
void off();
int getn();
int getm();
};
lamp::lamp()
{
// n=m+1;
m = n;
n++;
mark=false;
}
void lamp::display()
{
cout<<"number="<<m+1;
if(mark==true)
cout<<","<<setw(3)<<"on"<<endl;
else
cout<<","<<setw(3)<<"off"<<endl;
}
void lamp::on()
{
mark=true;
}
void lamp::off()
{
mark=false;
}
int lamp::getn()
{
return n;
}
int lamp::getm()
{
return m;