C++中的类

目录

1,类的初探

2,结构体引入类

2.1回忆结构体

2.2我们可以将上面的Car结构体改成Class

2.3真正的成员函数


1,类的初探

C++ 中的类(class)是一种编程结构,用于创建对象。这些对象可以拥有属性(即数据成员)和行为(即成员函数或方法)。类的概念是面向对象编程的核心之一,其主要目的是将数据和与数据相关的操作封装在一起。例如,如果你有一个“汽车”类,它可能包含颜色、品牌、型号等属性(数据成员),以及启动、停止、加速等行为(成员函数)。每当你基于这个类创建一个对象时,你就有了一个具体的汽车,具有这些属性和行为。
C++ 类的基本结构通常包含:
1. 数据成员(Attributes):定义类的属性。这些是类内部的变量,用于存储对象的状态。
2. 成员函数(Methods):定义类的行为。这些是可以操作对象的数据成员的函数。
3. 构造函数和析构函数:特殊的成员函数。构造函数在创建对象时自动调用,用于初始化对象。析构函数在对象销毁时调用,用于执行清理操作。
4. 访问修饰符:如  public ,  private ,  protected ,用于控制对类成员的访问权限。例如, public
成员可以在类的外部访问,而  private 成员只能在类内部访问。
5. 继承:允许一个类继承另一个类的特性。这是代码重用和多态性的关键。
通过这些特性,C++ 类提供了一种强大的方式来组织和处理数据,使得代码更加模块化、易于理解和维
护。

2,结构体引入类

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.2我们可以将上面的Car结构体改成Class

#include <iostream>

using namespace std;

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

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 = "C++车的品牌是:" + brand
          + ",型号是: " + type
          + ",颜色是:" + color
          + ",上市年限是:" + std::to_string(year);
      cout << str << endl;
}

void A6PrintCarInfo(string color,string brand,string type, int year)
{
    string str = "C++车的品牌是:" + 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->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中 void (*printCarInfo)(string color, string brand,string type, int year);到底是变量还是函数?其实它是一个函数指针,是一个指针变量,保存的是函数的地址变量,所以不是成员函数,是成员数据;

真正的成员函数遵守封装特性,在函数体内部访问成员数据的时候,不需要参数传递

在 C++ 中,双冒号  :: 称为 "作用域解析运算符"(Scope Resolution Operator)。它用于指定一
个成员(如函数或变量)属于特定的类或命名空间。例如,在类的外部定义成员函数时, :: 用于
指明该函数属于哪个类。

下面代码中realprintCarInfo()才是成员函数,先在代码中声明成员函数,再在类的外部实现成员函数Car::realprintCarInfo()

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
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 bwmThreePrintCarInfo(string color,string brand,string type, int year)
{
    string str = "C++车的品牌是:" + brand
          + ",型号是: " + type
          + ",颜色是:" + color
          + ",上市年限是:" + std::to_string(year);
      cout << str << endl;
}

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

void Car::realprintCarInfo(){   //在类的外部进行成员函数的实现
    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->color = "黑色";
    AodiA6->brand = "奥迪";
    AodiA6->type = "A6";
    AodiA6->year = 2008;
    AodiA6->printCarInfo = A6PrintCarInfo;
    AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);
    AodiA6->realprintCarInfo(); //成员函数的调用

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值