C++复习 -- 类的实现

类:

概念

C++ 中的类(class)是一种编程结构,用于创建对象。

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


C++ 类的基本结构:


1. 数据成员(Attributes):定义类的属性。这些是类内部的变量,用于存储对象的状态。
2. 成员函数(Methods):定义类的行为。这些是可以操作对象的数据成员的函数。
3. 构造函数和析构函数:特殊的成员函数。构造函数在创建对象时自动调用,用于初始化对象。析构函数在对象销毁时调用,用于执行清理操作。
4. 访问修饰符:如 public , private , protected ,用于控制对类成员的访问权限。例如, public成员可以在类的外部访问,而 private 成员只能在类内部访问。
5. 继承:允许一个类继承另一个类的特性。这是代码重用和多态性的关键。
通过这些特性,C++ 类提供了一种强大的方式来组织和处理数据,使得代码更加模块化、易于理解和维护。


===============================================
结构体 -> 结构体变量 

类 -> 对象

二者都是抽象 转变为具象


==========================

通过结构体引入类

更详细的内容请看这篇: 


C语言 + 结构体实现:

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

using namespace std;


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;
}


=========================================


与C++实现的区别:


C++ 和C  语言的结构体能混着用,无非 就是常量字符串,C语言 的类型是 char * ,而C++是 string
C ++ 里面不用 malloc 去申请空间实例化指针,直接把struct 改为class,为类创建对象,调用new函数来实例化
注意: class  类里面的数据不写的情况下是私有权限 -->private -- 不能在main里面调用要声明为 public权限


C++ 的 面向对象的特征更加明显 --> 封装,继承,多态 (权限控制)

C++实现

修改如下:

#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;
}

===========================================

成员函数:

像什么结构体实现的 函数指针实际上还是一个成员变量 --> 指针变量 -->不是真正的成员函数

// 成员函数直接使用类内部的成员变量

#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(){

    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();
return 0;
}

=====================================================

:: 作用域解析运算符


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

=================================


一般用法

 成员函数在类里面定义,在类外实现函数

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;
}

==========================================

组合:


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


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;

class Wl{
public:
    string brand;
    int year;

    void printWheelInfo();

};


class Car{ //汽车“类”
public:
string color; //颜色
string brand; //品牌
string type; //车型
Wl wl;
Wl *pwl;

int year; //年限
// 这些函数指针还是成员变量  --> 指针变量
void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
void (*carRun)(string type); //函数指针,指向车运行的函数
void (*carStop)(string type); //函数指针,执行车停止的函数
// 成员函数
void realPrintCarInfo();

};

void Wl::printWheelInfo()
{
  cout<< "轮胎品牌: "<<brand<<"  轮胎年限:  "<<year<<endl;
}


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.pwl= new Wl();

BWMthree.pwl->brand = "米其林";
BWMthree.pwl->year = 1999;


//BWMthree.wl.brand = "米其林";
//BWMthree.wl.year = 1999;

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();
//BWMthree.wl.printWheelInfo();

BWMthree.pwl->printWheelInfo();

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

AodiA6->pwl= new Wl();
AodiA6->pwl->brand = "马牌";
AodiA6->pwl->year = 2003;

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

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();

//AodiA6->wl.printWheelInfo();
AodiA6->pwl->printWheelInfo();
return 0;
}

new 对象的时候不用 ()  也ok,比如Car *AodiA6 = new Car(); --> Car *AodiA6 = new Car;

====================================================


 

  • 22
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值