Sample Class

C:\DATA\C#\BAT\Prog1>SampleClass
0番目:27  1番目:38  2番目:5  3番目:67  4番目:59
67
5
0番目:5  1番目:27  2番目:38  3番目:59  4番目:67

配列をパラメータとする Object Class のサンプルです。

前田稔の超初心者のプログラム入門

プログラムの説明

  1. 配列をパラメータで渡す、一般的な Object Class のサンプルです。
    後位参照が起こらないように、先に class sample を定義して下さい。
    /*★ Class のサンプル     前田 稔 ★*/
    using namespace System;
    
    ref class sample
    {
      public:
        void  print(array<int>^ t)
        {
            for(int i=0; i<t->GetLength(0); i++)
                Console::Write(i + "番目:" + t[i] + "  ");
            Console::WriteLine("");
        }
    
        int  max(array<int>^ t)
        {   int     i,wk;
            wk= 0;
            for(i=0; i<t->GetLength(0); i++)
            {   if (wk<t[i])    wk= t[i];  }
            return wk;
        }
    
        int  min(array<int>^ t)
        {   int     i,wk;
            wk= int::MaxValue;
            for(i=0; i<t->GetLength(0); i++)
            {   if (wk>t[i])    wk= t[i];  }
            return wk;
        }
    
        void  sort(array<int>^ t)
        {   int     i,j,wk;
            for(i=0; i<t->GetLength(0)-1; i++)
                for(j=i+1; j<t->GetLength(0); j++)
                    if (t[i]>t[j])
                    {   wk= t[i];
                        t[i]= t[j];
                        t[j]= wk;
                    }
        }
    };
    
    static int main()
    {
        array<int>^ tbl = gcnew array<int>(5);
        Random^ rand = gcnew Random();
        sample^ cls = gcnew sample();
    
        for(int i=0; i<5; i++)
        {   tbl[i] = rand->Next(100);  }
    
        cls->print(tbl);
        Console::WriteLine(cls->max(tbl));
        Console::WriteLine(cls->min(tbl));
        cls->sort(tbl);
        cls->print(tbl);
    }
    
  2. array<int>^ tbl は、Object Class に渡す配列の領域です。
    sample^ cls は sample Object Class のポインタです。
        array<int>^ tbl = gcnew array<int>(5);
        Random^ rand = gcnew Random();
        sample^ cls = gcnew sample();
        
  3. ^tbl には、0~99の乱数を格納します。
        for(int i=0; i<5; i++)
        {   tbl[i] = rand->Next(100);  }
        
  4. Object Class の print() メソッドを呼び出して ^tbl の値を印字します。
    次に最大値と最小値を求めるメソッドを呼び出して印字します。
        cls->print(tbl);
        Console::WriteLine(cls->max(tbl));
        Console::WriteLine(cls->min(tbl));
        
  5. sort() メソッドを呼び出して ^tbl の値をソートします。
    ソートされた ^tbl の値を印字します。
        cls->sort(tbl);
        cls->print(tbl);
        
  6. Constructor や Destructor は必要が無いので、省略しています。
        sample() {  }
        ~sample() {  }
        
    配列を受け取るメソッドは、どれも同じ要領です。
    array<int>^ t は、配列のオブジェクトが渡されるので、t->GetLength(0)で大きさを取得することが出来ます。
    ref class sample
    {
      public:
        void  print(array<int>^ t)
        {
            for(int i=0; i<t->GetLength(0); i++)
               ・・・
        }
        
  7. C#でも Sample Class を作成しています。
    ☆C#とC++/CLI との違いを学んで下さい。

UnManeged Mode

  1. 同じプログラムを UnManeged Mode で作成してみました。
    /*★ Class のサンプル     前田 稔 ★*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    class sample
    {
      public:
        void  print(int t[], int siz)
        {
            for(int i=0; i<siz; i++)
                printf("%d 番目: %d  ",i,t[i]);
            printf("\n");
        }
    
        int  max(int t[], int siz)
        {   int     i,wk;
            wk= 0;
            for(i=0; i<siz; i++)
            {   if (wk<t[i])    wk= t[i];  }
            return wk;
        }
    
        int  min(int t[], int siz)
        {   int     i,wk;
            wk= t[0];
            for(i=1; i<siz; i++)
            {   if (wk>t[i])    wk= t[i];  }
            return wk;
        }
    
        void  sort(int t[], int siz)
        {   int     i,j,wk;
            for(i=0; i<siz-1; i++)
                for(j=i+1; j<siz; j++)
                    if (t[i]>t[j])
                    {   wk= t[i];
                        t[i]= t[j];
                        t[j]= wk;
                    }
        }
    };
    
    static int main()
    {
        time_t  nowtime;
        time(&nowtime);
        srand(nowtime);
    
        int     tbl[5];
        sample  *cls = new sample();
    
        for(int i=0; i<5; i++)
        {   tbl[i] = rand()%100;  }
    
        cls->print(tbl,5);
        printf("MAX: %d\n", cls->max(tbl,5));
        printf("MIN: %d\n", cls->min(tbl,5));
        cls->sort(tbl,5);
        cls->print(tbl,5);
    
        delete cls;
        return 0;
    }
    
  2. 乱数を使うときは <stdlib.h> を include して下さい。
    <time.h> は現在時刻で乱数を初期化するために include しています。
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
        
  3. time(&nowtime); で現在時刻を取得して、srand(nowtime); で乱数を初期化します。
    このようにすると、毎回異なる乱数を発生するようになります。
    int tbl[5]; は、Object Class に渡す配列の領域です。
    sample *cls は sample Object Class のポインタです。
        time_t  nowtime;
        time(&nowtime);
        srand(nowtime);
    
        int     tbl[5];
        sample  *cls = new sample();
        
  4. tbl[] には、0~99の乱数を格納します。
        for(int i=0; i<5; i++)
        {   tbl[i] = rand()%100;  }
        
  5. Object Class の print() メソッドを呼び出して tbl の値を印字します。
    UnManeged Mode では tbl の先頭ポインタが渡されるだけなので、サイズ情報も渡します。
    次に最大値と最小値を求めるメソッドを呼び出して印字します。
        cls->print(tbl,5);
        printf("MAX: %d\n", cls->max(tbl,5));
        printf("MIN: %d\n", cls->min(tbl,5));
        
  6. sort() メソッドを呼び出して tbl[] の値をソートします。
    ソートされた tbl[] の値を印字します。
    最後に new で生成した Object を delete で解放して下さい。
        cls->sort(tbl,5);
        cls->print(tbl,5);
        delete cls;
        
  7. Constructor や Destructor は必要が無いので、省略しています。
    配列を受け取るメソッドは、どれも同じ要領です。
    int t[] は、int 型配列の先頭のポインタです。
    siz で t[] のサイズを受け取ります。
    class sample
    {
      public:
        void  print(int t[], int siz)
        {
            for(int i=0; i<siz; i++)
               ・・・
        }
        
  8. C#でも Sample Class を作成しています。
    ☆C#とC++/CLI との違いを学んで下さい。

超初心者のプログラム入門(C/C++)