c++類的簡單例項
阿新 • • 發佈:2019-01-11
輸入學生的學號,以及三門課程成績,輸出平均成績,並輸出是否通過(假如任意一門成績小於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;
}