|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <iostream>
- #include <string.h>
- using namespace std;
- class Dog
- {
- public:
- Dog();//构造函数
- Dog(char Nname[20],int Nage,char Nsex,float Nweight,char Ncolor[10]);//有参的构造函数
- Dog(Dog &p);//拷贝函数
- void SetName();
- void SetSex();
- void SetColor();
- void SetWeight();
- void Talk();
- ~Dog() {} ;//析构函数
- private:
- char name[20];
- int age;
- char sex;
- float weight;
- char color[10];
- };
- Dog::Dog()
- {
- }
- Dog::Dog(char *Nname,int Nage,char Nsex,float Nweight,char *Ncolor)
- {
- strcpy(name,Nname);//在C++中,给一个数组赋值字符串的时候,只能用strcpy;不能用这种形式:name=Nname
- //&name=Nname; //因为数组首地址不可以改变. 还有&name=Nname,&只是"取"这个数组的首地址.
- age=Nage;
- sex=Nsex;
- weight=Nweight;
- strcpy(name,Ncolor);
- //color=Ncolor;
- }
- void Dog::SetName()
- {
- gets(name);
- }
- void Dog::SetSex()
- {
- cin>>sex;
- }
- void Dog::SetColor()
- {
- gets(color);
- }
- void Dog::SetWeight()
- {
- cin>>weight;
- }
- void Dog::Talk()
- {
- cout<<"你好,主人.我的基本信息是:";
- cout<<"\t狗名:"<<name<<"\t性别:"<<sex<<"\t体重:"<<weight<<"\t颜色:"<<color<<endl;
- }
- main()
- {
- Dog d1,d2,d3;
- d1=Dog("xiao1" , 2 , 'm' , 5 , "red" );
- d2=Dog("xiao2" , 1 , 'f' , 2 , "yellow");
- d3=Dog("xiao3" , 3 , 'm' , 10 , "blue");
- cout<<"这是一个可以模拟创建小狗狗的程序:\n"<<endl;
- cout<<"以下为系统已经给出的3种可爱小狗狗:\n"<<endl;
- d1.Talk();
- d2.Talk();
- d3.Talk();
- return 0;
- }
复制代码
这是代码,但是在调用Talk的时候,狗的姓名输不出来,不知道为什么。
|
|