co-1. クラスとメソッド
1
金子邦彦
C++ オブジェクト指向プログラミング入門)(全3回)
URL: https://www.kkaneko.jp/pro/cpp/index.html
C++におけるクラス定義の基本構造、属性・コン
トラクタ・デストラクタ・メソッドの記述方法
学習内容の構成
1. クラス定義:属性(メンバ変数)、コンストラ
クタ、デストラクタを含むクラスの宣言と実装
2. メソッド:オブジェクトに属する操作や処理の
定義と呼び出し
3. オブジェクトの生成newによるコンストラ
タ呼び出しとオブジェクト操作
前提:C++の基本文法、ヘッダファイルと実装
ファイルの分離
意義:オブジェクト指向プログラミングによる
データと操作の一体化の理解
2
クラス定義
3
クラス定義の中には,属性の定義(属性名データ),コンストラク
の定義,デストラクタの定義,そのメソッド定義を含める
#pragma once
class Ball {
private:
double x, y;
public:
Ball( const double x, const double y );
Ball( const Ball& ball );
Ball& operator= ( const Ball& ball );
~Ball();
};
コンストラクタ,
デストラクタ
属性(メンバ変数ともいう)
#include "Ball.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 */
}
例題1
クラス定義
クラス定義には,属性定義,コ
ンストラクタ,デストラクタを
含める
4
Ball クラス
Ball クラス
Ball は,x, y という2つの属性から構成する
クラス定義には,キーワード class を使用
5
位置
x
y
ソースコード
6
#pragma once
class Ball {
private:
double x, y;
public:
Ball( const double x, const double y );
Ball( const Ball& ball );
Ball& operator= ( const Ball& ball );
~Ball();
};
コンストラクタ,
デストラクタ
属性(メンバ変数ともいう)
ファイル名: Ball.h
ソースコード
7
#include "Ball.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 */
}
ファイル名: Ball.cpp
Visual Studio 2019 C++ でのビルド結果例
8
オブジェクトの生成
オブジェクトを生成するプログラム
9
b 3 4
x y
クラス Ball のオブジェクト生成を行うプログラム
メソッド
メソッドは,オブジェクトに属
する操作や処理のこと
引数(ひきすう)とは,メソッ
ドに渡す値のこと
メソッドは,クラスに属する
属性メソッドにアクセスするとき
.」や「->」を用いる
10
例題2
メソッド定義
11
例題2.メソッド
Ball の座標値から,原点までの距離を求めるメ
ソッド distance-to-0 を作り,実行する
演習1の Ball クラスを使用
12
ソースコード
13
#pragma once
class Ball {
private:
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;
};
コンストラクタ,
デストラクタ
属性(メンバ変数ともいう)
ファイル名: Ball.h
追加されたメソッド
ソースコード
14
#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
追加されたメソッド
ソースコード
15
#include <stdio.h>
#include "ball.h"
int main( int argc, char** argv )
{
Ball* b = new Ball( 3, 4 );
fprintf( stderr, "b->distance_to_0() = %f¥n", b->distance_to_0() );
delete b;
}
ファイル名: main.cpp
Visual Studio 2019 C++ での実行結果例
16
プログラムの実行結果が
表示されている
まとめ
クラス定義の中には,属性の定義(属性名デー
タ型),コンストラクタの定義,デストラクタ
定義,その他メソッドの定義を含める
コンストラクタとは,オブジェクトの生成を行う
メソッドのことである.
メソッドは,オブジェクトに属する操作や処理
こと
キーワード
class クラス定義
new コンストラクタの呼び出し
17
実習
18
問題
Student クラスを定義しなさい.メンバ変数は
の通り
int _age;
char _name[32];
19
解答例
20
#pragma once
class Student {
private:
int _age;
char _name[32];
public:
Student( const int age, const char name[32] );
Student( const Student& student );
Student& operator= (const Student& student );
~Student();
};
ファイル名: Student.h
解答例
21
#include <string.h>
#include "Student.h"
#pragma warning(disable:4996)
Student::Student( const int age, const char name[32] ) : _age( age )
{
strcpy( this->_name, name );
}
Student::Student( const Student& student ) : _age( student._age )
{
strcpy( this->_name, student._name );
}
Student& Student::operator= (const Student& student )
{
this->_age = student._age;
strcpy( this->_name, student._name );
return *this;
}
Student::~Student()
{
/* do nothing */
}
ファイル名: Student.cpp