c++クラスの簡単な例

5096 ワード

学生の学号を入力し、3つの課程の成績を入力し、平均成績を出力し、合格するかどうかを出力します(いずれかの成績が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 <

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 <

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;
}