C++类关系

目录

三种类关系:

包含关系:

友元关系

友元函数

友元类

 继承关系


三种类关系:

        包含关系、友元关系、继承关系

包含关系:

        has_a  关系

        即一个类是由其他类共同组成。

        嵌套类不占用外类空间。

        has_a关系所描述的包含关系中的类的构造与析构顺序:

                构造顺序与类中所包含的类对象的声明顺序相同,析构顺序则反之。

        

        定义三个简单类:

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

class Stu
{
public:
    int age = 20;
public:
    Stu ()
    {
        cout << "stu的构造 " << endl;
    }

    ~Stu()
    {
        cout << "stu的析构" << endl;
    }
};


class Table
{
public:
    int length = 60;
public:
    Table()
    {
        cout << "table的构造" << endl;
    }
    ~Table()
    {
        cout << "table的析构" << endl;
    }
};


class Chair
{
public:
    int height = 40;
public:
    Chair()
    {
        cout << "chair的构造" << endl;
    }
    ~Chair()
    {
        cout << "chair的析构" << endl;
    }
};

        定义一个复合类:(包含关系)

class Room
{
public:
    int area = 100;
    Stu stu;
    Table table;
    Chair chair;

public:
    Room()
    {
        cout << "room的构造" << endl;
    }

    ~Room()
    {
        cout << "room的析构 " << endl;
    }
};

       主函数:

int main()
{
    Room room;
    cout << room.area << endl;

    //通过指针的偏移运算来获取嵌套类中的属性,来验证包含关系中的内存布局
    printf("%d\n", *(int*)&room);           //100
    printf("%d\n", *((int*)&room + 1));     //20
    printf("%d\n", *((int*)&room + 2));     //60
    cout << ((Chair*)&room + 3)->height << endl;     //40
    cout << "------------------" << endl;

    //但以上方式过于繁琐,一般使用访问类中包含对象中的属性
    cout << room.stu.age << endl;
    cout << sizeof (Room) << endl;  //16,4个int大小

    return 0;
}

        运行结果:

        

         内存关系图:

        

友元关系

        use_a  关系

        描述类与函数之间,类与类之间的一种亲密关系

        友元关系包含两个方式:友元函数,友元类。

友元函数

        就是类中只用friend关键字来声明某一个函数为本类的友元,当指定的友元函数中存在这个类的对象时,那么这个类对象在友元函数中可以无视类中的访问权限的影响。

#include <iostream>
using namespace std;

//友元类
class Boy
{
private:
    string name;
    int money;

public:
    Boy(string name, int money)
    {
        this->name = name;
        this->money = money;
    }

    //友元函数可以访问所有权限,无视修饰影响
    friend void beautiful_girl(Boy& boy);
};


//外函数
void beautiful_girl(Boy& boy)
{
    cout << boy.name << boy.money << endl;
}

int main()
{
    Boy boy("zhangsan", 1000);
    beautiful_girl(boy);

    return 0;
}

        运行结果:

        

友元类

#include <iostream>
using namespace std;

class Boy
{
private:
    string name;
    int age;
public:
    Boy(string name, int age)
    {
        this->name = name;
        this->age = age;
    }

    //在其中一个类中声明友元关系
    friend class Girl;
};
class Girl
{
private:
    string name;
    int age;
public:
    Girl(string name, int age)
    {
        this->name = name;
        this->age = age;
    }
    void go_shopping(Boy& boy)
    {
        cout << boy.name << "开开心心正陪着" << this->name << "逛商场买东西" << endl;
    }
};
int main()
{
    Boy boy("zhangsan",20);
    Girl girl("小丽",18);
    girl.go_shopping(boy);
    return 0;
}

        运行结果:

        

 注意:同一文件下,两个类不能相互友元

class Boy
{
public:
    friend class Girl;
};
class Girl
{
public:
    friend class Boy;
};

        此写法是错误的,若想达到两个类相互友元,则需要分文件编程。

        分五个文件:2个头文件 girl.h  boy.h。2个类文件:girl.cpp  boy.cpp。1个主函数文件:main.cpp。

        girl.h代码:

#ifndef GIRL_H
#define GIRL_H
#include <iostream>
using namespace std;

//前置声明
class Boy;

class Girl
{
private:
    string name;
    int age;
public:
    Girl(string name, int age);//构造函数
    void go_shopping(Boy& boy);//类方法
    friend class Boy;//友元
};

#endif // GIRL_H

        boy.h代码:

#ifndef BOY_H
#define BOY_H

#include <iostream>
using namespace std;

class Girl;

class Boy
{
private:
    string name;
    int age;
public:
    Boy(string name, int age);
    void hard_work(Girl& girl);
    friend class Girl;

};

#endif // BOY_H

        girl.cpp代码:

#include "girl.h"
#include "boy.h"

//构造函数
Girl::Girl(string name, int age)
{
    this->name = name;
    this->age = age;
}

//类方法
void Girl::go_shopping(Boy &boy)
{
    cout << boy.name << "开开心心正陪着" << this->name << "逛商场买东西" << endl;
}

        boy.cpp代码:

#include "boy.h"
#include "girl.h"

Boy::Boy(string name, int age)
{
    this->name = name;
    this->age = age;
}

void Boy::hard_work(Girl& girl)
{
   cout << girl.name << "正陪着" << this->name << "努力地为了美好生活而奋斗" << endl;
}

        main.cpp代码:

#include <iostream>
#include "girl.h"
#include "boy.h"

using namespace std;

int main()
{
    Boy boy("张三", 20);
    Girl girl("小李",19);

    girl.go_shopping(boy);
    boy.hard_work(girl);

    return 0;
}

        运行结果:

        

 继承关系

        C++类的继承关系_m0_58495729的博客-CSDN博客

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值