c++类的简单实例

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


1、

#include <iostream>  
#include <string>  
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 <iostream>    
#include <string>    
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 <iostream>  
#include <string>  
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 <iostream>    
#include <string>    
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 <iostream>   
#include <string>    
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;
}



  • 11
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值