co-3. サブクラス,継承
1
金子邦彦
C++ オブジェクト指向プログラミング入門)(全3回)
URL: https://www.kkaneko.jp/pro/cpp/index.html
C++における継承の仕組み、親クラス(スーパークラ
ス)から子クラス(サブクラス)への属性・メソッドの
引き継ぎ
【学習内容の構成】
1. クラス階層:複数のクラスが親子関係をなす構造
2. 継承:親クラスの属性とメソッドを子クラスが受け
継ぐ仕組み
3. 派生クラスの実装:基本クラス(Ball)を継承した
派生クラス(ColorBall)の作
前提:C++のクラス、コンストラクタ、デストラクタ、
アクセサの理解
意義:コードの再利用性向上、クラス設計の柔軟性
獲得
2
クラス階層
クラス階層とは,複数のクラスが親子
関係をなすこと
3
クラスA
クラスB
クラスD
クラスC
継承
継承とは,親クラス属性メソッドクラス
受け継ぐこと
親クラスのことを「スーパークラス」,クラス
ことを「サブクラス」ともいう
4
オブジェクトの生成
次のオブジェクトを生成
5
オブジェクト生成を行うプログラム
b1 3 4 0
_x _y _color
クラスの類似性
類似した2つのクラス
Ball ColorBall
属性 属性
_x _x
_y _y
_color
メソッド
distance_to_0 distance_to_0
6
メソッドの名前も
中身も全く同じとする
_color の有り
無しが違う
_x, _y 同じ
クラスの親子関係
クラス①が,クラス②がであるとき
クラス②は,クラス①の属性メソッドすべて持つ
クラス②で,クラス①にない属性メソッド追加さ
れることがある
7
クラス①
クラス②
クラスBall
クラスColorBall
属性 _color を追加
派生クラス
あるクラスを継承して作成したクラス
Ball 元となるクラス(基本クラス)
ColorBall 継承したクラス(派生クラス)
8
ソースコード
9
#pragma once
class Ball {
protected:
double _x, _y;
public:
Ball( const double x, const double y );
Ball( const Ball& ball );
Ball& operator= ( const Ball& ball );
~Ball();
double distance_to_0() const;
double x() const { return this->_x; };
double y() const { return this->_y; };
};
コンストラクタ,
デストラクタ
属性(メンバ変数ともいう)
ファイル名: Ball.h
アクセサ
protected 指定により,サブクラスから属性アクセス可能
ソースコード
10
#include "Ball.h"
#include <math.h>
Ball::Ball( const double x, const double y ) : _x( x ), _y( y )
{
/* do nothing */
}
Ball::Ball( const Ball& ball ) : _x( ball.x() ), _y( ball.y() )
{
/* do nothing */
}
Ball& Ball::operator= (const Ball& ball )
{
this->_x = ball.x();
this->_y = ball.y();
return *this;
}
Ball::~Ball()
{
/* do nothing */
}
double Ball::distance_to_0() const
{
return sqrt( ( this->x() * this->x() ) + ( this->y() * this->y() ) );
}
ファイル名: Ball.cpp
ソースコード
11
#pragma once
#include "Ball.h"
class ColorBall : public Ball {
protected:
int _color;
public:
ColorBall( const double x, const double y, const int color );
ColorBall( const ColorBall& ball );
ColorBall& operator= ( const ColorBall& ball );
~ColorBall();
int color() const { return this->_color; };
};
ファイル名: ColorBall.h
ソースコード
12
#include "ColorBall.h"
ColorBall::ColorBall( const double x, const double y , const int color ) : Ball( x, y ),
_color( color )
{
/* do nothing */
}
ColorBall::ColorBall( const ColorBall& ball ) : Ball( ball.x(), ball.y() ), _color( ball.color() )
{
/* do nothing */
}
ColorBall& ColorBall::operator= (const ColorBall& ball )
{
this->_x = ball.x();
this->_y = ball.y();
this->_color = ball.color();
return *this;
}
ColorBall::~ColorBall()
{
/* do nothing */
}
ファイル名: ColorBall.cpp
ソースコード
13
#include <stdio.h>
#include "ball.h"
#include "ColorBall.h"
int main( int argc, char** argv )
{
ColorBall* b1 = new ColorBall( 3, 4, 0 );
fprintf( stderr, "b1: %f, %f, %d¥n", b1->x(), b1->y(), b1->color() );
delete b1;
}
ファイル名: main.cpp
アクセサによる
属性アクセス
Visual Studio 2019 C++ での実行結果例
14
プログラムの実行結果が
表示されている
まとめ
クラス階層とは,複数のクラスが親子関係をなす
こと
クラス①が,クラス②がであるとき
クラス②は,クラス①の属性メソッドすべて持つ
クラス②で,クラス①にない属性メソッド追加さ
れることがある
親子関係の指定は, class ColorBall : public Ball
のように書く.ColorBall が子,Ball が親.
継承とは,親クラス属性メソッド子クラス
受け継ぐこと
親クラスのことを「スーパークラス」,子クラス
のことを「サブクラス」ともいう
15