ROS基础知识学习笔记(4)—C++类和对象(2)

(一)C++对象模型和this指针

成员变量和成员函数分开存储

  1. 空对象占用内存为1
    每个空对象有个独一无二的内存地址
  2. 非静态成员变量,属于类的对象上
  3. 静态成员变量,不属于类的对象上
  4. 非静态成员函数,不属于类的对象上
  5. 非静态成员变量,属于类的对象上

this指针概念

this指针指向被调用的成员函数所属的对象

  1. 解决名称冲突
  2. 返回对象本身*this

空指针可以调用成员函数

const 可以修饰成员函数

(二)友元

1.全局函数做友元

#include <iostream>

using namespace std;

class Building
{
    friend void GoodGay(Building *building);
private:
    string bedroom;

public:
    string livingroom;

public:
    Building()
    {
        bedroom="房间";
        livingroom="客厅";
    }


};

void GoodGay(Building *building)
{
    cout<<"hello world"<<building->bedroom<<endl;
    cout<<"hello world"<<building->livingroom<<endl;
}

int main()
{
    Building build;
    GoodGay(&build);
    return 0;
}

2.类做友元

太难了

3.成员函数做友元

(三)C++运算符重载

(四)继承(共性和个性)

1.class 子类:继承方式 父类

2.继承方式:

  1. 公共继承
  2. 保护继承
  3. 私有继承

在这里插入图片描述

3.继承的对象模型

子类继承父类中全部的非静态变量
private只是被隐藏了,但会继承下去

4.构造和析构顺序

构造先父类后子类,析构相反;

5.继承同名成员处理方式(变量和函数)

访问子类,直接访问
访问父类,加作用域

函数重载时,子类会隐藏父类

6.静态成员处理方式(两种访问方式)

在这里插入图片描述

7.多继承语法

8.菱形继承

菱形继承时,两个父类具有相同的数据,需要加以作用域区分

#include <iostream>

using namespace std;

class Animal
{
public:
    Animal()
    {
        cout<<"hello world"<<endl;
        age=15;
    };

    int age;
};

class Sheep:public Animal{};

class Tuo:public Animal{};

class SheepTuo:public Sheep,public Tuo{};

int main()
{
    SheepTuo s1;
    s1.Sheep::age=50;
    s1.Tuo::age=70;

    cout<<"hello world"<<s1.Sheep::age<<endl;
    cout<<"hello world"<<s1.Tuo::age<<endl;

    return 0;
}

输出

hello world
hello world
hello world50
hello world70

虚继承解决菱形继承的问题

#include <iostream>

using namespace std;

class Animal
{
public:
    Animal()
    {
        cout<<"hello world"<<endl;
        age=15;
    };

    int age;
};

//virtual关键字虚继承,Animal称为虚基类
class Sheep:virtual public Animal{};      
class Tuo:virtual public Animal{};

class SheepTuo:public Sheep,public Tuo{};

int main()
{
    SheepTuo s1;
    s1.Sheep::age=50;
    s1.Tuo::age=70;
    s1.age=1000;

    cout<<"hello world "<<s1.Sheep::age<<endl;
    cout<<"hello world "<<s1.Tuo::age<<endl;
    cout<<"hello world "<<s1.age<<endl;
    return 0;
}

输出

hello world
hello world 1000
hello world 1000
hello world 1000

(五)多态

  1. 多态分为两类:
    静态多态:函数重载和运算符重载属于静态多态,复用函数名
    动态多态:派生类和虚函数实现运行时多态
  2. 区别:
    静态多态:函数地址早绑定,编译阶段确定函数地址
    动态多态:函数地址晚绑定,运行阶段确定函数地址
#include <iostream>

using namespace std;

class Animal
{
public:
    virtual void  speak()
    {
        cout<<"Animal speak!"<<endl;
    }
};

class Cat:public Animal
{
public:
    void speak()
    {
        cout<<"Cat speak!"<<endl;
    }
};

class Dog:public Animal
{
public:
    void speak()
    {
        cout<<"Dog speak!"<<endl;
    }
};

void dospeak(Animal &animal)
{
    animal.speak();
}

int main()
{
    Cat cat1;
    dospeak(cat1);

    return 0;
}

结果:

Cat speak!
  1. 条件
    要有继承关系
    子类要重写父类的虚函数
  2. 优点
    代码组织结构清晰
    可读性强
    利于前期后期拓展和维护
    在这里插入图片描述
  3. 纯虚函数
    virtual 返回值类型 函数名(参数列表)=0;
  4. 类中有纯虚函数,也叫抽象类
    特点:
    无法实例化对象
    子类必须重写抽象类中的纯虚函数,否则子类也是抽象类
#include <iostream>

using namespace std;

class Animal
{
public:
    virtual void dospeak()=0;
    
};

class Cat:public Animal
{
public:
    virtual void dospeak()
    {
        cout<<"hello world"<<endl;
    }
};

int main()
{
    Animal *animal= new Cat;

    animal->dospeak();
    
    delete animal;

    return 0;
}

b站链接:多态案例1
b站链接:多态案例2

发布了35 篇原创文章 · 获赞 4 · 访问量 4025

猜你喜欢

转载自blog.csdn.net/qq_42589654/article/details/104283527
今日推荐