Successfully reported this slideshow.
Your SlideShare is downloading. ×

co-1. クラスとメソッド

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

Check these out next

1 of 21 Ad

co-1. クラスとメソッド

Download to read offline

トピックス:クラス定義, コンストラクタ, メソッド, class, new

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

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

トピックス:クラス定義, コンストラクタ, メソッド, class, new

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)

Recently uploaded (20)

Advertisement

co-1. クラスとメソッド

  1. 1. co-1. クラスとメソッド 1 金子邦彦 (C++ オブジェクト指向プログラミング入門)(全3回) URL: https://www.kkaneko.jp/pro/cpp/index.html
  2. 2. アウトライン • クラス定義 • メソッドの記述 • プログラムの実行 2
  3. 3. クラス定義 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 */ }
  4. 4. 例題1 • クラス定義 • クラス定義には,属性定義,コ ンストラクタ,デストラクタを 含める 4
  5. 5. Ball クラス • Ball クラス • Ball は,x, y という2つの属性から構成する • クラス定義には,キーワード class を使用 5 位置 x y
  6. 6. ソースコード 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. 7. ソースコード 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
  8. 8. Visual Studio 2019 C++ でのビルド結果例 8
  9. 9. オブジェクトの生成 • オブジェクトを生成するプログラム 9 b 3 4 x y • クラス Ball のオブジェクト生成を行うプログラム
  10. 10. メソッド • メソッドは,オブジェクトに属 する操作や処理のこと • 引数(ひきすう)とは,メソッ ドに渡す値のこと • メソッドは,クラスに属する • 属性やメソッドにアクセスするとき は「.」や「->」を用いる 10
  11. 11. 例題2 • メソッド定義 11
  12. 12. 例題2.メソッド • Ball の座標値から,原点までの距離を求めるメ ソッド distance-to-0 を作り,実行する • 演習1の Ball クラスを使用 12
  13. 13. ソースコード 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. 14. ソースコード 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. 15. ソースコード 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() = %fn", b->distance_to_0() ); delete b; } ファイル名: main.cpp
  16. 16. Visual Studio 2019 C++ での実行結果例 16 プログラムの実行結果が 表示されている
  17. 17. まとめ • クラス定義の中には,属性の定義(属性名とデー タ型),コンストラクタの定義,デストラクタの 定義,その他メソッドの定義を含める • コンストラクタとは,オブジェクトの生成を行う メソッドのことである. • メソッドは,オブジェクトに属する操作や処理の こと • キーワード class クラス定義 new コンストラクタの呼び出し 17
  18. 18. 実習 18
  19. 19. 問題 • Student クラスを定義しなさい.メンバ変数は次 の通り int _age; char _name[32]; 19
  20. 20. 解答例 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. 21. 解答例 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

×