class为定义类的关键字,Date为类的名字,{}内的是类的内容简称为成员:类中的变量称为成员变量(属性),类中的函数称为成员函数(方法)。类末尾的分号不能省略。定义在类里面的成员函数默认为inline。C++中struct也可以定义类,C++兼容C中struct的用法,同时struct升级成了类,明显的变化是 struct中可以定义函数,⼀般情况下我们还是推荐用class定义类。
class Date
{
public:
void Init(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
private:
//为了区分成员变量,⼀般习惯上成员变量
// 会加⼀个特殊标识,如 _ 或者 m开头
int _year; // year_ m_year
int _month;
int _day;
};
int main()
{
Date d;
d.Init(2024, 3, 31);
return 0;
}c语言结构体中的成员默认是公共的,没有访问限制。而类中的成员(属性和方法)可以具有不同的访问权限,如公共(public)、私有(private)、受保护(protected)等,以控制数据的封装性和安全性。public,private,protected称为访问限定符。
public修饰的成员在类外可以直接被访问;protected和private修饰的成员在类外不能直接被访问,protected和private是⼀样的,以后继承章节才能体现出他们的区别。
访问权限作用域从该访问限定符出现的位置开始直到下⼀个访问限定符出现时为止,如果后⾯没有 访问限定符,作用域就到}即类结束。
class定义成员没有被访问限定符修饰时默认为private,struct默认为public。
⼀般成员变量都会被限制为private/protected,需要给别人使用的成员函数会放为public。
结构体在c语言中不包含方法,只是数据的容器,而类中通常包含方法(函数),这些方法用于操作类的实例数据。
类定义了一个新的作用域,使用类中的属性和方法时,需要使用::(作用域操作符)手动指定使用的成员属于哪个类域。
#include<iostream>
using namespace std;
class Stack
{
public:
// 成员函数
void Init(int n = 4);
private:
// 成员变量
int* array;
size_t capacity;
size_t top;
};
//声明和定义分离,需要指定类域
void Stack::Init(int n)//需要用作用域操作符指定类域
{
array = (int*)malloc(sizeof(int) * n);
if (nullptr == array)
{
perror("malloc申请空间失败");
return;
}
capacity = n;
top = 0;
}
int main()
{
Stack st;
st.Init();
return 0;
}类域影响的是编译的查找规则,如果不指定类域,编译器会默认到全局去查找,找不到就会报错。
#include<iostream>
using namespace std;
class Stack
{
public:
// 成员函数
void Init(int n = 4);
private:
// 成员变量
int* array;
size_t capacity;
size_t top;
};
//声明和定义分离,需要指定类域
void Init(int n)//需要用作用域操作符指定类域
{
array = (int*)malloc(sizeof(int) * n);
if (nullptr == array)
{
perror("malloc申请空间失败");
return;
}
capacity = n;
top = 0;
}
int main()
{
Stack st;
st.Init();
return 0;
}
示例:
#include <iostream>
using namespace std;
// 定义一个简单的类 Car
class Car {
private:
string make;
string model;
int year;
public:
// 构造函数,用于初始化对象的数据成员
Car(string make, string model, int year) {
this->make = make;
this->model = model;
this->year = year;
}
// 成员函数,用于显示车辆信息
void display_info() {
cout << year << " " << make << " " << model << endl;
}
};
int main() {
// 实例化一个 Car 对象
Car my_car("Toyota", "Corolla", 2020);
// 调用对象的成员函数来显示车辆信息
my_car.display_info();
return 0;
}我们在定义类之后用类类型在物理内存中创建对象的过程,称为类实例化出对象。类就相当于一个标准的设计图(在内存中不占空间),一个设计图可以建很多间房子(一个类可以实例化很多对象,实例化的对象在内存中占空间)。
对象的大小的计算和c语言结构体大小的计算很相似。
内存对齐:
• 第⼀个成员在与结构体偏移量为0的地址处。
• 其他成员变量要对齐到某个数字(对齐数)的整数倍的地址处。
• 注意:对齐数=编译器默认的⼀个对齐数与该成员大小的较小值。
• VS中默认的对齐数为8
• 结构体总大小为:最大对齐数(所有变量类型最大者与默认对齐参数取最小)的整数倍。
• 如果嵌套了结构体的情况,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整体大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍。
#include<iostream>
using namespace std;
// 计算⼀下A / B / C实例化的对象是多⼤?
class A
{
public:
void Print()
{
cout << _ch << endl;
}
private:
char _ch;
int _i;
};
class B
{
public:
void Print()
{
//...
}
};
class C
{};
int main()
{
A a;
B b;
C c;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
cout << sizeof(c) << endl;
return 0;
}类的成员函数本身在对象实例化时不会占用对象的空间,因为成员函数只是类的方法定义,不存储在每个对象的实例中。具体来说,成员函数是共享的,它们的代码只会在程序的一个位置定义,并不会重复存储在每个类的对象实例中。在A中对齐数为4一共有两个元素,所以大小为8个字节。

在C++中,空类(即没有显式定义任何成员变量或成员函数)的实例大小确实是1字节。这个大小主要是由于C++标准的要求,即空类的实例不能是大小为0的,这是为了确保每个实例在内存中都有独特的地址。空类的大小为1字节是C++标准的要求,用来确保每个对象实例都有唯一的地址。这种设计决策可以在不同的编译器和平台上保持一致性,使得空类在语义上仍然是一个有效的对象类型。
#include<iostream>
using namespace std;
class Date
{
public:
// void Init(Date* const this, int year, int month, int day)
void Init(int year, int month, int day)
{
//编译报错:error C2106 : “ = ” :左操作数必须为左值
// this = nullptr;
// this->_year = year;
_year = year;
this->_month = month;
this->_day = day;
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
// 这里只是声明,没有开空间
int _year;
int _month;
int _day;
};
int main()
{
// Date类实例化出对象d1和d2
Date d1;
Date d2;
// d1.Init(&d1, 2024, 3, 31);
d1.Init(2024, 3, 31);
d1.Print();
d2.Init(2024, 7, 5);
d2.Print();
return 0;
}d1和d2都是Date实例化出来的对象用d1调出来的Print()和d2调出来的Print()应该是一样的,那么为什么d1调出来的打印的是d1的数据,而d2调出来的打印的是d2的数据呢?
这是因为有this指针存在。d1调用成员函数的时候会给函数传入指向d1的this指针,同d2。
编译器编译后,类的成员函数默认都会在形参第⼀个位置,增加⼀个当前类类型的指针,叫做this 指针。比如Date类的Init的真实原型为, void Init(Date* const this, int year, int month, int day)
类的成员函数中访问成员变量,本质都是通过this指针访问的,如Init函数中给_year赋值,this ->_year = year;
C++规定不能在实参和形参的位置显示的写this指针(编译时编译器会处理),但是可以在函数体内显 示使用this指针。
接下来看下面两个题:
这段代码能成功运行吗?
#include<iostream>
using namespace std;
class A
{
public:
void Print()
{
cout << "A::Print()" << endl;
}
private:
int _a;
};
int main()
{
A* p = nullptr;
p->Print();
return 0;
}成员函数并不储存在对象内,所以p->Print()并没有对p解引用。虽然p是空指针,但是并没有对空指针解引用,所以这段代码可以正常运行。
接下来观察另一段代码,判断是否能正常运行。
#include<iostream>
using namespace std;
class A
{
public:
void Print()
{
cout << "A::Print()" << endl;
cout << _a << endl;
}
private:
int _a;
};
int main()
{
A* p = nullptr;
p->Print();
return 0;
}成员变量储存在对象中,而p是空指针,_a要通过this指针访问,而this是指向p的指针,所以this是空指针,通过空指针去访问_a,程序会崩溃。