c++编写定义学生类student,求大神帮忙看看,看可以怎么改改,或者说一下出错的原因
1.定义一个学生类student,类有5个数据成员(学号、姓名、年龄、C++及高数两科成绩),以及如下成员函数。请编写主函数使用这个类,实现对学生数据的赋值和输出,要求:...
1.定义一个学生类student,类有5个数据成员(学号、姓名、年龄、C++及高数两科成绩),以及如下成员函数。请编写主函数使用这个类,实现对学生数据的赋值和输出,要求:(1)设计两个成员函数分别实现对数据的输入、输出;(2)设计一个构造函数实现初始化或传递相关数据;(3)设计一个析构函数,能显示相应信息;(4)设计一个成员函数计算总成绩并输出。下面是我写的#include <iostream.h>class student{public: student(); student(int no,int a,int s1,int s2,char Name[21]); ~student() {cout<<"调用析构函数.\n";} void sum(); void shuchu(); void shuru();private: int number,age,c,math; char name[10];};student::student(){number=0;age=0;c=0;math=0;name[21]='\0';} student::student(int no,int a,int s1,int s2,char Name[21]) {number=no;age=a;c=s1;math=s2; void student::shuru(){ cout<<"please input the number"<<number<<endl; cin>>number;cout<<"please input the number"<<age<<endl;cin>>age;cout<<"please input the number"<<c++<<endl;cin>>c++;cout<<"please input the number"<<math<<endl;cin>>math;cout<<"please input the number"<<name<<endl;cin>>name;}void student::shuchu(){cout<<"学号:"<<number<<'n/'<<"年龄"<<age<<'n/'<<"c++"<<c++<<'n/'<<"高数"<<math<<'n/'<<"名字"<<name<<endl;}void student::sum(){ cout<<"总成绩是:"<<(c)+(math)<<endl;}void main(){student c1(1,20,90,95,"May" ); student c2 student.shuru(); student.shuchu(); student.sum();}
错误:error C2601: 'shuru' : local function definitions are illegal error C2601: 'shuchu' : local function definitions are illegl
error C2601: 'sum' : local function definitions are illegal error C2601: 'main' : local function definitions are illegal fatal error C1004: unexpected end of file found 展开
错误:error C2601: 'shuru' : local function definitions are illegal error C2601: 'shuchu' : local function definitions are illegl
error C2601: 'sum' : local function definitions are illegal error C2601: 'main' : local function definitions are illegal fatal error C1004: unexpected end of file found 展开
1个回答
展开全部
你的代码贴的太乱以后不要这么贴,错误很多。
第一、字符串操作。第二、你的成员函数shuru没什么作用,构造函数里已经对数据赋值了。第三、要用对象去调用成员函数,用类调用非静态函数是不对的。你按着下面的改一下就可以了。
#include <iostream.h>
#include <string.h>
class student{
public:
student();
student(int no,int a,int s1,int s2,char Name[21]);
~student()
{
cout<<"调用析构函数.\n"<<endl;
}
void sum();
void shuchu();
void shuru();
private:
int number,age,c,math;
char name[10];
};
student::student()
{
number=0;age=0;c=0;math=0;strcpy(name,"0");
}
student::student(int no,int a,int s1,int s2,char Name[21])
{
number=no;age=a;c=s1;math=s2; strcpy(name,Name);
}
void student::shuru()
{
cout<<"please input the number"<<number<<endl;
cin>>number;
cout<<"please input the number"<<age<<endl;
cin>>age;
cout<<"please input the number"<<c++<<endl;
cin>>c;
cout<<"please input the number"<<math<<endl;
cin>>math;
cout<<"please input the number"<<name<<endl;
cin>>name;
}
void student::shuchu()
{
cout<<"学号:"<<number<<"\n"<<"年龄"<<age<<"\n"<<"c++"<<c++<<"\n"<<"高数"<<math<<"\n"<<"名字"<<name<<endl;
}
void student::sum()
{
cout<<"总成绩是:"<<(c)+(math)<<endl;
}
void main()
{
student c1(1,20,90,95,"May" );
student c2;
//c1.shuru();
c1.shuchu();
c1.sum();
}
第一、字符串操作。第二、你的成员函数shuru没什么作用,构造函数里已经对数据赋值了。第三、要用对象去调用成员函数,用类调用非静态函数是不对的。你按着下面的改一下就可以了。
#include <iostream.h>
#include <string.h>
class student{
public:
student();
student(int no,int a,int s1,int s2,char Name[21]);
~student()
{
cout<<"调用析构函数.\n"<<endl;
}
void sum();
void shuchu();
void shuru();
private:
int number,age,c,math;
char name[10];
};
student::student()
{
number=0;age=0;c=0;math=0;strcpy(name,"0");
}
student::student(int no,int a,int s1,int s2,char Name[21])
{
number=no;age=a;c=s1;math=s2; strcpy(name,Name);
}
void student::shuru()
{
cout<<"please input the number"<<number<<endl;
cin>>number;
cout<<"please input the number"<<age<<endl;
cin>>age;
cout<<"please input the number"<<c++<<endl;
cin>>c;
cout<<"please input the number"<<math<<endl;
cin>>math;
cout<<"please input the number"<<name<<endl;
cin>>name;
}
void student::shuchu()
{
cout<<"学号:"<<number<<"\n"<<"年龄"<<age<<"\n"<<"c++"<<c++<<"\n"<<"高数"<<math<<"\n"<<"名字"<<name<<endl;
}
void student::sum()
{
cout<<"总成绩是:"<<(c)+(math)<<endl;
}
void main()
{
student c1(1,20,90,95,"May" );
student c2;
//c1.shuru();
c1.shuchu();
c1.sum();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询