프로그래밍/C,C++
C++ Object Functions(클래스 함수)
이원중
2020. 3. 17. 23:17
반응형
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
string major;
double gpa;
Student(string aName, string aMajor, double aGpa) {
name = aName;
major = aMajor;
gpa = aGpa;
}
bool hasHonors() {
if (gpa >= 3.5) {
return true;
}
return false;
}//True => have Honor
};
int main() {
Student student1("Jim", "Business", 2.4);
Student student2("Pam", "Art", 3.6);
cout << student1.hasHonors();
cout << student2.hasHonors();
return 0;
}
클래스 함수 부분은 bool hasHonors()~~함수 부분이다.
gpa가 3.5가 넘으면 true값을 반환해주고, 그렇지 못하면 false를 반환해준다.
student1같은 경우에는 gpa가 2.4이기 때문에 false값을 반환하여 0을 출력하고, student2는 gpa가 3.6이기 때문에 true로 1을 출력한다.
이 또한 유용하게 쓰일 것 같다.
반응형