师徒恋的绊脚石书包网:创建一个汽车类(Car),为其定义两个属性:颜色和型号,为该类创建两个构造函数:

来源:百度文库 编辑:高校问答 时间:2024/05/02 07:48:03

class CCar
{
public:
CCar();
CCar(int nColor = NULL, char *str = NULL);
~CCar();
void SetColor(int nColor);
int GetColor(){return m_Color};
void SetStyle(char *str);
char *GetStyle(){return m_Style};
protected:
int m_Color;
char *m_Style;
};

CCar::CCar()
{
m_Color=0;
m_Style=new char[10];
sprintf(m_Style,"Normal");
}

CCar::CCar(int nColor, char *str)
{
m_Color=nColor;
char *p=str;
while(p!='\0') p++;
delete m_Style;
m_Style=new char[p-str];
sprintf(m_Style,"%s",str);
}
CCar::~CCar()
{
delete m_Style;
}

void CCar::SetColor(int nColor)
{
m_Color=nColor;
}

void CCar::SetStyle(char *str)
{
char *p=str;
while(p!='\0') p++;
delete m_Style;
m_Style=new char[p-str];
sprintf(m_Style,"%s",str);
}

class Car
{
public:
Car();
Car(int color, int mode);
private:
int color;
int mode;
}