求解C++错误在哪
#include<iostream>#include<string>usingnamespacestd;classstudent{public:student(strin...
#include<iostream>
#include<string>
using namespace std;
class student
{ public:
student(string name1,int xuehao1,int chinese1,int math1,int english1)
{name=name1;
xuehao=xuehao1;
chinese=chinese1;
math=math1;
english=english1;
}
void print()
{cout<<name<<endl;
cout<<xuehao<<endl;
cout<<chinese<<endl;
cout<<math<<endl;
cout<<english<<endl;
}
private:
string name;
int xuehao;
int chinese;
int math;
int english;
};
class us:public student{
public:
us(string name1,int xuehao1,int chinese1,int math1,int english1,string name2,xuehao2,int chinese2,int math2,int english2,string name3,xuehao3,int chinese3,int math3,int english3,string grade1,string type1,string major1):
pupil(string name1,xuehao1,chinese1,math1,english1),
midle(string name2,xuehao2,chinese2,math2,english2),
high(string name3,xuehao3,y3,s3,e3)
{grade=grade1;
type=type1;
major=major1;
}
void print1()
{pupil.print();
cout<<grade<<endl;
}
void print2()
{midle.print;
cout<<type<<endl;
}
void print3()
{high.print();
cout<<major<<endl;
}
private:
string grade;
string type;
string major;
student pupil;
student midle;
student high;
};
int main()
{ us a("宋",11,98,99,100,"戴",30,23,24,34,"小",23,23,34,45,"一年级","理科","网络工程");
a.print1();
a.print2();
a.print3();
return 0;
} 展开
#include<string>
using namespace std;
class student
{ public:
student(string name1,int xuehao1,int chinese1,int math1,int english1)
{name=name1;
xuehao=xuehao1;
chinese=chinese1;
math=math1;
english=english1;
}
void print()
{cout<<name<<endl;
cout<<xuehao<<endl;
cout<<chinese<<endl;
cout<<math<<endl;
cout<<english<<endl;
}
private:
string name;
int xuehao;
int chinese;
int math;
int english;
};
class us:public student{
public:
us(string name1,int xuehao1,int chinese1,int math1,int english1,string name2,xuehao2,int chinese2,int math2,int english2,string name3,xuehao3,int chinese3,int math3,int english3,string grade1,string type1,string major1):
pupil(string name1,xuehao1,chinese1,math1,english1),
midle(string name2,xuehao2,chinese2,math2,english2),
high(string name3,xuehao3,y3,s3,e3)
{grade=grade1;
type=type1;
major=major1;
}
void print1()
{pupil.print();
cout<<grade<<endl;
}
void print2()
{midle.print;
cout<<type<<endl;
}
void print3()
{high.print();
cout<<major<<endl;
}
private:
string grade;
string type;
string major;
student pupil;
student midle;
student high;
};
int main()
{ us a("宋",11,98,99,100,"戴",30,23,24,34,"小",23,23,34,45,"一年级","理科","网络工程");
a.print1();
a.print2();
a.print3();
return 0;
} 展开
1个回答
展开全部
主要是class us的定义出现了错误。从定义来看,us这个类中,包含了3个student:pupil,midle和high,这种情况下,定义us从student继承是不合适的,因为us是包含了3个学生的一个类,它的性质和单个的student是不一样的,不能从student派生。us应该是一个单独的类。修改如下:
class us { //原来这一行是class us:public student{,现在的定义取消了继承
public:
us(string name1,int xuehao1,int chinese1,int math1,int english1,string name2,int xuehao2,int chinese2,int math2,int english2,string name3,int xuehao3,int chinese3,int math3,int english3,string grade1,string type1,string major1):
//上面这一行的两个参数xuehao2和xuehao3在原来的定义中没有定义成int类型,
//现在已经加上int的定义
pupil(string name1,xuehao1,chinese1,math1,english1),
midle(string name2,xuehao2,chinese2,math2,english2),
high(string name3,xuehao3,chinese3,math3,english3)
//上面这一行,原来的写法是:high(string name3,xuehao3,y3,s3,e3),但y3,s3和e3没有定义
//根据意思,已经修改成high(string name3,xuehao3,chinese3,math3,english3)
{grade=grade1;
type=type1;
major=major1;
}
//...其他地方没有改动,省略。。。
}
已经调试运行通过,你可以试试。
class us { //原来这一行是class us:public student{,现在的定义取消了继承
public:
us(string name1,int xuehao1,int chinese1,int math1,int english1,string name2,int xuehao2,int chinese2,int math2,int english2,string name3,int xuehao3,int chinese3,int math3,int english3,string grade1,string type1,string major1):
//上面这一行的两个参数xuehao2和xuehao3在原来的定义中没有定义成int类型,
//现在已经加上int的定义
pupil(string name1,xuehao1,chinese1,math1,english1),
midle(string name2,xuehao2,chinese2,math2,english2),
high(string name3,xuehao3,chinese3,math3,english3)
//上面这一行,原来的写法是:high(string name3,xuehao3,y3,s3,e3),但y3,s3和e3没有定义
//根据意思,已经修改成high(string name3,xuehao3,chinese3,math3,english3)
{grade=grade1;
type=type1;
major=major1;
}
//...其他地方没有改动,省略。。。
}
已经调试运行通过,你可以试试。
追问
我照你的方法试了 还是不行 求解求解 难道是我的机子有问题么(我是照你的方法,直接复制过去的)
追答
是编译错误还是运行错误?可以说的具体一点。我把这边调试的全部代码给你看看吧。
#include
#include
using namespace std;
class student
{ public:
student(string name1,int xuehao1,int chinese1,int math1,int english1)
{name=name1;
xuehao=xuehao1;
chinese=chinese1;
math=math1;
english=english1;
}
void print()
{cout<<name<<endl;
cout<<xuehao<<endl;
cout<<chinese<<endl;
cout<<math<<endl;
cout<<english<<endl;
}
private:
string name;
int xuehao;
int chinese;
int math;
int english;
};
class us{
public:
us(string name1,int xuehao1,int chinese1,int math1,int english1,string name2,int xuehao2,int chinese2,int math2,int english2,string name3,int xuehao3,int chinese3,int math3,int english3,string grade1,string type1,string major1):
pupil( name1,xuehao1,chinese1,math1,english1),
midle( name2,xuehao2,chinese2,math2,english2),
high( name3,xuehao3,chinese3,math3,english3)
{grade=grade1;
type=type1;
major=major1;
}
void print1()
{pupil.print();
cout<<grade<<endl;
}
void print2()
{midle.print();
cout<<type<<endl;
}
void print3()
{high.print();
cout<<major<<endl;
}
private:
string grade;
string type;
string major;
student pupil;
student midle;
student high;
};
int main()
{ us a("宋",11,98,99,100,"戴",30,23,24,34,"小",23,23,34,45,"一年级","理科","网络工程");
a.print1();
a.print2();
a.print3();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询