一道关于私有成员定义求解
# include <iostream.h># include <string.h>
class CRect
{
private:
char *color;
int x_coordinate;
int y_coordinate;
int length;
int width;
public:
CRect(char color1[], int x_coordinate1, int y_coordinate1, int length1, int width1);
void SetColor(char *color1);
void SetSize(int length1, int width1);
void Move(int x_coordinate1, int y_coordinate1);
void Draw();
};
int main(void)
{
CRect A("blue",30,40,20,100);
A.Draw();
//A.SetColor("yellow");
A.SetSize(100,50);
A.Move(30,20);
A.Draw();
return 0;
}
CRect :: CRect (char color1[], int x_coordinate1, int y_coordinate1, int length1, int width1)
{
strcpy(color,color1);
x_coordinate = x_coordinate1;
y_coordinate = y_coordinate1;
length = length1;
width = width1;
}
void CRect :: SetColor(char *color1)
{
strcpy(color,color1);
}
void CRect :: SetSize(int length1, int width1)
{
length = length1;
width = width1;
}
void CRect :: Move(int x_coordinate1, int y_coordinate1)
{
x_coordinate = x_coordinate1;
y_coordinate = y_coordinate1;
}
void CRect :: Draw()
{
cout<<"颜色为:"<<color<<endl;
cout<<"长度为:"<<length<<endl;
cout<<"宽度为:"<<width<<endl;
cout<<"坐标为:"<<x_coordinate<<" "<<y_coordinate<<endl;
}
为什么私有成员不能定义为 char *color这样程序就自己停止运行了 而char color{10]这样就可以呢