设计一个学生类CStudent,其结构如下:
私有数据成员Name(学生姓名)、Degree(成绩);
构造函数对数据成员初始化;
设计一个友元函数Display(CStudent&);输出成绩的等级:>=90为优秀;80~89为良好;70~79为中等;60~69为及格;< 60为不及格;
输出结果如下:
姓名 成绩 等级
Mary 78 中等
Jack 93 优秀
Mike 54 不及格
John 88 良好
求用C++编写:设计一个学生类CStudent,其结构如下: 私有数据成员Name(学生姓名)、Degree(成
答案:3 悬赏:0 手机版
解决时间 2021-03-18 03:09
- 提问者网友:绫月
- 2021-03-17 02:11
最佳答案
- 五星知识达人网友:低血压的长颈鹿
- 2021-03-17 02:43
#include <iostream.h>
#include <string.h>
const int SIZE=2;//人数
class CStudent
{
private:
char* name[SIZE];//姓名
int degree[SIZE];//成绩
public:
CStudent(const char *str[SIZE],const int score[SIZE])
{
for (int i=0;i<SIZE;i++)
{
name[i]=new char[strlen(str[i])+1];//分配内存
strcpy(name[i],str[i]);
degree[i]=score[i];
}
};
~CStudent()
{
for (int i=0;i<SIZE;i++)
{
delete [] name[i];//释放内存
}
}
friend void Display(CStudent& stu);
};
void Display(CStudent& stu)
{
cout <<"姓名"<<'\t'<<"成绩"<<'\t'<<"等级"<<endl;//输出
for (int i=0;i<SIZE;i++)
{
cout <<stu.name[i]<<'\t';
int score=stu.degree[i];
if (score>=90)
cout <<score<<'\t'<<"优秀"<<endl;
else if (score>=80&&score<90)
cout <<score<<'\t'<<"良好"<<endl;
else if (score>=70&&score<80)
cout <<score<<'\t'<<"中等"<<endl;
else if (score>=60&&score<70)
cout <<score<<'\t'<<"及格"<<endl;
else
cout <<score<<'\t'<<"不及格"<<endl;
}
}
void main( )
{
const char* name[SIZE]={"Tommy","James"};
const int score[SIZE]={97,50};
CStudent a(name,score);
Display(a);
}
#include <string.h>
const int SIZE=2;//人数
class CStudent
{
private:
char* name[SIZE];//姓名
int degree[SIZE];//成绩
public:
CStudent(const char *str[SIZE],const int score[SIZE])
{
for (int i=0;i<SIZE;i++)
{
name[i]=new char[strlen(str[i])+1];//分配内存
strcpy(name[i],str[i]);
degree[i]=score[i];
}
};
~CStudent()
{
for (int i=0;i<SIZE;i++)
{
delete [] name[i];//释放内存
}
}
friend void Display(CStudent& stu);
};
void Display(CStudent& stu)
{
cout <<"姓名"<<'\t'<<"成绩"<<'\t'<<"等级"<<endl;//输出
for (int i=0;i<SIZE;i++)
{
cout <<stu.name[i]<<'\t';
int score=stu.degree[i];
if (score>=90)
cout <<score<<'\t'<<"优秀"<<endl;
else if (score>=80&&score<90)
cout <<score<<'\t'<<"良好"<<endl;
else if (score>=70&&score<80)
cout <<score<<'\t'<<"中等"<<endl;
else if (score>=60&&score<70)
cout <<score<<'\t'<<"及格"<<endl;
else
cout <<score<<'\t'<<"不及格"<<endl;
}
}
void main( )
{
const char* name[SIZE]={"Tommy","James"};
const int score[SIZE]={97,50};
CStudent a(name,score);
Display(a);
}
全部回答
- 1楼网友:舊物识亽
- 2021-03-17 04:53
#include<iostream>
#include<cstring>
using namespace std;
class CStudent
{
public:
CStudent(char *_name,int _score)
{
name = new char[strlen(_name) + 1];
strcpy(name,_name);
score = _score;
}
~CStudent()
{
delete name;
}
friend void display(CStudent& stu);
private:
char *name;
int score;
};
void display(CStudent& stu)
{
cout << stu.name << ' ' << stu.score << ' ';
if(stu.score>=90)
cout << "优秀";
else if(stu.score >=80)
cout << "良好";
else if(stu.score>=70)
cout << "中等";
else if(stu.score>=60)
cout << "及格";
else
cout << "不及格";
cout << endl;
}
int main()
{
CStudent Stu1("Marry",95);
display(Stu1);
return 0;
}
这个看起来还容易些。
- 2楼网友:渡鹤影
- 2021-03-17 04:05
#include <iostream.h>
#include <string.h>
const int size=2;//人数
class cstudent
{
private:
char* name[size];//姓名
int degree[size];//成绩
public:
cstudent(const char *str[size],const int score[size])
{
for (int i=0;i<size;i++)
{
name[i]=new char[strlen(str[i])+1];//分配内存
strcpy(name[i],str[i]);
degree[i]=score[i];
}
};
~cstudent()
{
for (int i=0;i<size;i++)
{
delete [] name[i];//释放内存
}
}
friend void display(cstudent& stu);
};
void display(cstudent& stu)
{
cout <<"姓名"<<'\t'<<"成绩"<<'\t'<<"等级"<<endl;//输出
for (int i=0;i<size;i++)
{
cout <<stu.name[i]<<'\t';
int score=stu.degree[i];
if (score>=90)
cout <<score<<'\t'<<"优秀"<<endl;
else if (score>=80&&score<90)
cout <<score<<'\t'<<"良好"<<endl;
else if (score>=70&&score<80)
cout <<score<<'\t'<<"中等"<<endl;
else if (score>=60&&score<70)
cout <<score<<'\t'<<"及格"<<endl;
else
cout <<score<<'\t'<<"不及格"<<endl;
}
}
void main( )
{
const char* name[size]={"tommy","james"};
const int score[size]={97,50};
cstudent a(name,score);
display(a);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯