(三)C++中的静态成员、this指针、友元函数与运算符重载

C++中的静态成员与this指针

一、静态成员

1、static 关键字

  • 静态属性只能在全局范围内进行初始化赋值
  • 静态方法可以直接通过类名进行访问,也可以通过对象名进行访问
class Teacher {
private:
    char* name;
public:
    //计数器
    static int total;
public:
    Teacher(char* name) {
        this->name = name;
        cout << "Teacher有参构造函数" << endl;
    }
    ~Teacher() {
        cout << "Teacher析构函数" << endl;
    }
    void setName(char* name) {
        this->name = name;
    }
    char* getName() {
        return this->name;
    }
    //计数的静态函数
    static void count(){
        total++;
        cout << "total:" << total << endl;
    }
};

//静态属性初始化赋值
int Teacher::total = 10;

void main() {
    Teacher::total++;
    cout << Teacher::total << endl;
    //直接通过类名访问
    Teacher::count();
    cout << Teacher::total << endl;
    //通过对象名访问
    Teacher t1((char*)"JACK");
    t1.count();
    cout << Teacher::total << endl;
    getchar();
}

二、类的大小

C/C++ 内存分区:栈、堆、全局(静态、全局)、常量区(字符串)、程序代码区
普通属性与结构体具有相同的内存布局

class A {
public:
    int i;
    int j;
    int k;
};

class B {
public:
    int i;
    int j;
    int k;
    void myprint() {
        cout << "打印" << endl;
    }

};

void main() {
    cout << sizeof(A) << endl;
    cout << sizeof(B) << endl;
    getchar();
}

三、this 指针

1、this指针

this 是当前对象的指针
因为函数是共享的,所有使用this指针用来标识当前对象

class Teacher {
private:
    char* name;
    int age;
public:
    Teacher(char* name,int age) {
        this->name = name;
        this->age = age;
        cout << "Teacher有参构造函数" << endl;
    }
    ~Teacher() {
        cout << "Teacher析构函数" << endl;
    }
    void setName(char* name) {
        this->name = name;
    }
    char* getName() {
        return this->name;
    }
    void setAge(int age) {
        this->age = age;
    }
    int getAge() {
        return this->age;
    }
    void myprint() {
        cout << this->getName() << "," << this->getAge() << endl;
    }
};


void main() {
    Teacher t1((char*)"Jack", 20);
    Teacher t2((char*)"Rose", 18);
    t1.myprint();
    t2.myprint();
    getchar();
}

2、常函数(const修饰函数)

常函数,修饰的是this;既不能改变指针的值,也不能改变指针指向的内容
常量对象只能调用常量函数,不能调用非常量函数
常函数当前对象不能被修改,防止数据成员被非法访问

class Teacher {
private:
    char* name;
    int age;
public:
    Teacher(char* name,int age) {
        this->name = name;
        this->age = age;
        cout << "Teacher有参构造函数" << endl;
    }
    ~Teacher() {
        cout << "Teacher析构函数" << endl;
    }
    void setName(char* name) {
        this->name = name;
    }
    char* getName() {
        return this->name;
    }
    void setAge(int age) {
        this->age = age;
    }
    int getAge() {
        return this->age;
    }
    void myprint() const {
        printf("%#x\n",this);
        //不能改变属性的值
        //this->name = "Jason";
        //不能改变this指针的值
        //this = (Teacher*)0x000090;
        cout << this->getName() << "," << this->getAge() << endl;
    }
};

四、友元函数与友元类

1、友元函数

在友元函数中可以访问私有的属性

class A {
private:
    int i;
public:
    A(int i) {
        this->i = i;
    }
    void myprint() {
        cout << i << endl;
    }
    //友元函数
    friend void modify_i(A*p, int a);
};

//友元函数的实现,在友元函数中可以访问私有的属性
void modify_i(A*p, int a) {
    p->i = a;
}

void main() {
    A* a = new A(10);
    a->myprint();
    modify_i(a, 20);
    a->myprint();
    getchar();
}

2、友元类

友元类可以访问引用友元类的类的任何成员

class A {
    //友元类
    friend class B;
private:
    int i;
public:
    A(int i) {
        this->i = i;
    }
    void myprint() {
        cout << i << endl;
    }
    //友元函数
    friend void modify_i(A*p, int a);
};

//友元函数的实现,在友元函数中可以访问私有的属性
void modify_i(A*p, int a) {
    p->i = a;
}

class B {
private:
    A a;
public:
    B(int a_i):a(a_i) {
        this->a = a;
    }
    //B这个友元类可以访问A类的任何成员
    void accessAny() {
        a.i = 30;
    }
    A getA() {
        return a;
    }
};

void main() {
    B* b = new B(10);
    A a = b->getA();
    a.myprint();
    b->accessAny();
    a = b->getA();
    a.myprint();
    getchar();
}

五、运算符重载

运算符的重载,本质上还是函数的调用

1、单独进行运算符重载

class Point {
public:
    int x;
    int y;
public:
    Point(int x = 0,int y = 0) {
        this->x = x;
        this->y = y;
    }

    void myprint() {
        cout << x << "," << y << endl;
    }
};
//重载+号
Point operator+(Point &p1, Point &p2) {
    Point tmp(p1.x + p2.x, p1.y + p2.y);
    return tmp;
}

//重载-号
Point operator-(Point &p1, Point &p2) {
    Point tmp(p1.x - p2.x, p1.y - p2.y);
    return tmp;
}

void main() {
    Point p1(10,20);
    Point p2(20,10);
    Point p3 = p1 + p2;
    p3.myprint();
    getchar();
}

2、类成员函数运算符重载

class Point {
public:
    int x;
    int y;
public:
    Point(int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }

    void myprint() {
        cout << x << "," << y << endl;
    }
    //成员函数运算符重载+号
    Point operator+(Point &p2) {
        Point tmp(this->x + p2.x, this->y + p2.y);
        return tmp;
    }

    //成员函数运算符重载-号
    Point operator-(Point &p2) {
        Point tmp(this->x - p2.x, this->y - p2.y);
        return tmp;
    }
};


void main() {
    Point p1(10, 20);
    Point p2(20, 10);
    //p1.operator+(p1);
    Point p3 = p1 + p2;
    p3.myprint();
    getchar();
}

3、友元函数运算符重载

当属性私有时,通过友元函数完成运算符重载

class Point {
    friend Point operator+(Point &p1, Point &p2);
    friend Point operator-(Point &p1, Point &p2);
private:
    int x;
    int y;
public:
    Point(int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }

    void myprint() {
        cout << x << "," << y << endl;
    }
    
};

//友元函数运算符重载+号
Point operator+(Point &p1, Point &p2) {
    Point tmp(p1.x + p2.x, p1.y + p2.y);
    return tmp;
}

//友元函数运算符重载-号
Point operator-(Point &p1, Point &p2) {
    Point tmp(p1.x - p2.x, p1.y - p2.y);
    return tmp;
}

void main() {
    Point p1(10, 20);
    Point p2(20, 10);
    Point p3 = p1 + p2;
    p3.myprint();
    getchar();
}

你可能感兴趣的:((三)C++中的静态成员、this指针、友元函数与运算符重载)