主题:关于类的一个小问题
#include<iostream.h>
#include<string.h>
class Animal
{
private:
char name[40];
float weight;
int age;
public:
void SetName(char *nm) { strcpy(name, nm); }
void SetWeight(float wt) { weight = wt; }
void SetAge(int ag) { age = ag; }
void GetName(char *nm) const { strcpy(nm, name); }
float GetWeight() const { return weight; }
int GetAge() const { return age; }
void Move();
};
void Animal:: Move()
{
cout << "Animal moving!\n";
}
void main()
{
Animal dog;
char *p = "peter";
dog.SetName(p);
dog.GetName(p);//这一句有什么问题吗?为什么运行有问题
cout << "The dog's name is:" << p << endl;
dog.Move();
}[code=c]
请填写代码
[/code]
#include<string.h>
class Animal
{
private:
char name[40];
float weight;
int age;
public:
void SetName(char *nm) { strcpy(name, nm); }
void SetWeight(float wt) { weight = wt; }
void SetAge(int ag) { age = ag; }
void GetName(char *nm) const { strcpy(nm, name); }
float GetWeight() const { return weight; }
int GetAge() const { return age; }
void Move();
};
void Animal:: Move()
{
cout << "Animal moving!\n";
}
void main()
{
Animal dog;
char *p = "peter";
dog.SetName(p);
dog.GetName(p);//这一句有什么问题吗?为什么运行有问题
cout << "The dog's name is:" << p << endl;
dog.Move();
}[code=c]
请填写代码
[/code]