オブジェクト向けプログラミング-C++Operator Overloading&Type conversion(Static)【第11回レッスンノート】

18342 ワード

今回の授業では、[]、->などの演算子のリロードの具体例について引き続き説明しました
C++単一パラメータのクラスのタイプ変換の例も説明した
最後にstaticの3つ目の役割:静的データメンバー
 
詳しくはもう注釈しましたから、ゆっくり見てください.
どんな問題があってもこの文章の下で伝言を残して、私はすぐに解答します:)
 
#include <iostream>

#include <cmath>



using namespace std;



class myArray {

private:

    float * p;

    unsigned int size;

public:

    myArray (unsigned int len = 0);

    ~myArray ();

    unsigned int getSize () const;

    double moudar () const;



    const float & operator [] (int index) const; //                

    float & operator [] (int index);             //Only member function

};



myArray::myArray (unsigned int len) {

    float * tmp = new float[len];

    if (NULL == tmp) {       //Check whether new work OK

        cout << "memory allocation error!" << endl;

        p = NULL;

        size = 0;

    } else {

        p = tmp;

        size = len;

        for (int i = 0; i < len; ++i) {

            p[i] = 0.0f;

        }

    }

}



myArray::~myArray () {

    if (NULL != p)  {

        delete [] p;

        size = 0;

    }

    p = NULL;               //      p     

}



unsigned int myArray::getSize () const {

    return size;

}



double myArray::moudar () const {

    //float sum = 0.0f;

    double sum = 0.0;

    for (int i = 0; i < size; ++i) {

        float a = p[i];

        sum += a * a;

    }

    sum = sqrt (sum);

    return sum;

}



const float & myArray::operator [] (int index) const {

    return p[index];

}



float & myArray::operator [] (int index) {

    return p[index];

}



ostream & operator << (ostream & out, const myArray & ma) {

    int size = ma.getSize ();

    out << "{";

    for (int i = 0; i < size - 1; ++i) {

        out << ma[i] << ", ";

    }

    out << ma[size - 1] << "}" << endl;

    return out;

}



int main() {



    myArray ma(4);           //init one object

    double length = ma.moudar ();



    float element = ma[2];    //Get the element

    ma[3] = 12.5f;            //Update the element value

    ma[0] = 3.8f;



    cout << ma << endl;       //!! Only golbal function



    return 0;

}

 
// =  ()  []  ->  ->*   Only overloaded by member function



// <<         ,         ;         ,   



#include <iostream>



using namespace std;



class myClassA {

public:

    int i;

    myClassA (int k = 200) : i(k) { cout << "myClassA init" << endl; };

};



class myClassB {               // B         ,       ,     

                               //      ,  ,           

public:

    int m;

    myClassA a;                //    ,  B     ,   A    

    myClassA * operator -> ();

    //myClassB * operator -> ();



    myClassB (int j) : a(j), m(10) { cout << "myClassB init" << endl; };

};



/*

myClassB * myClassB::operator->() {

    cout << "myClassB * myClassB::operator->() " << endl;

    return this;

}

*/



myClassA * myClassB::operator->() {

    cout << " myClassA * myClassB::operator->() " << endl;

    return &a;

}



int main() {



    myClassB b (23);

    cout << b->i << endl;

    //cout << b->m << endl;



    return 0;

}

 
//C++           

//    



#include <iostream>



using namespace std;



//class A;



class Integer {

    int i;

    //A a;

public:

    //explicit     :      ,     ,     

    

    explicit Integer (int k = 0) : i (k) { cout << "Integer (int k = 0)" << endl; }

    Integer & operator = (const Integer & obj);

    friend ostream & operator << (ostream & out, const Integer & ing);



    //operator A() { return a; }

    explicit operator int () { return i; }   //      

    explicit operator double () { return i + 45.51; }   //      

};



ostream & operator << (ostream & out, const Integer & ing) {

    out << ing.i << endl;



    return out;

}



Integer & Integer::operator = (const Integer & obj) {

    if (this == &obj)    return *this;   //     

    i = obj.i;

    cout << "operator = (const Integer & obj) " << endl;

    return *this;

}



int main () {



    Integer a (45), b;

    b = Integer (63);

    cout << b << endl;



    int m = int (b);          // Integer        

    cout << m << endl;



    double d = double (b);

    cout << d << endl;



    return 0;

}

 
//static       



#include <iostream>



using namespace std;



class Integer {

    //static int i;

    static int number;  //

public:

    Integer (int k = 0) { ++number; }

    static int getNumber () const { return number; }

};



int Integer::number = 0;



int main () {



    Integer Zhao, jin, wei, shi, tian, cai;



    cout << Zhao.getNumber () << endl;

    cout << Integer::getNumber()<< endl;



    return 0;

}