int main() {
int num;
cout << "1~9までの数を入力してください。\n";
cin >> num;
try {
if(num <= 0)
throw "0以下を入力しました";
if(num >= 10)
throw "10以上を入力しました";
cout << num << "です。\n";
}
catch(char* err) {
cout << "エラー:" << err << "\n";
return 1;
}
return 0;
}
[C++] テンプレートクラス, STL vector
クラスの雛形から色々な型を扱うクラスを作成できる。
template <class T>
class Array {
private:
T data[5];
public:
void setData(int num, T d);
T getData(int num);
};
template <class T>
void Array<T>::setData(int num, T d){
if ( num < 0 || num > 4)
cout << "配列の範囲を超えています。\n";
else
data[num] = d;
}
template <class T>
T Array<T>::getData(int num){
if (num < 0 || num > 4){
cout << "配列の範囲を超えています\n";
return data[0];
}
else
return data[num];
}
int main() {
cout << "int型の配列を作成します\n";
Array<int> i_array;
i_array.setData(0, 80);
i_array.setData(1, 60);
i_array.setData(2, 58);
i_array.setData(3, 77);
i_array.setData(4, 57);
for(int i=0; i<5; i++)
cout << i_array.getData(i) << '\n';
cout << "double型の配列を作成します。\n";
Array<double> d_array;
d_array.setData(0, 35.5);
d_array.setData(1, 45.6);
d_array.setData(2, 26.8);
d_array.setData(3, 76.2);
d_array.setData(4, 85.5);
for (int j=0; j<5; j++)
cout << d_array.getData(j) << '\n';
return 0;
}
C++では standard template libraryをSTLと呼ぶ
vectorは数が決まっていない複数のデータを管理できる構造
#include <iostream>
#include <vector>
using namespace std;
int main() {
int num;
vector<int> vt;
cout << "幾つ整数データを入力しますか?\n";
cin >> num;
for(int i=0; i<num; i++){
int data;
cout << "整数を入力してください。\n";
cin >> data;
vt.push_back(data);
}
cout << "表示します。\n";
vector<int>::iterator it = vt.begin();
while(it != vt.end()){
cout << *it;
cout << "-";
it++;
}
cout << "\n";
return 0;
}
vt.begin(), vt.end()で先頭・末尾のデータを知ることができる
### STLによるアルゴリズム
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> vt;
for(int i=0; i<10; i++){
vt.push_back(i);
}
cout << "並び替え前は";
vector<int>::iterator it = vt.begin();
while(it != vt.end()){
cout << *it;
it++;
}
cout << "です。\n";
cout << "逆順にすると";
reverse(vt.begin(), vt.end());
it = vt.begin();
while(it != vt.end()){
cout << *it;
it++;
}
cout << "です。\n";
cout << "並び替え後は";
sort(vt.begin(), vt.end());
it = vt.begin();
while(it != vt.end()){
cout << *it;
it++;
}
cout << "です。\n";
return 0;
}
主なアルゴリズムにはcount, find, reverse, replace, remove, merge, sort, searchなどがある。
[C++] コピーコンストラクタ
オブジェクトが他のオブジェクトで初期化される場合に呼び出されるコンストラクタ
Car::Car(const Car& c){}
class Car {
private:
int num;
double gas;
char* pName;
public:
Car(char* pN, int n, double g);
~Car();
Car(const Car& c);
Car& operator=(const Car& c);
};
Car::Car(char* pN, int n, double g) {
pName = new char[strlen(pN)+1];
strcpy(pName, pN);
num = n;
gas = g;
cout << pName << "を作成しました。\n";
}
Car::~Car(){
cout << pName << "を破棄します。\n";
delete[] pName;
}
Car::Car(const Car& c){
cout << c.pName << "で初期化します。\n";
pName = new char[strlen(c.pName) + strlen("のコピー1") + 1];
strcat(pName, c.pName);
strcat(pName, "のコピー1");
num = c.num;
gas = c.gas;
}
Car& Car::operator=(const Car& c){
cout << pName << "に" << c.pName << "を代入します。\n";
if(this != &c) {
delete[] pName;
pName = new char[strlen(c.pName) + strlen("のコピー2") + 1];
strcat(pName, c.pName);
strcat(pName, "のコピー2");
num = c.num;
gas = c.gas;
}
return *this;
}
int main() {
Car mycar("mycar", 1234, 25.5);
Car car1 = mycar;
Car car2("car2", 0, 0);
car2 = mycar;
return 0;
}
[C++] クラスのメモリ確保
コンストラクタの中で動的にメモリを確保した場合、デストラクタ(destructor)を定義してメモリを解放する
デストラクタはチルダをつける
デストラクタはオブジェクトの破棄のタイミングで自動的に解放される
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Car {
private:
int num;
double gas;
char* pName;
public:
Car(char* pN, int n, double g);
~Car();
void show();
};
Car::Car(char* pN, int n, double g) {
pName = new char[strlen(pN)+1];
strcpy(pName, pN);
num = n;
gas = g;
cout << pName << "を作成しました。\n";
}
Car::~Car(){
cout << pName << "を破棄します。\n";
delete[] pName;
}
void Car::show(){
cout << "車のナンバーは" << num << "です。\n";
cout << "ガソリン量は" << gas << "です。\n";
cout << "名前は" << pName << "です。\n";
}
int main() {
Car car1("mycar", 1234, 25.5);
car1.show();
return 0;
}
[C++] 演算子のオーバーロード
– 演算子について新しい用法を定義することを演算子のオーバーロード(operator overloading)と言う
戻り値の型 operator 演算子(引数);
– 演算子の左オペランドがそのクラス以外の値を使うことをfriendをつけたfriend functionという
– 単項演算子をオーバーロードする
戻り値の型 operator 演算子();
class Point {
private:
int x;
int y;
public:
Point(int a = 0, int b = 0) {x= a; y = b;}
void setX(int a) {x=a;}
void setY(int b) {y=b;}
void show() {cout << " x:" << x << " y:" << y << '\n';}
Point operator++();
Point operator++(int d);
friend Point operator+(Point p1, Point p2);
friend Point operator+(Point p, int a);
friend Point operator+(int a, Point p);
};
Point Point::operator++(){
x++;
y++;
return *this;
}
Point Point::operator++(int d){
Point p = *this;
x++;
y++;
return p;
}
Point operator+(Point p1, Point p2){
Point tmp;
tmp.x = p1.x + p2.x;
tmp.y = p1.y + p2.y;
return tmp;
}
Point operator+(Point p, int a){
Point tmp;
tmp.x = p.x + a;
tmp.y = p.y + a;
return tmp;
}
Point operator+(int a, Point p) {
Point tmp;
tmp.x = a + p.x;
tmp.y = a + p.y;
return tmp;
}
int main() {
Point p1(1, 2);
Point p2(3, 6);
p1 = p1+p2;
p1++;
p1 = p1 + 3;
p2 = 3 + p2;
p1.show();
p2.show();
return 0;
}
$ g++ -o sample sample.cpp && ./sample
x:8 y:12
x:6 y:9
[C++] classの階層
direct base classとindirect base classがある。
2つ以上のクラスを継承したクラスを多重継承(multiple inheritance)という
deriveは派生という意味
class Base1 {
protected:
int bs1;
public:
Base1(int b1=0) {bs1=b1;}
void showBs1();
};
class Base2 {
protected:
int bs2;
public:
Base2(int b2=0) {bs2=b2;}
void showBs2();
};
class Derived: public Base1, public Base2 {
protected:
int dr;
public:
Derived(int d=0){dr=d;}
void showDr();
};
void Base1::showBs1() {
cout << "bs1は" << bs1 << "です。\n";
}
void Base2::showBs2() {
cout << "bs2は" << bs2 << "です。\n";
}
void Derived::showDr(){
cout << "drは" << dr << "です。\n";
}
int main(){
Derived drv;
drv.showBs1();
drv.showBs2();
drv.showDr();
return 0;
}
メンバ関数が同じ場合
int main(){
Derived drv;
drv.Base1::showBs();
drv.Base2::showBs();
drv.showDr();
return 0;
}
仮想基本クラス(virtual base class)
class Base0 {
protected:
int bs0;
public:
Base0(int b0=0) {bs0=b0;}
void showBs0();
};
class Base1 : public virtual Base0{
protected:
int bs1;
public:
Base1(int b1=0) {bs1=b1;}
void showBs();
};
class Base2 : public virtual Base0{
protected:
int bs2;
public:
Base2(int b2=0) {bs2=b2;}
void showBs();
};
class Derived: public Base1, public Base2 {
protected:
int dr;
public:
Derived(int d=0){dr=d;}
void showDr();
};
void Base0::showBs0() {
cout << "bs0は" << bs0 << "です。\n";
}
void Base1::showBs() {
cout << "bs1は" << bs1 << "です。\n";
}
void Base2::showBs() {
cout << "bs2は" << bs2 << "です。\n";
}
void Derived::showDr(){
cout << "drは" << dr << "です。\n";
}
int main(){
Derived drv;
drv.showBs0();
return 0;
}
[C++] 純粋仮想関数(pure virual function)
純粋仮想関数は仮想関数の宣言の最後に =0という指定をつけたもの
純粋仮想関数を1つでも持つ抽象クラスはオブジェクトを作成できない。
class Vehicle {
protected:
int speed;
public:
void setSpeed(int s);
virtual void show() = 0;
};
class Car : public Vehicle {
private:
int num;
double gas;
public:
Car(int n, double g);
void show();
};
class RacingCar : public Car {
private:
int course;
public:
RacingCar();
void setCourse(int c);
void show();
};
class Plane : public Vehicle {
private:
int flight;
public:
Plane(int f);
void show();
};
void Vehicle::setSpeed(int s){
speed = s;
cout << "速度を" << speed << "にしました。\n";
}
Car::Car(int n, double g) {
num = n;
gas = g;
cout << "ナンバー" << num << "ガソリン量" << gas << "の車を作成しました。\n";
}
void Car::show(){
cout << "車のナンバーは" << num << "です。\n";
cout << "ガソリン量は" << gas << "です。\n";
cout << "速度は" << speed << "です。\n";
}
Plane::Plane(int f) {
flight = f;
cout << "便" << flight << "の飛行機を作成しました。\n";
}
void Plane::show() {
cout << "飛行機の便は" << flight << "です。\n";
cout << "速度は" << speed << "です。\n";
}
int main() {
Vehicle* pVc[2];
Car car1(1234, 20.5);
pVc[0] = &car1;
pVc[0]->setSpeed(60);
Plane pln1(232);
pVc[1] = &pln1;
pVc[1]->setSpeed(500);
for(int i=0; i<2; i++){
pVc[i]->show();
}
return 0;
}
typeid演算子でオブジェクトのclassを調べることができる。
ヘッダにtypeinfoをインクルードする
int main() {
Vehicle* pVc[2];
Car car1(1234, 20.5);
pVc[0] = &car1;
Plane pln1(232);
pVc[1] = &pln1;
for(int i=0; i<2; i++) {
if(typeid(*pVc[i]) == typeid(Car))
cout << (i+1) << "番目は" << typeid(Car).name() << "です。\n";
else
cout << (i+1) << "番目は" << typeid(Car).name() << "ではありません。" << typeid(*pVc[i]).name() <<"です。\n";
}
return 0;
}
$ g++ -o sample sample.cpp && ./sample
ナンバー1234ガソリン量20.5の車を作成しました。
便232の飛行機を作成しました。
1番目は3Carです。
2番目は3Carではありません。5Planeです。
typeid(Car).name()だとoutputがおかしい。
[C++] 派生クラスと基本クラス
protectedとすると、派生クラスからアクセスできる。
class Car {
protected:
int num;
double gas;
public:
Car();
void setCar(int n, double g);
void show();
};
class RacingCar : public Car {
private:
int course;
public:
RacingCar();
void setCourse(int c);
void show();
};
Car::Car() {
num = 0;
gas = 0.0;
cout << "車を作成しました。\n";
}
void Car::setCar(int n, double g){
num = n;
gas = g;
cout << "車のナンバーを" << num << "ガソリン量を" << gas << "にしました。\n";
}
void Car::show(){
cout << "車のナンバーは" << num << "です。\n";
cout << "ガソリン量は" << gas << "です。\n";
}
RacingCar::RacingCar(){
course = 0;
cout << "レーシングカーを作成しました。\n";
}
void RacingCar::setCourse(int c){
course = c;
cout << "コース番号を" << course << "にしました。\n";
}
void RacingCar::show(){
cout << "レーシングカーのナンバーは" << num << "です。\n";
cout << "ガソリンの量は" << gas << "です。\n";
cout << "コース番号は" << num << "です。\n";
}
int main() {
RacingCar rccar1;
rccar1.setCar(1234, 20.5);
rccar1.setCourse(5);
rccar1.show();
return 0;
}
$ g++ -o sample sample.cpp && ./sample
車を作成しました。
レーシングカーを作成しました。
車のナンバーを1234ガソリン量を20.5にしました。
コース番号を5にしました。
レーシングカーのナンバーは1234です。
ガソリンの量は20.5です。
コース番号は1234です。
派生クラスで定義したメンバ関数が機能することをオーバーライドという
[C++] classの様々な機能
拡張クラスが既存のクラスのメンバを受け継ぐことを継承(inheritance)と呼ぶ
基本クラス、新しいクラスは派生クラスと呼ぶ
### 継承
class Car {
private:
int num;
double gas;
public:
Car();
void setCar(int n, double g);
void show();
};
class RacingCar : public Car {
private:
int course;
public:
RacingCar();
void setCourse(int c);
};
Car::Car() {
num = 0;
gas = 0.0;
cout << "車を作成しました。\n";
}
void Car::setCar(int n, double g){
num = n;
gas = g;
cout << "車のナンバーを" << num << "ガソリン量を" << gas << "にしました。\n";
}
void Car::show(){
cout << "車のナンバーは" << num << "です。\n";
cout << "ガソリン量は" << gas << "です。\n";
}
RacingCar::RacingCar(){
course = 0;
cout << "レーシングカーを作成しました。\n";
}
void RacingCar::setCourse(int c){
course = c;
cout << "コース番号を" << course << "にしました。\n";
}
int main() {
RacingCar rccar1;
rccar1.setCar(1234, 20.5);
rccar1.setCourse(5);
return 0;
}
$ g++ -o sample sample.cpp && ./sample
車を作成しました。
レーシングカードを作成しました。
車のナンバーを1234ガソリン量を20.5にしました。
コース番号を5にしました。
### 基本クラスのコンストラクタを指定
RacingCar::RacingCar(int n, double g, int c): Car(n, g) が呼び出されるようになる。
class Car {
private:
int num;
double gas;
public:
Car();
Car(int n, double g);
void setCar(int n, double g);
void show();
};
class RacingCar : public Car {
private:
int course;
public:
RacingCar();
RacingCar(int n, double g, int c);
void setCourse(int c);
};
Car::Car() {
num = 0;
gas = 0.0;
cout << "車を作成しました。\n";
}
Car::Car(int n, double g){
num = n;
gas = g;
cout << "ナンバー" << num << "ガソリン量" << gas << "の車を作成しました。\n";
}
void Car::setCar(int n, double g){
num = n;
gas = g;
cout << "車のナンバーを" << num << "ガソリン量を" << gas << "にしました。\n";
}
void Car::show(){
cout << "車のナンバーは" << num << "です。\n";
cout << "ガソリン量は" << gas << "です。\n";
}
RacingCar::RacingCar(){
course = 0;
cout << "レーシングカーを作成しました。\n";
}
RacingCar::RacingCar(int n, double g, int c): Car(n, g){
course = 0;
cout << "コース番号" << course << "のレーシングカーを作成しました。\n";
}
void RacingCar::setCourse(int c){
course = c;
cout << "コース番号を" << course << "にしました。\n";
}
int main() {
RacingCar rccar1(1234, 20.5, 5);
return 0;
}
[C++] classの機能: constructor
constructorは戻り値を持たない
constractorは className::className(引数)となる
class Car {
private:
int num;
double gas;
public:
Car();
void show();
};
Car::Car() {
num = 0;
gas = 0.0;
cout << "車を作成しました。\n";
}
void Car::show(){
cout << "車のナンバーは" << num << "です。\n";
cout << "ガソリン量は" << gas << "です。\n";
}
int main() {
Car car1;
car1.show();
return 0;
}
引数の数・型が異なっていれば、同じ名前の関数を複数定義できる。これをオーバーっロードという。
class Car {
private:
int num;
double gas;
public:
Car();
Car(int n, double g);
void show();
};
Car::Car() {
num = 0;
gas = 0.0;
cout << "車を作成しました。\n";
}
Car::Car(int n, double g){
num = n;
gas = g;
cout << "車のナンバーは" << num << "ガソリン量は" << gas << "の車を作成しました。\n";
}
void Car::show(){
cout << "車のナンバーは" << num << "です。\n";
cout << "ガソリン量は" << gas << "です。\n";
}
int main() {
Car car1;
Car car2(1234, 20.5);
return 0;
}
$ g++ -o sample sample.cpp && ./sample
車を作成しました。
車のナンバーは1234ガソリン量は20.5の車を作成しました。
### コンストラクタの応用
オブジェクtの配列を作成
int main() {
Car mycars[3] = {
Car(),
Car(1234, 25.5),
Car(4567, 52.2)
};
return 0;
}
$ g++ -o sample sample.cpp && ./sample
車を作成しました。
車のナンバーは1234ガソリン量は25.5の車を作成しました。
車のナンバーは4567ガソリン量は52.2の車を作成しました。
int main() {
Car cars[3];
return 0;
}
$ g++ -o sample sample.cpp && ./sample
車を作成しました。
車を作成しました。
車を作成しました。
class Car {
private:
int num;
double gas;
public:
Car(int n=0, double g=0);
void show();
};
//
int main() {
Car car1;
Car car2(1234, 20.5);
return 0;
}
オブジェクトに関連づけられないメンバを静的メンバという
関数の前にstaticを付与する
class Car {
private:
int num;
double gas;
public:
static int sum;
Car();
void setCar(int n, double g);
void show();
static void showSum();
};
Car::Car() {
num = 0;
gas = 0.0;
sum++;
cout << "車を作成しました。\n";
}
void Car::setCar(int n, double g){
num = n;
gas = g;
cout << "車のナンバーは" << num << "ガソリン量は" << gas << "の車を作成しました。\n";
}
void Car::showSum(){
cout << "車は全部で" << sum << "台あります。\n";
}
void Car::show(){
cout << "車のナンバーは" << num << "です。\n";
cout << "ガソリン量は" << gas << "です。\n";
}
int Car::sum = 0;
int main() {
Car::showSum();
Car car1;
car1.setCar(1234, 20.5);
Car::showSum();
Car car2;
car2.setCar(4567, 30.5);
Car::showSum();
return 0;
}