Successfully reported this slideshow.
Your SlideShare is downloading. ×

co-2. メソッド定義と呼び出し

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 15 Ad

co-2. メソッド定義と呼び出し

Download to read offline

トピックス:クラス, 属性, メソッド, アクセサ, public

C++ オブジェクト指向プログラミング入門(スライド資料とプログラム例)(Visual Studio 2019 を使用)(全3回)
https://www.kkaneko.jp/pro/cpp/index.html

金子邦彦研究室ホームページ
https://www.kkaneko.jp/index.html

トピックス:クラス, 属性, メソッド, アクセサ, public

C++ オブジェクト指向プログラミング入門(スライド資料とプログラム例)(Visual Studio 2019 を使用)(全3回)
https://www.kkaneko.jp/pro/cpp/index.html

金子邦彦研究室ホームページ
https://www.kkaneko.jp/index.html

Advertisement
Advertisement

More Related Content

More from kunihikokaneko1 (20)

Advertisement

Recently uploaded (20)

co-2. メソッド定義と呼び出し

  1. 1. co-2. 属性,アクセサ 1 金子邦彦 (C++ オブジェクト指向プログラミング入門)(全3回) URL: https://www.kkaneko.jp/pro/cpp/index.html
  2. 2. メソッド • メソッドは,オブジェクトに属 する操作や処理のこと • 引数(ひきすう)とは,メソッ ドに渡す値のこと • メソッドは,クラスに属する • 属性やメソッドにアクセスするとき は「.」や「->」を用いる 2
  3. 3. アクセサ • アクセサは,属性値の取得,属 性値の更新を行うためのメソッ ド 3
  4. 4. 例題1 • Ball クラスのオブジェクトを3 個生成し,それぞれのメモリア ドレスの表示を行う • オブジェクトを生成したら,オ ブジェクトアドレスは,ポイン タ変数に格納する 4
  5. 5. クラスとオブジェクト 5 ball-1 ball-2 ball-3 3 4 1 1 3 4 Ball クラス
  6. 6. ソースコード 6 #pragma once class Ball { public: 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
  7. 7. ソースコード 7 #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
  8. 8. ソースコード 8 #include <stdio.h> #include "ball.h" int main( int argc, char** argv ) { Ball* b1 = new Ball( 3, 4 ); Ball* b2 = new Ball( 1, 1 ); Ball* b3 = new Ball( 3, 4 ); fprintf( stderr, "b1: %f, %fn", b1->x, b1->y ); fprintf( stderr, "b2: %f, %fn", b2->x, b2->y ); fprintf( stderr, "b3: %f, %fn", b3->x, b3->y ); delete b1; delete b2; delete b3; } ファイル名: main.cpp -> による属性アクセス
  9. 9. Visual Studio 2019 C++ での実行結果例 9
  10. 10. 例題2 • アクセサを定義し使用する 10
  11. 11. ソースコード 11 #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; double x() const { return this->_x; }; double y() const { return this->_y; }; }; コンストラクタ, デストラクタ 属性(メンバ変数ともいう) ファイル名: Ball.h アクセサ アクセサを定義.属性の読み出しは可能
  12. 12. ソースコード 12 #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 メソッド本体内ではアクセサを使用
  13. 13. ソースコード 13 #include <stdio.h> #include "ball.h" int main( int argc, char** argv ) { Ball* b1 = new Ball( 3, 4 ); Ball* b2 = new Ball( 1, 1 ); Ball* b3 = new Ball( 3, 4 ); fprintf( stderr, "b1: %f, %fn", b1->x(), b1->y() ); fprintf( stderr, "b2: %f, %fn", b2->x(), b2->y() ); fprintf( stderr, "b3: %f, %fn", b3->x(), b3->y() ); delete b1; delete b2; delete b3; } ファイル名: main.cpp アクセサによる 属性アクセス
  14. 14. Visual Studio 2019 C++ での実行結果例 14
  15. 15. • 属性を public にする場合 属性値を意図せず書き換えるリスク • アクセサを作る場合 アクセサでは,属性値の書き換え不可. 属性値を意図せず書き換えるリスクを抑制. 15

×