練習問題
問題2
学生を表すクラスの作成
- 生徒を表すクラス cStudentを作成する
- ファイルはcStudent.h cStudent.cpp theMain.cppを作成すること
- cStudentクラスには、privateなメンバ変数として名前、身長、体重を登録できるものとする。
- メンバ変数にはメンバ関数にてアクセスするものとする。(getter,setterを作る)
- メンバ変数には適切な名前を付けること。また、名前はstring型を使うこと。
- コンストラクタは引数なしのものを作成し、内部で名前はからの文字列(““)、そのほかは0で初期化する。
- main関数内にて、セッター関数にて太郎(身長175センチ、体重66キロ)、次郎(身長168センチ、体重72キロ)を登録し、出力せよ。
コンストラクタのオーバーロード
- 生徒を表すクラス cStudentのコンストラクターをオーバーロードして、オブジェクトを作成するときに名前、身長、体重を引数でセットできるようにしなさい。
- 呼び出し例:cStudent stu1(“yamada taro”, 165, 65);
メンバ関数の追加
- 身長と体重からBMI(Body mass index)を計算して返す関数を作成しなさい(戻り値、名前、引数すべて適切なものを考える)
- 身長が$h \hspace{0.3em} \mathrm{m}$で体重が$w \hspace{0.3em}\mathrm{kg}$の人のBMIは$ BMI = w / h^2$で計算される。
表示関数の追加
- cStudentクラスのオブジェクトの表す学生のプロフィールを表示するメンバ関数showMe()を作りなさい。(戻り値と引数は、どうすればよいか考える)
showMe()呼び出し例: 名前:yamada taro 身長:165cm 体重:65kg の場合 ==== Profile ==== 名前:yamada taro 身長:165 cm 体重:65 kg BMI:23.8 =================
ここまでのソースコード
cStudent.h
#pragma once #include <iostream> #include <string> using std::string; class cStudent { private: //プライベートメンバとして、 //名前、身長、体重がある string m_name;//名前:string型 float m_height;//身長:小数点数 float float m_weight;//体重:小数点数 float public: cStudent(); cStudent(string _name, float _height, float _weight); string getMyName(); float getMyHeight(); float getMyWeight(); void setMyName(string _name); void setMyHeight(float _height); void setMyWeight(float _weight); float calcBMI(); void printMe(); };
cStudent.cpp
#include "cStudent.h" using std::cout; using std::endl; using std::pow; cStudent::cStudent() { this->m_name = ""; this->m_height = 0.0; this->m_weight = 0.0; } cStudent::cStudent(string _name, float _height, float _weight) { this->m_name = _name; this->m_height = _height; this->m_weight = _weight; //cout << "Constructer with Arguments has called." << endl; } //引数でもらってきた名前をメンバにセット void cStudent::setMyName(string _name) { this->m_name = _name; } //引数でもらってきた身長をメンバにセット void cStudent::setMyHeight(float _height) { this->m_height = _height; } //引数でもらってきた体重をメンバにセット void cStudent::setMyWeight(float _weight) { this->m_weight = _weight; } string cStudent::getMyName() { return(this->m_name); } float cStudent::getMyHeight() { return(this->m_height); } float cStudent::getMyWeight() { return(this->m_weight); } //メンバ変数の体重と身長を使って //BMIを計算する 体重/身長^2 kg/(m^2) //どうして、iostreamにpowが入っているのかは謎 float cStudent::calcBMI() { //float res = this->m_weight / pow(this->m_height / 100.0, 2); //return res; return(this->m_weight / pow(this->m_height / 100.0, 2)); } void cStudent::printMe() { cout << "==== Profile of " << this->m_name << " ====" << endl; //cout << "名前:" << this->m_name << endl; cout << "身長:" << this->m_height << " cm" << endl; cout << "体重:" << this->m_weight << " kg" << endl; cout << "BMI:" << this->calcBMI() << endl; cout << "=================" << endl; }
theMain.cpp
#include "cStudent.h" #include <iostream> #include <vector> int main() { using std::cin; using std::cout; using std::endl; using std::vector; cStudent s[2] = { {"太郎",170.0,66.0}, {"次郎",168.0,72.0} }; for (int i = 0; i < 2; i++) { s[i].printMe(); } }
使ってみる
main関数内で以下のようなデータを読み込んで、クラスのメンバに値をセットしなさい。
人数分のclassの配列を作って、for分でデータを読み込む!
初めの数字はデータ数、その後に学生のデータが1行に1人分ずつ並んでいる
9 yamada 165 65 imada 156 84 kagawa 172 76 shibata 148 45 muto 182 95 okada 185 105 nagata 178 82 suzuki 179 88 akiyama 185 90
普通の関数と組み合わせてみる
main関数の前部分に関数を二つ作りなさい。
- void sortByHeight(引数は考える)
- 引数で受け取ったcStudentクラスの配列を身長順に並べ替える関数(第2引数で、昇順、降順切り替えられると素敵)
- void sortByWeight(引数は考える)
- 引数で受け取ったcStudentクラスの配列を体重順に並べ替える関数(第2引数で、昇順、降順切り替えられると素敵)
これらの関数を使い、並べ替えられたプロフィール一覧を表示しなさい。
模範?解答
これって何ソーティングだっけ?
sortByWeightは自分で考えてみよう!
それと、昇順降順の切り替えも考えてみましょう!
//オブジェクト(cStudent型)の配列を身長で並べ替える関数 //昇順にしようかな? void sortByHeight(cStudent *_stu, int n); //オブジェクト(cStudent型)の配列を体重で並べ替える関数 //昇順にしようかな? void sortByHeight(cStudent* _stu, int n) { cStudent v; int j; for (int i = 1; i < n; i++) { v = _stu[i]; j = i - 1; while (j >= 0 && _stu[j].getMyHeight() > v.getMyHeight()) { _stu[j + 1] = _stu[j]; j--; } _stu[j + 1] = v; } }