QT第12-14节课程笔记-类的引入

2.3 类

在这里插入图片描述

2.3.1 类的初探

C++ 中的类(class)是一种编程结构,用于创建对象。这些对象可以拥有属性(即数据成员)和行为(即成员函数或方法)。类的概念是面向对象编程的核心之一,其主要目的是将数据和与数据相关的操作封装在一起。例如,如果你有一个“汽车”类,它可能包含颜色、品牌、型号等属性(数据成员),以及启动、停止、加速等行为(成员函数)。每当你基于这个类创建一个对象时,你就有了一个具体的汽车,具有这些属性和行为。

C++ 类的基本结构通常包含:

  1. 数据成员(Attributes):定义类的属性。这些是类内部的变量,用于存储对象的状态。
  2. 成员函数(Methods):定义类的行为。这些是可以操作对象的数据成员的函数。
  3. 构造函数和析构函数:特殊的成员函数。构造函数在创建对象时自动调用,用于初始化对象。析构函数在对象销毁时调用,用于执行清理操作。
  4. 访问修饰符:如 public, private, protected,用于控制对类成员的访问权限。例如,public 成员可以在类的外部访问,而 private 成员只能在类内部访问。
  5. 继承:允许一个类继承另一个类的特性。这是代码重用和多态性的关键。

通过这些特性,C++ 类提供了一种强大的方式来组织和处理数据,使得代码更加模块化、易于理解和维护。

2.3.2 结构体引入类

2.3.2.1 回忆结构体

如果用C语言实现上面描述的汽车类,我们实现如下代码

#include <stdio.h>
#include <stdlib.h>

struct Car{       //汽车“类”
    char *color;  //颜色
    char *brand;  //品牌
    char *type;   //车型
    int year;     //年限

    void (*printCarInfo)(char *color,char *brand,char *type, int year); //函数指针,指向车介绍函数
    void (*carRun)(char *type);       //函数指针,指向车运行的函数
    void (*carStop)(char *type);      //函数指针,执行车停止的函数
};

void bwmThreePrintCarInfo(char *color,char *brand,char *type, int year)
{
    printf("车的品牌是:%s, 型号是: %s, 颜色是:%s,上市年限是%d\n",
           brand,type,color,year);
}
void A6PrintCarInfo(char *color,char *brand,char *type, int year)
{
    printf("车的品牌是:%s,型号是: %s, 颜色是:%s, 上市年限是%d\n",
           brand,type,color,year);
}

int main()
{
    struct Car BWMthree;
    BWMthree.color = "白色";
    BWMthree.brand = "宝马";
    BWMthree.type  = "3系";
    BWMthree.year  = 2023;

    BWMthree.printCarInfo = bwmThreePrintCarInfo;
    BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);

    struct Car *AodiA6;
    AodiA6 = (struct Car*)malloc(sizeof(struct Car));

    AodiA6->color = "黑色";
    AodiA6->brand = "奥迪";
    AodiA6->type = "A6";
    AodiA6->year = 2008;

    AodiA6->printCarInfo = A6PrintCarInfo;
    AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);
    return 0;
}

2.3.2.2 新建C++工程来使用结构体
  • 在C++中,字符串用string来表示,发现有个string赋值给char 的警告,所以修改所有char *为string类型
main.cpp:33:17: warning: ISO C++11 does not allow conversion from string literal to 'char *'
  • 修改后,发现printf的%s控制位,不能用于string的输出,所有有string构建了即将要输出的字符串
  • C++中,通过std::tostring()函数,将整型数转化成字符串
  • 在printInfo中使用cout输出汽车信息
  • 发现在C++工程中,使用malloc在堆申请结构体空间有问题,所以直接在此引入类的概念,把struct改成class
  • 引入新问题,class的成员数据和成员函数在不指定权限的情况下,默认private权限,类的对象无法进行直接访问
main.cpp:33:9: error: 'color' is a private member of 'Car'
main.cpp:5:11: note: implicitly declared private here
  • 添加public属性

    访问权限类内部同一个类的对象派生类(子类)类外部
    public✔️ 可访问✔️ 可访问✔️ 可访问✔️ 可访问
    private✔️ 可访问❌ 不可访问❌ 不可访问❌ 不可访问
    protected✔️ 可访问❌ 不可访问✔️ 可访问❌ 不可访问
  • 把main函数中的原本结构体变量改成了类的实例化,如果变量类型是指针,把原来的malloc改成new一个对象

  • 最后解决了所有问题

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;

class Car{       //汽车“类”

public:
    string color;  //颜色
    string brand;  //品牌
    string type;   //车型
    int year;     //年限

    void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
    void (*carRun)(string type);       //函数指针,指向车运行的函数
    void (*carStop)(string type);      //函数指针,执行车停止的函数
};

void bwmThreePrintCarInfo(string color,string brand,string type, int year)
{
    string str = "车的品牌是:" + brand
            + ",型号是: " + type
            + ",颜色是:" + color
            + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}
void A6PrintCarInfo(string color,string brand,string type, int year)
{
    string str = "车的品牌是:" + brand
            + ",型号是: " + type
            + ",颜色是:" + color
            + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}

int main()
{
    Car BWMthree;
    BWMthree.color = "白色";
    BWMthree.brand = "宝马";
    BWMthree.type  = "3系";
    BWMthree.year  = 2023;

    BWMthree.printCarInfo = bwmThreePrintCarInfo;
    BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);

    Car *AodiA6 = new Car();
  //  AodiA6 = (struct Car*)malloc(sizeof(struct Car));

   
    AodiA6->color = "黑色";
    AodiA6->brand = "奥迪";
    AodiA6->type = "A6";
    AodiA6->year = 2008;
    AodiA6->printCarInfo = A6PrintCarInfo;

    AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);

    return 0;
}


但是!我们还没有真正体验到面向对象的封装特性,仅仅感受到权限上的区别

2.3.2.3 真正的成员函数
  • 上一节的案例中, void (*printCarInfo)(string color,string brand,string type, int year); 到底是变量函数函数呢?
  • 是一个指针变量,是保存某个函数地址的变量,所以它不是成员函数,是成员数据
  • 真正的成员函数遵守封装特性,在函数体内部访问成员数据的时候,不需要参数传递
  • 在 C++ 中,双冒号 :: 称为 “作用域解析运算符”(Scope Resolution Operator)。它用于指定一个成员(如函数或变量)属于特定的类或命名空间。例如,在类的外部定义成员函数时,:: 用于指明该函数属于哪个类。
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;

class Car{       //汽车“类”

public:
    //成员数据
    string color;  //颜色
    string brand;  //品牌
    string type;   //车型
    int year;     //年限
    //其实也是成员数据,指针变量,指向函数的变量,并非真正的成员函数
    void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
    void (*carRun)(string type);       //函数指针,指向车运行的函数
    void (*carStop)(string type);      //函数指针,执行车停止的函数

    void realPrintCarInfo();//声明成员函数

};

void Car::realPrintCarInfo() //在类的外部进行成员函数的实现
{
    string str = "车的品牌是:" + brand
            + ",型号是: " + type
            + ",颜色是:" + color
            + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}

void bwmThreePrintCarInfo(string color,string brand,string type, int year)
{
    string str = "车的品牌是:" + brand
            + ",型号是: " + type
            + ",颜色是:" + color
            + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}
void A6PrintCarInfo(string color,string brand,string type, int year)
{
    string str = "车的品牌是:" + brand
            + ",型号是: " + type
            + ",颜色是:" + color
            + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}

int main()
{
    Car BWMthree;
    BWMthree.color = "白色";
    BWMthree.brand = "宝马";
    BWMthree.type  = "3系";
    BWMthree.year  = 2023;

    BWMthree.printCarInfo = bwmThreePrintCarInfo;
    BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);
    BWMthree.realPrintCarInfo();

    Car *AodiA6 = new Car();
  //  AodiA6 = (struct Car*)malloc(sizeof(struct Car));


    AodiA6->color = "黑色";
    AodiA6->brand = "奥迪";
    AodiA6->type = "A6";
    AodiA6->year = 2008;
    AodiA6->printCarInfo = A6PrintCarInfo;

    AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);
    AodiA6->realPrintCarInfo();
	 delete(AodiA6);
    return 0;
}


2.3.4 QT中经常出现的用法

在 C++中,一个类包含另一个类的对象称为组合(Composition)。这是一种常见的设计模式,用于表示一个类是由另一个类的对象组成的。这种关系通常表示一种"拥有"(“has-a”)的关系。

普通变量访问成员变量或者成员函数,使用 “ . ” 运算符

指针变量访问成员变量或者成员函数,使用“ -> ”运算符,像C语言的结构体用法

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;

class Wheel
{
public:
    string brand;
    int year;

    void wheelPrintInfo();
};

void Wheel::wheelPrintInfo()
{
    cout << "我的轮胎品牌是:" << brand << endl;
    cout << "我的轮胎日期是:" << year  << endl;
}

//在 C++中,一个类包含另一个类的对象称为组合(Composition)。
class Car{       //汽车“类”

public:
    //成员数据
    string color;  //颜色
    string brand;  //品牌
    string type;   //车型
    int year;     //年限

    Wheel wl;
    Wheel *pwl;

    //其实也是成员数据,指针变量,指向函数的变量,并非真正的成员函数
    void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
    void (*carRun)(string type);       //函数指针,指向车运行的函数
    void (*carStop)(string type);      //函数指针,执行车停止的函数

    void realPrintCarInfo();//声明成员函数

};

void Car::realPrintCarInfo() //在类的外部进行成员函数的实现
{
    string str = "车的品牌是:" + brand
            + ",型号是: " + type
            + ",颜色是:" + color
            + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}

void bwmThreePrintCarInfo(string color,string brand,string type, int year)
{
    string str = "车的品牌是:" + brand
            + ",型号是: " + type
            + ",颜色是:" + color
            + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}
void A6PrintCarInfo(string color,string brand,string type, int year)
{
    string str = "车的品牌是:" + brand
            + ",型号是: " + type
            + ",颜色是:" + color
            + ",上市年限是:" + std::to_string(year);
    cout << str << endl;
}

int main()
{
    Car BWMthree;
    BWMthree.color = "白色";
    BWMthree.brand = "宝马";
    BWMthree.type  = "3系";
    BWMthree.year  = 2023;

    BWMthree.pwl = new Wheel();
    BWMthree.pwl->brand = "米其林";
    BWMthree.pwl->year = 2023;
    //BWMthree.wl.brand = "米其林";
    //BWMthree.wl.year = 2023;

    BWMthree.printCarInfo = bwmThreePrintCarInfo;
    BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);
    BWMthree.realPrintCarInfo();
    //BWMthree.wl.wheelPrintInfo();

    Car *AodiA6 = new Car();
  //  AodiA6 = (struct Car*)malloc(sizeof(struct Car));


    AodiA6->color = "黑色";
    AodiA6->brand = "奥迪";
    AodiA6->type = "A6";
    AodiA6->year = 2008;
    AodiA6->printCarInfo = A6PrintCarInfo;

    AodiA6->pwl = new Wheel;
    AodiA6->pwl->brand = "普利司通";
    AodiA6->pwl->year = 2012;

    //AodiA6->wl.brand = "马牌";
    //AodiA6->wl.year = 2023;

    AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);
    AodiA6->realPrintCarInfo();
    //AodiA6->wl.wheelPrintInfo();
    AodiA6->pwl->wheelPrintInfo();
    delete(AodiA6->pwl);
    delete(AodiA6);
    return 0;
}

  • 26
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上官可编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值