c++类的简单实例

输入学生的学号,以及三门课程成绩,输出平均成绩,并输出是否通过(假如任意一门成绩小于60则没通过)


1、

#include   
#include   
using namespace std;
class Student
{
public:
	void setStudent(string num, int chi, int mat, int eng)
	{
		number = num;
		Chinese = chi;
		Math = mat;
		English = eng;
	}
	int avery(Student& s)
	{
		return(s.Chinese + s.English + s.Math) / 3;
	}

	bool pass(Student& s)
	{
		bool f = false;
		if (!(s.Chinese<60 || s.English<60 || s.Math<60))
		{
			f = true;
		}
		return f;
	}
private:
	string number;
	int Chinese, Math, English;
};


int main()
{
	Student student;
	string number; int Chinese; int Math;  int English;

	while (cin >> number >> Chinese >> Math >> English)
	{
		student.setStudent(number, Chinese, Math, English);

		cout <<"该生的平均成绩: "<< student.avery(student) << endl;
		if (student.pass(student))
			cout << "pass" << endl;
		else
			cout << "not pass" << endl;
		
	}
		
	return 0;
}

2、

#include     
#include     
using namespace std;
class Student
{
public:
	
	Student(string number, int Chinese, int Math, int Eng) 
	{
		this->number = number;
		this->Chinese = Chinese;
		this->Math = Math;
		English = Eng;
		
	}

	int avery()
	{
		return(Chinese + English + Math) / 3;
	}

	bool pass()
	{
		bool f = false;
		if (!(Chinese<60 || English<60 || Math<60))
		{
			f = true;
		}
		return f;
	}
private:
	string number;
	int Chinese, Math, English;
};


int main()
{
	string a;
	int  b=0;
	int c=0;
	int d=0;
	while (cin >> a >> b >> c >> d)
	{
		Student student(a, b, c, d);
		cout << "该生的平均成绩: " << student.avery() << endl;
		if (student.pass())
			cout << "pass" << endl;
		else
			cout << "not pass" << endl;
	}

	return 0;
}


3、

#include   
#include   
using namespace std;
class Student
{
public:
	void setStudent(string num, int chi, int mat, int eng);
	int avery(Student& s);
	bool pass(Student& s);

private:
	string number;
	int Chinese, Math, English;
};

void Student::setStudent(string num, int chi, int mat, int eng)
{
	number = num;
	Chinese = chi;
	Math = mat;
	English = eng;
}
int Student::avery(Student& s)
{
	return(s.Chinese + s.English + s.Math) / 3;
}

bool Student::pass(Student& s)
{
	bool f = false;
	if (!(s.Chinese<60 || s.English<60 || s.Math<60))
	{
		f = true;
	}
	return f;
}

int main()
{
	Student student;
	string number; int Chinese; int Math;  int English;

	while (cin >> number >> Chinese >> Math >> English)
	{
		student.setStudent(number, Chinese, Math, English);

		cout <<"该生的平均成绩: "<< student.avery(student) << endl;
		if (student.pass(student))
			cout << "pass" << endl;
		else
			cout << "not pass" << endl;
		
	}
		
	return 0;
}


4、需加入this

#include     
#include     
using namespace std;
class Student
{
public:
	
	Student(string number, int Chinese, int Math, int Eng) 
	{
		this->number = number;
		this->Chinese = Chinese;
		this->Math = Math;
		English = Eng;
		
	}

	void setStudent(string num, int chi, int mat, int eng)
	{
		number = num;
		Chinese = chi;
		Math = mat;
		English = eng;
	}
	int avery()
	{
		return(Chinese + English + Math) / 3;
	}

	bool pass()
	{
		bool f = false;
		if (!(Chinese<60 || English<60 || Math<60))
		{
			f = true;
		}
		return f;
	}
private:
	string number;
	int Chinese, Math, English;
};


int main()
{
	Student student("1023",78,67,89);


		cout << "该生的平均成绩: " << student.avery() << endl;
		if (student.pass())
			cout << "pass" << endl;
		else
			cout << "not pass" << endl;

	return 0;
}


5、无参构造函数与有参构造函数一起调用

#include    
#include     
using namespace std;
class Student
{
public:
	Student(){}
	Student(string num, int chi, int mat, int eng) :number(num), Chinese(chi), Math(mat), English(eng)
	{}
	~Student(){}

	void setStudent(string num, int chi, int mat, int eng);
	int avery();
	bool pass();
private:
	string number;
	int Chinese;
	int Math;
	int English;
};
void Student::setStudent(string num, int chi, int mat, int eng)
{
	number = num;
	Chinese = chi;
	Math = mat;
	English = eng;
}
int Student::avery()
{
	return(Chinese + English + Math) / 3;
}

bool Student::pass()
{
	bool f = false;
	if (!(Chinese<60 || English<60 || Math<60))
	{
		f = true;
	}
	return f;
}

int main()
{
	Student student;
	string number; int Chinese; int Math;  int English;

	while (cin >> number >> Chinese >> Math >> English)
	{
		student.setStudent(number, Chinese, Math, English);

		cout << "该生的平均成绩: " << student.avery() << endl;
		if (student.pass())
			cout << "pass" << endl;
		else
			cout << "not pass" << endl;

	}
	Student stu("1001", 69, 70, 80);
	cout << "该生平均成绩" << stu.avery() << endl;
	if (stu.pass())
		cout << "pass" << endl;
	else
	    cout << "not pass" << endl;
	return 0;
}



你可能感兴趣的:(c++学习)