#include
using namespace std;
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;
}
#include
using namespace std;
// C++将struct升级成了类
// 1、类⾥⾯可以定义函数
// 2、struct名称就可以代表类型
// C++兼容C中struct的⽤法
typedef struct ListNodeC
{
struct ListNodeC* next;
int val;
}LTNode;
// 不再需要typedef,ListNodeCPP就可以代表类型
struct ListNodeCPP
{
void Init(int x)
{
next = nullptr;
val = x;
}
ListNodeCPP* next;
int val;
};
int main() {
return 0;
}
访问限定符包括:private、public、protected
#include
using namespace std;
class Stack
{
public:
// 成员函数
void Init(int n = 4);
private:
int* array;
size_t capacity, 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
using namespace std;
class Date
{
public:
void Init(int year, int month, int day)
{
_year = year;
_month = month;
_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(2024, 3, 31);
d1.Print();
d2.Init(2024, 7, 5);
d2.Print();
return 0;
}
类实例化出的每个对象,都有独⽴的数据空间,但对象中只包含成员变量的空间(因为不同的对象,成员变量的值通常是不同的)而不包含成员函数,因为后者作为函数,只需要分配一份空间就能保证复用性,为每一个对象都分配空间则会造成浪费
实际上,函数被编译后是⼀段指令,对象中没办法存储,这些指令存储在⼀个单独的区域(代码段),那么对象中⾮要存储的话,只能是成员函数的指针。但同样的,对于每一个对象来说,函数的指针也都是一样的,也就不必再浪费空间存储了
再深入一些的讲,函数指针是⼀个地址,调⽤函数被编译成汇编指令[call 地址],其实编译器在编译链接时,就要找到函数的地址,而不是在运⾏时找(当然也有例外,动态多态是在运行时找的,就需要存储函数地址,这个之后会说)所以总而言之,对象的存储方式大概如下图所示
接着看看如下程序,并猜想他的运行结果
// 运行结果()
// A、编译报错 B、运行崩溃 C、正常运行
#include
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是个空指针,没有指向一个实例化对象,怎么能够调用函数?
不过有了上述的铺垫,现在理解应当就比较容易了,因为调用函数的底层操作并不是在访问一个空地址,函数体中也没有这样的操作,所以才能够正常运行,如果像下面这样就会运行崩溃了
#include
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();
// 运行时异常
// cout << p->_a << endl;
return 0;
}
另外,C++规定类实例化的对象也要符合内存对⻬的规则,这与C语言结构体的内存对齐规则是一样的
内存对齐规则
#include
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;
}
上述程序运行后,外面发现B和C的实例化对象明明没有成员变量,大小却为1,这是为了占位标识对象存在
另外提一嘴,为什么要应用内存对齐规则,这不是造成了空间浪费吗?
这里需要知道一个前提,CPU在读取数据的时候,不是能从任意位置读取的,比方说一次只能读4个字节(具体读多少,不同机器不同,这里仅举例)那么就不能从第3个字节的位置开始读
假设一个结构体存储了一个char类型的变量和一个int类型的变量,现在要读取该int类型的变量,在两种不同的存储方式下,读取效率是不一样的
所以,内存对齐规则其实是一种空间换时间的做法
void Init(Date* const this, int year, int month, int day)
this->_year = year;
#include
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;
Date d2;
// d1.Init(&d1, 2024, 3, 31);
d1.Init(2024, 3, 31);
d1.Print();
d2.Init(2024, 7, 5);
d2.Print();
return 0;
}
⾯向对象三⼤特性:封装、继承、多态