1、类
1.1 什么是类
C++ 中的类(class)是一种编程结 构,用于创建对象。这些对象可以拥有属性(即数据成员)和行为(即成员函数或方法)。类的概念是面向对象编程的核心之一,其主要目的是将数据和与数据相关的操作封装在一起。例如,如果你有一个“汽车”类,它可能包含颜色、品牌、型号等属性(数据成员),以及启动、停止、加速等行为(成员函数)。每当你基于这个类创建一个对象时,你就有了一个具体的汽车,具有这些属性和行为。
C++ 类的基本结构通常包含:
(1)数据成员(Attributes):定义类的属性。这些是类内部的变量,用于存储对象的状态。
(2)成员函数(Methods):定义类的行为。这些是可以操作对象的数据成员的函数。
(3)构造函数和析构函数:特殊的成员函数。构造函数在创建对象时自动调用,用于初始化对象。析构函数在对象销毁时调用,用于执行清理操作。
(4)访问修饰符:如 public , private , protected ,用于控制对类成员的访问权限。例如, public成员可以在类的外部访问,而 private 成员只能在类内部访问。
(5)继承:允许一个类继承另一个类的特性。这是代码重用和多态性的关键。
通过这些特性,C++ 类提供了一种强大的方式来组织和处理数据,使得代码更加模块化、易于理解和维护。
1.2 从结构体引入类
1.2.1 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)
{
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;
}
1.2.2 C++中使用结构体
新建一个C++文件直接来执行上述代码,会有一些问题。
在C++中字符串用string来表示,要修改所有char *为 string类型;
在修改后,会发现现printf的%s控制位,不能用于string的输出,即使有string构建了即将要输出的字符串;
C++中,通过std::tostring()函数,将整型数转化成字符串;
在printInfo中使用cout输出汽车信息;
发现在C++工程中,使用malloc在堆申请结构体空间有问题,所以直接在此引入类的概念,把struct改成class;
引入新问题,class的成员数据和成员函数在不指定权限的情况下,默认private权限,类的对象无法进行直接访问,因此要添加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;
}
1.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();
return 0;
}
1.3 组合
在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;
//类里面的对象如果是指针,也要new一下
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();
return 0;
}
2、权限初识
2.1 基本介绍
C++的访问权限主要分为三种: public 、 private 和 protected 。这些权限决定了类成员(包括数据成员和成员函数)的可访问性。
访问权限
|
类内部
|
同一个类的对象
|
派生类(子类)
|
类外部
|
public
|
✔️ 可访问
|
✔️ 可访问
|
✔️ 可访问
|
✔️ 可访问
|
private
|
✔️ 可访问
|
❌ 不可访问
|
❌ 不可访问
|
❌ 不可访问
|
protected
|
✔️ 可访问
|
❌ 不可访问
|
✔️ 可访问
|
❌ 不可访问
|
使用权限(如 public 、 private 和 protected )在C++中是一种关键的封装手段,它们旨在控制对类成员的访问。下面是一个表格,总结了使用权限的主要好处和潜在缺点:
好处 / 缺点
|
描述
|
好处
|
|
封装性
|
通过隐藏类的内部实现(私有和受保护成员),提高了代码的安全性和健壮性。
|
接口与实现的分离
|
公开接口(公开成员)与私有实现分离,有助于用户仅关注于如何使用类而不是如何实现。
|
易于维护
|
修改类的内部实现不会影响使用该类的代码,从而降低了维护成本。
|
控制读写访问
|
通过设置访问权限,可以精确控制类成员的读写访问。
|
继承的灵活性
|
protected 成员在派生类中是可访问的,使得继承更加灵活。
|
缺点
|
|
增加复杂性
|
过度使用或不当使用权限可能导致代码结构复杂,难以理解。
|
测试难度
|
私有成员的测试比公共成员更困难,因为它们不能从类的外部访问。
|
灵活性降低
|
过于严格的封装可能限制了某些有效的用法,降低了灵活性。
|
可能导致紧耦合
|
过多依赖 friend 类或函数可能导致类之间的耦合过紧。
|
2.2 简单示例
public权限相当于我们学习C语言结构体一样,不考虑访问权限的存在,但是要注意,类中不写权限,默认是私有权限。
假设一个场景:银行的账户是一个模板,是一个类,有存款人信息和账户额度,而具体的存款人视为一个对象, 一个对象不能私自修改账户额度,需要通过一个操作流程,比如去ATM或者柜台进行操作才能修改到账户额度,所以,存款人信息和账户额度设计成私有权限,通过公有的操作流程,也就是公有函数去操作私有变量。基于这个场景,我们编程实现代码。
#include <iostream>
#include <string>
using namespace std;
class BankAccount{
private:
//有存款人信息和账户额度
string name;
string addr;
int age;
double balance;
public:
string bankAddr;
//比如去ATM或者柜台进行操作才能修改到账户额度
void registerMes(string newName, string newAddr,int newAge,double newBalance);
void withdraw(double amount);
void deposit(double amount);
double getBalance();
void printUserInfo();
};
void BankAccount::printUserInfo()
{
string mesTem = "账户名:" + name + ",地址:" + addr +
",年龄:"+ std::to_string(age) + ",存款:" + std::to_string(balance);
cout << mesTem << endl;
}
void BankAccount::registerMes(string newName, string newAddr,int newAge,double newBalance)
{
name = newName;
addr = newAddr;
age = newAge;
balance = newBalance;
}
// 存款方法
void BankAccount::deposit(double amount) {
if (amount > 0) {
balance += amount;
} else {
cerr << "Deposit amount must be positive." << endl;
}
}
// 取款方法
void BankAccount::withdraw(double amount) {
if (amount > balance) {
cerr << "Insufficient funds." << endl;
} else if (amount <= 0) {
cerr << "Withdrawal amount must be positive." << endl;
} else {
balance -= amount;
}
}
// 获取当前余额的方法
double BankAccount::getBalance() {
return balance;
}
int main()
{
BankAccount user1;
user1.registerMes("老A","深圳光明区",35,100);
user1.printUserInfo();
user1.deposit(1000);
cout << user1.getBalance() << endl;
user1.withdraw(30);
cout << user1.getBalance() << endl;
return 0;
}
在这个示例中, balance 是一个 private 成员变量,它不能被类的外部直接访问。这保证了账户余额只能通过类提供的方法(如 deposit , withdraw , 和 getBalance )来修改和查询,从而防止了不合适的修改,比如直接设置余额为负数或任意值。这样的设计保证了类的封装性和数据的完整性。
3、引用
3.1 引用和指针的区别
引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。
在C语言中,一个数据对应一个内存,通过由一个变量名来访问这个内存空间的数据,叫做直接访问,相对直接访问,有个间接访问的说法,叫做指针。
而引用相当于又给这个内存中的数据提供了一个新的变量名,而且这个变量名功能比传统变量名更特殊,是直达地址的。
主要区别:
(1)不存在空引用。引用必须连接到一块合法的内存。
(2)一旦引用被初始化为一个对象,就不能被指向到另一个对象。指针可以在任何时候指向到另一个对象。
(3)引用必须在创建时被初始化。指针可以在任何时间被初始化。
//使用 & 进行引用
int& r = i; //r 是一个初始化为 i 的整型引用
double& s = d; //s 是一个初始化为 d 的 double 型引用
#include <iostream>
using namespace std;
int main ()
{
// 声明简单的变量
int i;
double d;
// 声明引用变量
int& r = i;
double& s = d;
i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
return 0;
}
3.2 将引用作为参数
#include <iostream>
using namespace std;
// 函数声明
void swap(int& x, int& y);
int main ()
{
// 局部变量声明
int a = 100;
int b = 200;
cout << "交换前,a 的值:" << a << endl;
cout << "交换前,b 的值:" << b << endl;
/* 调用函数来交换值 */
swap(a, b);
cout << "交换后,a 的值:" << a << endl;
cout << "交换后,b 的值:" << b << endl;
return 0;
}
// 函数定义
void swap(int& x, int& y)
{
int temp;
temp = x; /* 保存地址 x 的值 */
x = y; /* 把 y 赋值给 x */
y = temp; /* 把 x 赋值给 y */
return 0;
}
3.3 将引用作为返回值
通过使用引用来替代指针,会使 C++ 程序更容易阅读和维护。C++ 函数可以返回一个引用,方式与返回一个指针类似。
当函数返回一个引用时,则返回一个指向返回值的隐式指针。这样,函数就可以放在赋值语句的左边。
#include <iostream>
using namespace std;
double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};
double& setValues(int i) {
double& ref = vals[i];
return ref; // 返回第 i 个元素的引用,ref 是一个引用变量,ref 引用 vals[i]
}
// 要调用上面定义函数的主函数
int main ()
{
cout << "改变前的值" << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
}
setValues(1) = 20.23; // 改变第 2 个元素
setValues(3) = 70.8; // 改变第 4 个元素
cout << "改变后的值" << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
}
return 0;
}
当返回一个引用时,要注意被引用的对象不能超出作用域。所以返回一个对局部变量的引用是不合法的,但是,可以返回一个对静态变量的引用。
int& func() {
int q;
//! return q; // 在编译时发生错误
static int x;
return x; // 安全,x 在函数作用域外依然是有效的
}
4、重载
4.1 函数重载
在同一个作用域内,可以声明几个功能类似的同名函数,这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同,不能仅通过返回类型的不同来重载函数。
在下面的例子中,同名函数 print() 被用于输出不同的数据类型:
#include <iostream>
using namespace std;
class printData
{
public:
void print(int i) {
cout << "整数为: " << i << endl;
}
void print(double f) {
cout << "浮点数为: " << f << endl;
}
void print(char c[]) {
cout << "字符串为: " << c << endl;
}
};
int main(void)
{
printData pd;
// 输出整数
pd.print(5);
// 输出浮点数
pd.print(500.263);
// 输出字符串
char c[] = "Hello C++";
pd.print(c);
return 0;
}
4.2 运算符重载
在C++中,运算符重载是一个允许程序员自定义各种运算符(如 + , - , == , != 等)在自定义类型(类或结构体)上的行为的特性。这意味着你可以定义类似于内置类型的运算符行为,使你的自定义类型更加直观和易于使用。
基本原则:
(1)不可以创建新的运算符:只能重载已经存在的运算符。
(2)至少有一个操作数是用户定义的类型:不能重载两个基本类型的运算符。
(3)不能更改运算符的优先级:重载的运算符保持其原有的优先级和结合性。
示例1:假设我们有一个Person 类,我们可以重载 == 运算符来实现两个Person是否相等的判断。
#include <iostream>
using namespace std;
class Person
{
public:
string name;
int inNumberTail;
bool operator==(Person pTmp);
};
bool Person::operator==(Person pTmp){
return pTmp.name == name && pTmp.inNumberTail == inNumberTail;
}
int main()
{
//假设我们认定名字和身份证尾号6位一样的两个对象是同一个人!
Person p1;
p1.name = "张三";
p1.inNumberTail = 412508;
Person p2;
p2.name = "张三";
p2.inNumberTail = 412508;
bool ret = p1 == p2;
cout << ret << endl;
return 0;
}
示例2:假设我们有一个简单的 Point 类,我们可以重载 + 运算符来实现两个点的加法。
class Point {
public:
int x, y;
// 重载 + 运算符
Point operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
};
int main() {
Point p1;
p1.x = 1;
p1.y = 2;
Point p2;
p2.x = 2;
p2.y = 3;
Point p3 = p1 + p2; // 使用重载的 + 运算符
std::cout << "p3.x: " << p3.x << ", p3.y: " << p3.y << std::endl; // 输出p3.x: 4, p3.y: 6
return 0;
}
在示例2中,operator+ 被重载为一个成员函数,接受一个 Point 类型的常量引用作为参数,并返回两个点相加的结果。
这里的 const 表明这个 operator+ 函数不会修改调用它的 Point 对象。它只是读取对象的 x 和 y成员,并返回一个新的 Point 对象。这种做法在设计类的时候是很有用的,因为它可以确保某些函数不会意外地改变对象的状态,同时也使得这个函数可以在常量对象上被调用。
注:(1)一致性:重载的运算符应与其原始意图和常见用法保持一致。例如, + 运算符通常应该实现加法,而不是其他意外的操作。
(2)复杂性:过度使用运算符重载可能导致代码难以理解和维护。确保它们的使用直观且合理。
运算符重载是C++中提高代码可读性和表达力的强大工具,但需要谨慎使用,以保证代码的清晰性和维护性。