用C++编写程序,详细代码
发布网友
发布时间:2023-05-13 18:45
我来回答
共1个回答
热心网友
时间:2024-12-04 07:33
这是第一题的答案
#include <iostream>
class student
{
friend std::ostream& operator<<(std::ostream& os,student& stu);
public:
student(){}
student(const char *ptr,int num_,double mathscore_,double englishscore_):num(num_),mathscore(mathscore_),englishscore(englishscore_){::strcpy(name,ptr);}
student(const student & obj):num(obj.num),mathscore(obj.mathscore),englishscore(obj.englishscore){::strcpy(name,obj.name);}
public:
void showbase(){std::cout<<"姓名: "<<name<<" 学号: "<<num<<" 数学成绩:"<<mathscore<<" 英语成绩:"<<englishscore<<std::endl;}
void showstatic(){std::cout<<"总人数:"<<count<<std::endl;}
protected:
char name[18];
int num;
double mathscore;
double englishscore;
public: static int count;
};
int student::count;
std::ostream& operator<<(std::ostream& os,student & stu)
{os<<"姓名:"<<stu.name<<" 学号:"<<stu.num<<" 数学成绩:"<<stu.mathscore<<" 英语成绩:"<<stu.englishscore<<std::endl;return os;}
class CollegeStudent:public student
{
public :
CollegeStudent(const char *ptr,int num_,double mathscore_,double englishscore_,const char * mptr,int grad):student(ptr,num_,mathscore_,englishscore),grade(grad){::strcpy(major,mptr);}
public:
void showdata(){std::cout<<"姓名:"<<name<<" 学号:"<<num<<" 数学成绩:"<<mathscore<<" 英语成绩:"<<englishscore<<" 专业:"<<major<<" 年级:"<<grade<<std::endl;}
private:
char major[18];
int grade;
};
int main(int argc,char * argv[])
{
student arr[4];
student::count=4;
char name[18];
int num;
double mathscore;
double englishscore;
for(int i=0;i!=4;++i)
{
std::cout<<"请输入第"<<i+1<<"个学生的信息,依次输入:名字 学号 数学成绩 英语成绩 "<<std::endl;
std::cin>>name>>num>>mathscore>>englishscore;
arr[i]=student(name,num,mathscore,englishscore);
}
std::cout<<"输入完成,您输入的学生信息如下:\n";
for(int i=0;i!=4;++i)
{
std::cout<<arr[i];
}
::system("pause");
return 0;
}追问好厉害。。。。还有呢还有呢。。。要考试了。。。急啊。。谢谢谢谢!!!!
追答#include
class People
{
friend std::ostream & operator>a>>b>>c;
arr[i-1]=People(a,b,c);
}
for(int i=0;i!=4;++i)
{
std::cout<<arr[i];
arr[i].showNum();
}
system("pause");
return 0;
}