C++ 实验二 类和对象

实验二 类和对象

一、目的要求
1、理解面向对象的程序设计的特点;
2、学会类和对象的定义以及对象成员的引用;
3、理解类的封装性和信息隐藏,学会类声明和成员函数定义的分离。
二、实验内容与步骤
1.设计一个类,成员变量包括学号、姓名、性别、年龄、成绩;
2.完成以下:
(1)由键盘分别输入n个学生的学号、姓名、性别、年龄、成绩;
(2)计算每个学生的平均成绩;
(3)判断每个学生是否有不及格的课程;
(4)并输出所有学生的信息和平均成绩;
三、上机要求
1.类声明放在头文件(.h)中;
2.成员函数定义放在源文件(.cpp)中;
3.主函数放在源文件(file.cpp)中。
附代码
!!!编译环境 DevC++ 5.11 TDM-GCC 4.9.2 64-Bit Release
头文件 iquery.h

#ifndef  _IQUERY_H
#define _IQUERY_H 1
    using std::string;
	#define MAXN 100 

	class student{
		private:
			int old;
			string sex;            
			string name;
			string number; 
			double chinese;
			double math;
			double english;
			double daniel;
			double ave_grade;
	public:	
		void data_input();
//		double data_grade_calc();   
		bool data_grade_judge();    //judge 每个学生成绩是否及格 
		void data_output();
		
	};
#endif

头文件实现文件 iquery,cpp

#include 
#include "string"
#include "iquery.h"
#include "iomanip"
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void student :: data_input(){
		cout<<"    姓名:" ;cin>>name;
		cout<<"    年龄:" ;cin>>old; 
		cout<<"    性别:" ;cin>>sex;
		cout<<"    学号:" ;cin>>number;
		cout<<"接下来依次输入语文,数学,英语,理综成绩"<>chinese>>math>>english>>daniel;
		ave_grade=(math+chinese+english)/3;
}

//double student :: data_grade_calc(){           //计算主科平均成绩 
//	return (math+chinese+english)/3;
//}
bool student :: data_grade_judge(){
	while(chinese<90||math<90||english<90){
		return 1;
	}
	return 0;	
}
void student :: data_output(){
	cout<

简陋的源码 main.cpp

#include 
#include "iquery.h"
#include "iomanip"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
	int ele=0;
	char sel;
	int pos;
	student stu[MAXN];
	int i=0;
	int n=0; 
	int choice;
	 
	cout<<"请输入学生个数:"<>n;
	do{
		cout<<"请输入第"<

欢迎访问陈风的个人博客

你可能感兴趣的:(coding,life)