2009
03-20

#include<iostream>
#include<string.h>
using namespace std;
class Dog{
private:
 int age;
 float weight;
 char name[20];
public:
 void SetDog(int Nage=0,float Nweight,char Nname[20]=”Dog”);
 void ShowDog();
};
void Dog::SetDog(int Nage,float Nweight,char Nname[20])
{
 age=Nage;
 weight=Nweight;
 strcpy(name,Nname);


}
void Dog::ShowDog()
{
 cout<<”the name of dog is “<<name[20]<<endl
  <<”the age of dog is “<<age<<endl
  <<”the weight of dog is “<<weight<<endl;
}
int main()
{
 Dog mydog;
 cout<<”please enter your dog’s age and weight”;
 mydog.SetDog();
 mydog.ShowDog();
 return 0;
}


高手请进》有 3 条评论

  1. Freeze 说:

    帮忙看看 编译说有两个错误

  2. wxjeacen 说:

    #include<iostream>
    #include<cstring>
    using namespace std;
    class Dog{
    private:
        int age;
        float weight;
        char name[20];
    public:
        void SetDog(int Nage,float Nweight,char Nname[20]);
        void ShowDog();
    };
    void Dog::SetDog(int Nage,float Nweight,char Nname[20])
    {
        age=Nage;
        weight=Nweight;
        strcpy(name,Nname);

    }
    void Dog::ShowDog()
    {
        cout<<”the name of dog is “<<name<<endl
        <<”the age of dog is “<<age<<endl
        <<”the weight of dog is “<<weight<<endl;
    }
    int main()
    {
        Dog mydog;
        cout<<”please enter your dog’s age and weight”;
        mydog.SetDog(0,19.2,”Fiona”);
        mydog.ShowDog();
        return 0;
    }

    我帮你修改过了。
    可以运行了。

  3. Weberte 说:

    #include<iostream>
    #include<string.h>
    using namespace std;
    class Dog{
    private:
     int age;
     float weight;
     char *name;
    public:
     void SetDog(int Nage=0,float Nweight=0,char *Nname=”Dog”);
     void ShowDog();
    };
    void Dog::SetDog(int Nage,float Nweight,char *Nname)
    {
     age=Nage;
     weight=Nweight;
     name=Nname;

    }
    void Dog::ShowDog()
    {
     cout<<”the name of dog is “<<name<<endl
      <<”the age of dog is “<<age<<endl
      <<”the weight of dog is “<<weight<<endl;
    }
    int main()
    {
     Dog mydog;
     int age;
     float weight;
     cout<<”please enter your dog’s age and weight”<<endl;
     cin>>age;
     cin>>weight;
     mydog.SetDog(age,weight,”mydog”);
     mydog.ShowDog();
     return 0;
    }
    这样似乎更好

留下一个回复