C++类与对象程序问题

来源:百度知道 编辑:UC知道 时间:2024/06/07 21:16:25
我老师要我们做一个C++类与对象的作业,题目是要输出学生的基本信息,请各位高手看看我这个程序哪里错了。先谢谢了!!!最好就是帮我修改一下。
#include <iostream.h>
#include <string.h>
class student
{
private:
char name[10];
int no;
int age;
char pro[10];
char sex[4];
public:
void name (char *n);
void no (int n);
void age (int a);
void pro (char *p);
void sex (char *s);
void printf();
};
void student::name (char *n)
{
strcpy(name,n);
}
void student::no(int n)
{
no=n;
}
void student::age(int a)
{
age=a;
}
void student::pro(char *p)
{
strcpy(pro,p);
}
void student::sex(char *s)
{
strcpy(sex,s);
}
void student::printf()
{
cout<<"姓名:"<<name<<endl;
cout<<"学号:"<<no<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"专业:"<<pro

你为什么不用string来定义字符数组呢,这样多好用啊,还有你的错误就是你的那几个成员函数名字和你的private变量都是一样的,这样有冲突,所以要变成别的名字,这样就对了。。。
下边的是参考:
#include <iostream>
#include <string>
using namespace std;
class student
{
private:
string name;
int no;
int age;
string pro;
string sex;
public:
void setname(string n);
void setno (int n);
void setage (int a);
void setpro (string p);
void setsex (string s);
void printf();
};
void student::setname (string n)
{
name = n;
}
void student::setno(int n)
{
no = n;
}
void student::setage(int a)
{
age = a;
}
void student::setpro(string p)
{
pro = p;
}
void student::setsex(string s)
{
sex = s;
}
void student::printf()
{
cout << "姓名:" << name << endl;
cout << "学号:" << no <&