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;
}
相關文章
- C++學習隨筆——簡單的單例設計模式例項C++單例設計模式
- websocket簡單例項Web單例
- C++ 結構體例項和類例項的初始化C++結構體
- jQuery實現的簡單投票簡單程式碼例項jQuery
- opengl簡單入門例項
- javascript事件冒泡簡單例項JavaScript事件單例
- Spark 簡單例項(基本操作)Spark單例
- javascript this用法和簡單例項JavaScript單例
- Java的Socket通訊簡單例項Java單例
- jQuery實現的動畫簡單例項jQuery動畫單例
- 【C++】實現一個簡單的單例模式C++單例模式
- 多執行緒併發鎖分類以及簡單例項執行緒單例
- js選項卡簡單程式碼例項JS
- js簡單的留言功能程式碼例項JS
- 004--最簡單的SpringMVC例項SpringMVC
- php對mysql簡單讀取的例項PHPMySql
- MAC下使用OpenCV的例項簡單DemoMacOpenCV
- EventBus詳解及簡單例項單例
- $.ajax()函式用法簡單例項函式單例
- 策略模式與簡單java例項模式Java
- corba程式設計簡單例項ORB程式設計單例
- Angularjs製作簡單的路由功能簡單程式碼例項AngularJS路由
- C++ 單例類别範本(詳解)C++單例
- C++ 巢狀類簡單測試C++巢狀
- 簡單的單資料來源複製例項——流
- jQuery表單驗證簡單程式碼例項jQuery
- ruby中的類例項變數和例項的例項變數變數
- JavaScript簡單的日曆效果程式碼例項JavaScript
- javascript物件導向繼承的簡單例項JavaScript物件繼承單例
- ElasticSearch客戶端簡單操作例項Elasticsearch客戶端
- js map集合簡單程式碼例項JS
- javascript閉包簡單程式碼例項JavaScript
- javascript作用域簡單例項程式碼JavaScript單例
- mysql 儲存過程簡單例項MySql儲存過程單例
- spring4簡單例項(1)Spring單例
- JSON簡單格式程式碼例項JSON
- JavaScript 動畫效果簡單例項程式碼JavaScript動畫單例
- mysql儲存過程簡單例項MySql儲存過程單例