02_记录学生相关数据,输出平均分数

题目
编写一个程序,已有若干个学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。
【知识点】:2.2  2.3
【参考分】:20分
【难易度】:B

题目理解:

2017年8月21日
  • 已有若干个学生数据”,在构造函数中已经初始化啦?==》不对,还是要实例化,输入数据
  • 输出这些学生数据计算平均分”==》并



实现案例:

编写过程的思考:

2017年8月21日
  • 若干?不定参数如何传入参数呢,在求平均成绩的时候,输入类学生
  • vector使用方法?

ToDo:

  • 1

#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
	Student(int id,string name,double score);
	~Student();

	static void list_student(Student stu1, Student stu2);



private:
	static void get_avage_score(Student stu1, Student stu2);
	int _id;
	string _name;
	double _score;


};

Student::Student(int id, string name, double score) {
	_id = id;
	_name = name;
	_score = score;
}

Student::~Student() {
}
void Student::list_student(Student stu1, Student stu2) {
	cout << stu1._id << "stu1._name" << stu1._score << endl;
	cout << stu2._id << "stu2._name" << stu2._score << endl;
	Student::get_avage_score(stu1, stu2);
}
void Student::get_avage_score(Student stu1, Student stu2) {
	cout << (stu1._score + stu2._score) / 2 << endl;
}

int main1() {
	Student stu1(23456, "Zhangsan", 56);
	Student stu2(23449, "Zhangsan2", 76);

	Student::list_student(stu1, stu2);

	getchar();
	return 0;
}


参考案例:

编程技巧感悟:

2017年8月21日
  • 构造函数,不止是初始化啊!
    • 本例中,在构造函数中,同时对sum(总分数)进行累加求和,num(学生数量)进行自加。每次调用构造函数的时候,该变量就会改变。(sum、num为成员静态变量)

知识点点:

  • char类型与String类型相同与区别,用法技巧,平时定义字符串类型的变量如何定义?
    • 字符串如何打印出来?
    • 字符串类型的实参与形参如何赋值? 
      • 除了strcpy(),还有其它的相关函数吗?
  • static类型变量的用法,有什么特点?
  • vector使用方法?

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class Stud
{
public:
	Stud(int id,char name[] , int score);
	~Stud();
	void disp();
	static double get_score();

private:
	int _id;
	char _name[10];
	int _score;

	static int sum;
	static int num;

};

int Stud::sum = 0;
int Stud::num = 0;

void Stud::disp() {
	cout << setw(5) << _id << setw(8) << _name << setw(6) << _score << endl;
}
double Stud::get_score() {
	return sum / num;

}
Stud::Stud(int id, char name[], int score) {

	_id = id;
	//_name[10] = name; 
	strcpy(_name, name); //字符串究竟如何处理?
	_score = score;

	// 构造函数,不止是初始化!
	sum += score;
	num++;
}

Stud::~Stud() {
}

int main() {
	Stud stu1(456, "zhangsesad", 78), stu2(456666, "dasfssd", 89);
	stu1.disp();

	cout << Stud::get_score() << endl;

	getchar();
	return 0;
}

是否发布到博客2017年8月21日 15:11.




posted @ 2017-08-21 14:45  王_冲  阅读(403)  评论(0编辑  收藏  举报