引数に応じて動作を変えられる。コンストラクタの定義はこの部分
Student() {
}
Student(std::string name, int jpn, int math, int eng) {
m_name = name;
m_math = math;
m_jpn = jpn;
m_eng = eng;
}
実行結果
Tanaka
20
30
75
ソースコード
#include<iostream>
class Student {
public:
Student() {
}
Student(std::string name, int jpn, int math, int eng) {
m_name = name;
m_math = math;
m_jpn = jpn;
m_eng = eng;
}
~Student() {
}
void setName(std::string name) {
m_name = name;
}
void setMath(int math) {
m_math = math;
}
void setJpn(int jpn) {
m_jpn = jpn;
}
void setEng(int eng) {
m_eng = eng;
}
void show(void) {
std::cout << m_name << std::endl;
std::cout << m_jpn << std::endl;
std::cout << m_math << std::endl;
std::cout << m_eng << std::endl;
}
private:
std::string m_name;
int m_math = 0;
int m_eng = 0;
int m_jpn = 0;
};
int main() {
Student student1;
student1.setName("Tanaka");
student1.setJpn(20);
student1.setMath(30);
student1.setEng(75);
student1.show();
std::cout << std::endl;
return 0;
}
コメント