| Student クラス |
|
ID 名前 |
|
ID と名前をセット ID 取得 名前 取得 IDと名前をコンソール表示 |
#include <string> using namespace std; |
int ID; string name; public: Student(); // 引数なしのコンストラクタ Student(int i, string n); // 引数つきのコンストラクタ ~Student(); void setStudent(int i, string n); int getID(); string getName(); bool operator==(const Student& s) const; // 「等しい」ことの定義 void printInfo(); |
#include <iostream>
// 引数なしのコンストラクタ
Student::Student()
{
ID = 0;
name = "";
}
// 引数ありのコンストラクタ
Student::Student(int i, string n)
{
ID = i;
name = n;
}
// デストラクタ
Student::~Student()
{
}
void Student::setStudent(int i, string n){
ID = i;
name = n;
}
int Student::getID(){
return ID;
}
string Student::getName(){
return name;
}
bool Student::operator==(const Student &s) const{
return (ID == s.ID && name == s.name);
}
void Student::printInfo(){
std::cout << ID << "番の学生は" << name << "\n";
}
|
#include "Student.h" |
Student s1; // 引数なしコンストラクタが呼ばれる s1.setStudent(100,"織田信長"); s1.printInfo(); |