# include <iostream.h>
class Cstudent
{
private:
int age;
char name[10];
public:
Cstudent()
{
age=0;
name="";
}
void setvalue(int a,char n)
{
age=a;
name=n;
}
void display()
{
cout<<"name="<<name<<"  age="<<age<<endl;
}
};
void main()
{
Cstudent john;
int age;
char name[10];
age=20;
name="john12345";
john.setvalue(age,name);
john.display();
}

解决方案 »

  1.   

    // Cpp.cpp : Defines the entry point for the console application.
    //
    #include <string>
    #include <iostream>using namespace std;class Cstudent
    {
    private:
    int age;
    string name;
    public:
    Cstudent()
    {
    age=0;
    name="";
    }
    void setvalue(int a,string n)
    {
    age=a;
    name=n;
    }
    void display()
    {
    cout<<"name="<<name<<"  age="<<age<<endl;
    }
    };
    void main()
    {
    Cstudent john;
    int age;
    string name;
    age=20;
    name="john12345";
    john.setvalue(age,name);
    john.display();
    }--------------------
    我是这么写的~结果:
    name=john12345  age=20
      

  2.   

    # include <iostream.h>
    class Cstudent
    {
    private:
    int age;
    char name[10];
    public:
    Cstudent()
    {
    age=0;
    name="";//不对,name已经有初值
    }
    void setvalue(int a,char n)
    {
    age=a;
    name=n;//不对
    }
    void display()
    {
    cout<<"name="<<name<<"  age="<<age<<endl;
    }
    };
    void main()
    {
    Cstudent john;
    int age;
    char name[10];
    age=20;
    name="john12345";//不对
    john.setvalue(age,name);
    john.display();
    }
      

  3.   

    改了一下
    # include <iostream.h>
    #include  <string.h>
    class Cstudent
    {
    private:
    int age;
    char name[10];
    public:
    Cstudent()
    {
    age=0;
    strcpy(name,"");
    }
    void setvalue(int a,char *n)
    {
    age=a;
    strcpy(name,n);
    }
    void display()
    {
    cout<<"name="<<name<<"  age="<<age<<endl;
    }
    };
    void main()
    {
    Cstudent john;
    int age;
    char name[10];
    age=20;
    strcpy(name,"john12345");
    john.setvalue(age,name);
    john.display();
    }
      

  4.   

    : rivershan(笨猫)(C++/VC初学者) 
    对初学者你就来stl!
      

  5.   

    谢谢,两个程序都通过了。
    to: rivershan(笨猫)(C++/VC初学者) 
              string name 的写法我没见过,能讲讲吗?to: puppet(小虎) 
              name="";//不对,name已经有初值   能解释一下吗?谢谢
      

  6.   

    哈~
    我也算是初学者~C++~
    我看的是Essential~
    你看看就明白了~
    我讨厌C~
    :(