19-【每天亿点点C++,简单又有趣儿】this、友元、常对象

知识点:

存储
this 指针
空指针访问成员函数
const 修饰成员函数 常函数、常对象
友元:特殊的类,特殊的函数访问私有成员
成员函数做友元


#include<iostream>
#include<string>
using namespace std;


/***********   类存储   **************/
class Club
{

};
class Club2
{
    int a; //非静态成员变量
    static int b;
    void funcc(){}
    static void funfc(){}
};
void test_13()
{
    Club c;
    cout <<"空对象 占用空间:"<< sizeof(c)<<endl;//C++编译器会给每个空对象分配一个字节的空间,区分占用位置
    Club2 c2;
    cout <<"空对象 占用空间:"<< sizeof(c2)<<endl;//C++编译器会给每个空对象分配一个字节的空间,区分占用位置

}


/***********  this 指针  **************/

class Club3
{
public:
    Club3(int age)
    {
        this->age = age; //this 指针指向的是对象
    }
    Club3& Add(Club3 &p)
    {
        this->age += p.age;
        return *this;//指向本体
    }
    int age;
};
void test_14()
{
    Club3 p1(23);
    cout << "p age:"<< p1.age<<endl;
}
void test_15()
{
    Club3 p1(3);

    Club3 p2(23);

    cout << "p age:"<< p2.age<<endl;
    p2.Add(p1).Add(p1).Add(p1); //链式编程思想
    cout << "p age:"<< p2.age<<endl;
}



/*********** 空指针访问成员函数 **************/
class Club4
{
public:
    void showClassName()
    {
        cout << "this is a Club4" << endl;
    }
    void showPersonAge()
    {
        if (this == NULL)
        {
            cout << "空对象" << endl;
            return;
        }
        cout << "age = " << this->age << endl; //报错原因是对象是空
    }
    int age;
};
void test_16()
{
    Club4 * p = NULL;
    p->showClassName();
    p->showPersonAge();
}



/*********** 常函数 常对象**************/

class Club5
{
public:
    /* this 指针 是指针常量 即 指针是常量
    / 如果要想this的指向也不可修改,那么就需要再加const
    / 有这样限定的函数叫做  【常函数】*/
    void showClub5() const
    {
        // 方法后面的const修饰的是this 意思是不可以修改this指向的值
        // 实质上是 const Club5 * const this;
        this->m_b = 100;
        // 错误 this->m_a = 100;
    }
    int m_a =10;
    mutable int m_b=10; // 特殊变量,即使在常函数中也是可以修改值的
};
void test_17()
{
    // bug 当形如type *const name的时候,该指针必须一开始就赋初值,并且不应许改变!
    const Club5 c;//常对象
    // 常对象不可修改属性 c.m_a = 100;
    c.m_b = 90; //m_b是特殊变量,在常对象下也可以修改

    c.showClub5();//常对象只能调用常函数、因为普通函数可以修改属性
}



/***********    友元    **************/
class Building
{
    //goodGay 作为好朋友 可以访问私有成员
    friend void goodGay(Building *building);
public:
    Building()
    {
        m_SettingRoom = "客厅";
        m_BedRoom = "卧室";
    }
    string m_SettingRoom;//客厅
private:
    string m_BedRoom;//卧室
};
// 全局函数
void goodGay(Building *building)
{
    cout << " goodGay() globle 访问:" << building->m_SettingRoom << endl;
    cout << " goodGay() globle 访问:" << building->m_BedRoom << endl;
}
void test_18()
{
    Building b;
    goodGay(&b);
}



/*********** 成员函数做友元 **************/
class GoodGay;
class Building02;

class GoodGay
{
public:
    GoodGay();
    void visit();//可以访问Building类私有成员
    void visit2();//可以访问Building类私有成员
    Building02 * building;
};
class Building02
{
    friend void GoodGay::visit();
public:
    Building02();
    string m_SettingRoom;//客厅
private:
    string m_BedRoom;//卧室
};

//类外实现成员函数
Building02::Building02()
{
     m_SettingRoom = "客厅";
     m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
    building = new Building02;
}
void GoodGay::visit()
{
    cout << " visit 1 " << building->m_SettingRoom<<endl;
    cout << " visit 1 " << building->m_BedRoom<<endl;
}
void GoodGay::visit2()
{
    cout << " visit 2 " << building->m_SettingRoom<<endl;
}
void test_19()
{
    GoodGay gg;
    gg.visit();
    gg.visit2();
}







int main()
{
    test_13(); // 存储
    // this 指针
    test_14();//1-解决名称冲突
    test_15();//2-链式编程

    test_16();//空指针访问成员函数
    test_17();//const 修饰成员函数 常函数、常对象
    test_18();//友元:特殊的类,特殊的函数访问私有成员
    test_19();//成员函数做友元

}

输出
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/magic_shuang/article/details/107591360
今日推荐