编程c++。。
发布网友
发布时间:2022-05-27 07:12
我来回答
共4个回答
热心网友
时间:2023-10-11 07:18
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
Person(){};
~Person(){};
private :
string name;
char sex;//注意性别是字符型,自己可以改写成字符串类型
int age;
public :
void setMessage(string name,char sex,int age)
{
this->name = name;
this->sex = sex;
this->age = age;
}
};
class Student : public Person {
private :
string corse[3];//注意:只有三门课程
double score[3];
public :
Student(){};
~Student() {};
void setcorse(string scname[],int j);
void setScore(double sc[],int i);
string getcorse(int i);
double getScore(int i);
double getAvgScore();
};
void Student::setcorse(string scname[],int j)
{
for(int a=0; a!=j; a++)
{
this->corse[a] = scname[a];
}
}
void Student::setScore(double sc[],int i)
{
for(int b=0; b!=i; b++)
{
this->score[b] = sc[b];
}
}
string Student::getcorse(int i)
{
return corse[i];
}
double Student::getScore(int i)
{
return score[i];
}
double Student::getAvgScore()
{
double avgsc = 0;
for(int i=0;i<3;i++)
{
avgsc += score[i];
}
avgsc /=3;
return avgsc;
}
void main()
{
string name;
int age;
double score[3];
char sex;
string corse[3];
Student stu[3];//注意:只有三个学生
for(int i=0;i!=3;i++)
{
cout<<"姓名:";
cin>>name;
cout<<endl<<"性别:";
cin>>sex;
cout<<endl<<"年龄:";
cin>>age;
stu[i].setMessage(name,sex,age);
for(int k=0;k<3;k++)
{
string str;
cout<<endl<<"课程名:";
cin>>str;
corse[k] = str;
cout<<endl<<"成绩:";
cin>>score[k];
}
stu[i].setcorse(corse,3);
stu[i].setScore(score,3);
}
for(int m=0;m!=3;m++)
{
for(int l=0;l<3;l++)
{
cout<<"课程名:"<<stu[m].getcorse(l);
cout<<endl<<"成绩:"<<stu[m].getScore(l);
}
cout<<endl<<"平均成绩:"<<stu[m].getAvgScore()<<endl;
}
}
注意:没有写班级,如果要写的话,自己想办法添加
热心网友
时间:2023-10-11 07:19
//如果数据要求是私有成员的话,就这样:
#include <iostream>
#include <string>
using namespace std;
class Person
{
string name;
bool sex;
int age;
public:
void setname(string temp);
void setsex(bool temp);
void setage(int temp);
string getname();
bool getsex();
int getage();
};
void Person::setname(string temp)
{
name=temp;
}
void Person::setsex(bool temp)
{
sex=temp;
}
void Person::setage(int temp)
{
age=temp;
}
string Person::getname()
{
return name;
}
int Person::getage()
{
return age;
}
bool Person::getsex()
{
return sex;
}
class Student:public Person
{
double grade[3];
public:
void setgrade(double x1,double x2,double x3);
double ave_grade();
};
void Student::setgrade(double x1,double x2,double x3)
{
grade[0]=x1;
grade[1]=x2;
grade[2]=x3;
}
double Student::ave_grade()
{
return (grade[0]+grade[1]+grade[2])/3;
}
void main()
{
Student stu;
string tempname;
bool sex;
int age;
double grade[3];
cout<<"please input name:"<<endl;
cin>>tempname;
cout<<"please input sex(if boy,please input 1;if girl ,please input 0):"<<endl;
cin>>sex;
cout<<"please input age:"<<endl;
cin>>age;
cout<<"please input grades:"<<endl;
cin>>grade[0]>>grade[1]>>grade[2];
stu.setage(age);
stu.setgrade(grade[0],grade[1],grade[2]);
stu.setname(tempname);
stu.setsex(sex);
cout<<"you name is:"<<stu.getname()<<endl;
cout<<"you age is:"<<stu.getage()<<endl;
cout<<"you sex is(1 for boy,0 for girl):"<<stu.getsex()<<endl;
cout<<"you ave_grades is:"<<stu.ave_grade()<<endl;
}
热心网友
时间:2023-10-11 07:19
#include "stdafx.h"
#include "iostream.h"
class person
{
public:
char* name;
int age;
person()
{
name=new char[20];
age=0;
}
};
class student:public person
{
public:
int num;
};
int main(int argc, char* argv[])
{
student stu[10];
char count='y';
int n=0;
while(count=='y'||count=='Y')
{
cout<<"enter(num name age):"<<endl;
cin>>stu[n].num>>stu[n].name>>stu[n].age;
n++;
cout<<"more?"<<endl;
cin>>count;
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(stu[j].num>stu[j+1].num)
{
student st=stu[j+1];
stu[j+1]=stu[j];
stu[j]=st;
}
}
}
for(i=0;i<n;i++)
{
cout<<stu[i].num<<stu[i].name<<stu[i].age<<endl;
}
return 0;
}
热心网友
时间:2023-10-11 07:20
简单 但是麻烦 建议你自己谢谢