c++ 之类

类的前摇

C++ 中的类(class)是一种编程结构,用于创建对象。这些对象可以拥有属性(即数据成员)和行为(即成员函数或方法)。类的概念是面向对象编程的核心之一,其主要目的是将数据和与数据相关的操作封装在一起。

c++ 的类通常会有以下内容:

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

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

从c的结构体过渡到类

以下是用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)
{
    // cout << brand << type << color << brand << endl;
    printf("车的品牌是:%s,车的型号是:%s,颜色是:%s,上市年限:%d\n", brand, type, color, year);
}

void A6ThreePrintCarInfo(char *color, char *brand, char *type, int year)
{
    // cout << brand << type << color << brand << endl;
    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 = A6ThreePrintCarInfo;
    AodiA6->printCarInfo(AodiA6->color, AodiA6->brand, AodiA6->type, AodiA6->year);

    return 0;
}

在这里插入图片描述

类的写法

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

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 A6ThreePrintCarInfo(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 = A6ThreePrintCarInfo;
    AodiA6->printCarInfo(AodiA6->color, AodiA6->brand, AodiA6->type, AodiA6->year);

    return 0;
}

在这里插入图片描述
但是,目前仅仅是感受与c语言结构体的区别,还没真正体验到面向对象的封装特性。

成员函数

其实上面的代码还存在函数问题没有解决?
void (*printCarInfo)(string color, string brand, string type, int year); 这种函数在C++ 是什么鬼函数呢?

我们看一下成员函数的描述:真正的成员函数遵守封装特性,在函数体内部访问成员数据的时候,不需要参数传递。

去了解成员函数之前,插播一个 :: 这玩意。这个双冒号称为 “作用域解析运算符”(Scope Resolution Operator)。
有什么用?
它用于指定一个成员(如函数或变量)属于特定的类或命名空间。
例如:
在类的外部定义成员函数时,:: 用于指明该函数属于哪个类。

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

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()
{
    cout << "成员函数打印" << endl;
    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 A6ThreePrintCarInfo(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 = A6ThreePrintCarInfo;
    AodiA6->printCarInfo(AodiA6->color, AodiA6->brand, AodiA6->type, AodiA6->year);
    AodiA6->realPrintCarInfo();

    return 0;
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值