[C++] Dimensionクラス

このエントリをはてなブックマークに追加このエントリをdel.icio.usに追加このエントリをLivedoor Clipに追加このエントリをYahoo!ブックマークに追加このエントリをFC2ブックマークに追加このエントリをNifty Clipに追加このエントリをPOOKMARK. Airlinesに追加このエントリをBuzzurl(バザール)に追加このエントリをChoixに追加このエントリをnewsingに追加

C++による2次元座標(平面座標)を表現するクラスです。

追加的な関数としては、点と点の距離を求める関数くらいかな?

あんまり関数が思いつかなかったです。
それにしても、C++でのクラスファイルの作成って文法的なところが思ったよりも難しい。やっぱりPHPが簡単に思えてしまう。

また、いつものように変更したほうがいい点などは教えてください。

dimension.h

  1. #ifndef _MATH_H_INCLUDED
  2.     #include <math.h>
  3. #endif
  4. class dimension {
  5.     public:
  6.         double    x;
  7.         double    y;
  8.     public:
  9.         // コンストラクタ
  10.         dimension() {
  11.             this->x = 0;
  12.             this->y = 0;
  13.         }
  14.         // Setter
  15.         void Set( double px, double py ) {
  16.             this->x = px;
  17.             this->y = py;
  18.         }
  19.         void SetX( double px ) {
  20.             this->x = px;
  21.         }
  22.         void SetY( double py ) {
  23.             this->y = py;
  24.         }
  25.         // Getter
  26.         void Get( double &x, double &y ) {
  27.             x = this->x;
  28.             y = this->y;
  29.         }
  30.         double GetX() {
  31.             return this->x;
  32.         }
  33.         double GetY() {
  34.             return this->y;
  35.         }
  36.         // 点と点の距離を求める。(指定された点との距離)
  37.         double distance( dimension target_point ) {
  38.             double a = this->x – target_point.x;
  39.             double b = this->y – target_point.y;
  40.             double c = sqrt( a*a + b*b );
  41.             return c;
  42.         }
  43.         
  44.         // 点と点の距離を求める。(原点との距離)
  45.         double distance() {
  46.             double a = sqrt( this->x * this->x + this->y * this->y );
  47.             return a;
  48.         }
  49.         // デストラクタ
  50.         ~dimension() {
  51.         }
  52. };
  53. /*
  54. #include <stdio.h>
  55. int main() {
  56.     dimension p1;
  57.     dimension p2;
  58.     double     a, b;
  59.     p1.Set( 4, 5 );
  60.     p1.Get( a, b );
  61.     p2.Set( a, b );
  62.     printf( “Distance From Origine: %f\n”, p1.distance(p2) );
  63.     return 0;
  64. }
  65. */

関連記事

人気ブログランキングへ

[PR]

トラックバック

http://yamablo.com/2008/11/11-003903.php/trackback

コメント


Get Adobe Flash playerPlugin by wpburn.com wordpress themes