面向对象程序设计-C++ Operator Overloading & Type conversion (Static)【第十一次上课笔记】

本次上课继续讲解了 [ ] 、-> 等运算符重载的具体例子

也讲解了C++单个参数的类的类型转换的案例

最后稍微提到了 static 的第三种作用:静态数据成员

 

具体详解我都已注释出来了,大家可以慢慢看

有任何问题都可以在这篇文章下留言我会及时解答 :)

 


#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;
}

 

posted @ 2015-05-11 13:55  Jeremy Wu  阅读(253)  评论(0编辑  收藏  举报