C++による基本オブジェクト指向プログラミングの理解


必要条件

  • マシンにインストールされたC +コンパイラ
  • C +の基本的な理解
  • IDEまたはText Editor
  • 時間
  • オープンマインド
  • 導入


    オブジェクト指向プログラミングは、実生活オブジェクトまたはコードの実体を表すプログラミングのパラダイムです.
    初心者のためには、OOP、すなわちクラスとオブジェクトで理解しなければならない2つの基本的であるが、重要な概念があります.

    授業は何ですか。


    クラスは、オブジェクトの属性と動作を指定する青写真です.我々は新しいクラスの人間を作成すると仮定し、我々は人間に固有のいくつかの属性を指定することができます.
    ...
    class human {
      public:
        string name;
        int age;
    };
    ...
    
    上のスニペットで、属性を持つクラスを作成しましたname and age .

    オブジェクトは何ですか?


    オブジェクトはクラスのインスタンスであり、クラスから任意のプロパティや属性を仮定できるインスタンスです.明らかにするために、我々人間は我々の遺伝子の例です.
    以前に作成したクラスを使用して、オブジェクトを作成して属性を割り当てることができますname and age 値.
    #include <iostream>
    using namespace std;
    
    class human {
      public:
        string name;
        int age;
    };
    
    int main () {
      human exhibitA;
    
      exhibitA.name = "Michael";
      exhibitA.age = 25;
    
      cout <<"Exhibit A's Name is "<< exhibitA.name << endl;
      cout <<"Exhibit A's Age is "<< exhibitA.age << endl;
    
      return 0;
    }
    
    上のスニペットでは、うまくクラスを作成し、オブジェクトを作成し、その親クラスからオブジェクト属性を割り当てました.

    この時点で、どのようにクラスとオブジェクトを理解する必要があります.クラスが1つのオブジェクトに限られていないことに注意してください.
    #include <iostream>
    using namespace std;
    
    class human {
      public:
        string name;
        int age;
    };
    
    int main () {
      human exhibitA;
      human exhibitB;
    
      exhibitA.name = "Michael";
      exhibitA.age = 25;
    
      exhibitB.name = "Nerfetiti";
      exhibitB.age = 24;
    
      cout <<"Exhibit A's Name is "<< exhibitA.name << endl;
      cout <<"Exhibit A's Age is "<< exhibitA.age << endl;
    
      cout <<"Exhibit B's Name is "<< exhibitB.name << endl;
      cout <<"Exhibit B's Age is "<< exhibitB.age << endl;
    
      return 0;
    }
    
    上記のスニペットで見られるように、新しいオブジェクトはつくられて、親クラスで定義されるように属性を割り当てられました.

    どのような行動ですか?


    我々が見ているもう一つの概念は行動です、行動はオブジェクトが実行することができる行動です.たとえば、人間は走ることができて、食べることができて、眠ることができます.
    #include <iostream>
    using namespace std;
    
    class human {
      public:
        string name;
        int age;
    
        void run () {
          cout << name <<" is running" << endl;
        }
    };
    
    int main () {
      human exhibitA;
    
      exhibitA.name = "Michael";  
      exhibitA.run();
    
      return 0;
    }
    

    上記のスニペットで見られるように、「Michaelが走っている」ことを印刷するためにパブリック名変数を通過するために、関数はつくられました.その関数/動作に割り当てられたオブジェクトに基づいて、それはドット表記を通してその機能/ふるまいによって必要とされるパラメタを通過します.

    オーバーロードとは何か


    オーバーロードは、それに渡されたパラメータに基づいて異なる操作を実行するために同じ関数名を使用できる概念です.この例では、我々は新しい行動を起こすeat() , パラメータなしで、1つのパラメタと2つのパラメタで.
    #include <iostream>
    using namespace std;
    
    class human {
      public:
        string name;
        int age;
    
        void eat () {
          cout << name <<" is eating" << endl;
        }
    };
    
    int main () {
      human exhibitA;
    
      exhibitA.name = "Michael";
      exhibitA.eat();
    
      return 0;
    }
    
    
    上で見たように、我々はeat パラメータを指定しないと、「マイケルは食べている」という名前を返しますexhibitA 名前.

    上を移動すると、我々は過負荷eat もう一つの振舞いのためにパラメタを要求することによるふるまいも、Antと呼ばれました.
    #include <iostream>
    using namespace std;
    
    class human {
      public:
        string name;
        int age;
    
        void eat () {
          cout << name <<" is eating" << endl;
        }
    
        void eat (string food1) {
          cout << name <<" is eating "<< food1 << endl;
        }
    };
    
    int main () {
      human exhibitA;
    
      exhibitA.name = "Michael";
      exhibitA.eat();
      exhibitA.eat("Rice");
    
      return 0;
    }
    
    上のスニペットは2番目ですeat 関数はパラメータを必要としますが、このパラメータは、「Michaelがお米を食べている」という形に渡されます.

    さあ、最後に新しいeat つのパラメータを受け入れる動作.
    #include <iostream>
    using namespace std;
    
    class human {
      public:
        string name;
        int age;
    
        void eat () {
          cout << name <<" is eating" << endl;
        }
    
        void eat (string food1) {
          cout << name <<" is eating "<< food1 << endl;
        }
    
        void eat (string food1, string food2) {
          cout << name <<" is eating "<< food1 << " and " << food2 << endl;
        }
    };
    
    int main () {
      human exhibitA;
    
      exhibitA.name = "Michael";
      exhibitA.eat();
      exhibitA.eat("Rice");
      exhibitA.eat("Rice", "Beans");
    
      return 0;
    }
    
    この断片の最終的な出力は、「マイケルは米と豆を食べています」、最後ですeat コールオーバーロードeat つのパラメータを持つ振る舞いは、私たちが厳密に2つの引数を通過したので、人間のクラスで宣言されました.

    あなたが他のすべてに気づくならばeat まだ宣言された行動は他のものに関係なく動作しますeat 前後に宣言された動作.

    結論


    今では、オブジェクト指向プログラミングがどのように動作するかについてのアイデアを持っているはずですが、C +ではなく、この概念は他のプログラミング言語にも適用可能です.私は遺伝、多形、データ抽象化とインターフェースのような概念を逃しました.私は楽しみを持っている別の記事でこれらの概念をカバーしようとします.