C++类中要怎样调用字符串?为什么输出是这样的结果?

/*定义一个Dog类,包含age,weight等属性,以及对这些属性操作的方法
实现并测试这个类*/

#include <iostream>
#include <string.h>
#define N 10 
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

class Dog{
    public:
        void Age(int a);
        void Weight(int b);
        void Haircolor(char s[N]);
    private:
        int x,y;
        char c[N];
}; 
void Dog::Age(int a){
    x=a;
    cout<<"age="<<x<<endl;
}
void Dog::Weight(int b){
    y=b;
    cout<<"weight="<<y<<endl;
}
void Dog::Haircolor(char s[N]){
    int i=0,n=0;
    strcat(c,s);
    n=strlen(c);
    cout<<"Haircolor=";
    while((i++)<n){
        cout<<c[i];
    }
}
int main(int argc, char** argv) {
    Dog mydog;       //定义对象mydog
    int x=0,y=0;
    char c[N];
    cin>>x>>y;
    cin>>c[N];
    mydog.Age(x);
    mydog.Weight(y);
    mydog.Haircolor(c);
    return 0;
}
```![图片说明](https://img-ask.csdn.net/upload/202003/20/1584669498_184479.png)
#include <iostream>
#include <string.h>
#define N 10 
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

class Dog{
    public:
        void Age(int a);
        void Weight(int b);
        void Haircolor(char s[N]);
    private:
        int x,y;
        char c[N];
}; 
void Dog::Age(int a){
    x=a;
    cout<<"age="<<x<<endl;
}
void Dog::Weight(int b){
    y=b;
    cout<<"weight="<<y<<endl;
}
void Dog::Haircolor(char s[N]){
    int i=0,n=0;
    c[0] = 0;
    strcat(c,s);
    n=strlen(c);
    cout<<"Haircolor=";
    while(i<n){
        cout<<(char)c[i++];
    }
}
int main(int argc, char** argv) {
    Dog mydog;       //定义对象mydog
    int x=0,y=0;
    char c[N];
    cin>>x>>y;
    cin>>c;
    mydog.Age(x);
    mydog.Weight(y);
    mydog.Haircolor(c);
    return 0;
}

你的代码有几个问题:
1. cin>>c[N]; // 字符数组c的长度为10,而这行的意思是给下标为10的位置赋值,出现数组越界,此数组最大下标为9,但是这种情况编译器并不会报错
2. Dog类没有提供构造函数,编译器将会提供简单的默认构造函数,该默认构造函数不会对Dog类的成员进行任何初始化,所以Dog mydog;执行后其非静态变量全部都为随机值,包括x,y,c
3. Haircolor函数只是将参数s的字符串追加到c后面(strcat的作用),而你的代码既没有对dog.c初始化也没有对main函数的c初始化,只是给下标为10的位置赋了个值(前面说了,c没有下标为10的元素),因此在Haircolor里面输出的c为dog.c和传入s连起来的字符串,而两个字符串都为随机值。但是也有可能最后一个字符是你cin输入的字符,因为strcat追加的结束条件是遇到'\0'才会结束