一、类定义格式
1、定义
(1)class为定义类的关键字,Stack为类的名字,{}中为类的主体,注意类定义结束时后面分号不能省略。类体中内容称为类的成员;类中的变量称为类的属性或成员变量;类中的函数称为类的方法或者成员函数。
(2)为了区分成员变量,一般习惯上成员变量会加一个特殊标识,如成员变量前面或者后面加_或者m开头,注意C++中这个并不是强制的,只是一些惯例。
(3)定义在类里面的成员函数默认为inline。
#include<iostream>
#include<assert.h>
using namespace std;
class Stack
{
public:
//成员函数
void Init(int n = 4)
{
array = (int*)malloc(sizeof(int) * n);
if (nullptr == array)
{
perror("malloc 申请空间失败");
return ;
}
capacity = n;
top = 0;
}
void Push(int x)
{
//扩容
array[top++] = x;
}
int Top()
{
assert(top > 0);
return array[top-1];
}
void Destroy()
{
free(array);
array = nullptr;
top = capacity = 0;
}
private:
int* array;
size_t capacity;
size_t top;
};
int main()
{
Stack st;
st.Init();
st.Push(1);
st.Push(2);
cout << st.Top() << endl;
st.Destroy();
return 0;
}
#include<iostream>
using namespace std;
class Date
{
public:
void Init(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
private:
//为了区分成员变量,一般习惯上成员变量
//会加一个特殊标识,如year_ m_year(_或m开头).
int _year;
int _month;
int _day;
};
int main()
{
Date d;
d.Init(2024, 3, 31);
return 0;
}
2、访问限定符
(1)C++一种实现封装的方式,用类和对象的属性与方法结合在一块,让对象更加完善,通过访问权限选择性的将其接口提供给外部使用。
(2)public修饰的成员在类外可以直接被访问;protected和private修饰的成员在类外不能直接被访问,protected和private是一样的。(具体区别在后面会讲解的)
(3)访问权限作用域从该访问限定符出现的位置开始直到下一个访问限定符出现时为止,如果后面没有访问限定符,作用域到}即类结束。class定义成员没有被访问限定符修饰时默认为private,struct默认为public。
(4)一般成员变量都会被限制为private/protected,需要给别人使用的成员函数会放为public。
3、类域
(1)类,定义了一个新的作用域,类的所有成员都在类的作用域中,在类外定义成员时,需要使用::作用域操作符指明成员属于哪个类域。
(2)类域影响的是编译的查找规则,下面程序中Init如果不指定类域Stack,那么编译器就把Init当成全局函数,那么编译时,找不到array等的成员的声明/定义在哪里,就会报错。指定类域Stack,就是知道Init是成员函数,当前域找不到array等成员,就会到类域中去查找。
#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 = 0;
top = 0;
}
int main()
{
Stack st;
st.Init();
return 0;
}
4、实例化
(1)用类类型在物理内存中创建对象的过程,称为类实例化出对象。
(2)类是对象进行一种抽象的描述,是一个模型一样的东西,限定了类有哪些成员变量,这些成员变量只是声明,没有分配空间,用类实例化出对象时,才会分配空间。
(3)一个类可以实例化出多个对象,实例化出的对象,占用实际的物理空间,存储类成员变量。(类就像设计图一样,不能存储数据,实例化出的对象分配物理内存存储数据)。
#include<iostream>
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(2014, 3, 21);
d1.Print();
d2.Init(2024, 7, 5);
d2.Print();
return 0;
}
5、对象大小
类实例化出每个对象,都有独立的数据空间,所以对象中肯定包含成员变量,那么成员函数是否包含呢?首先函数被编译后是一段指令,对象中没办法储存,这些指令存储在一个单独的区域(代码段),那么对象中非要储存的话,只能是成员函数的指针,对象中是否有储存指针的必要呢?Date实例化d1和d2俩个对象,d1和d2都有各自独立的成员变量_year/_month/_day存储各自的数据,但是d1和d2的成员函数Init/Print指针却是一样的,存储在对象中就浪费了。如果Date实例化100个对象,那么成员函数指针就重复100次,太浪费了。其实函数指针是不需要存储的,函数指针是一个地址,调用函数被编译成汇编指令,起始编译器在编译链接时就要找到函数的地址,不是在运行时找,只有动态多态是在运行时找,就需要存储地址。
内存对齐规则
(1)第一个成员在与结构体偏移量为0的地址处
(2)其他成员变量要对齐到某个数字(对齐数)的整数倍地址处
(3)注意:对齐数=编译器默认的对齐数与该成员大小的较小值、
(4)vs中默认对齐数是8
(5)结构体的总大小为:最大对齐数(所有变量类型最大者与默认对齐参数取最小)的整数倍。
(6)如果嵌套了结构体的情况,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整体大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍。
#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;
}
上面的程序运行后,我们没有看到成员变量B和C类对象的大小是1,为什么没有成员变量还要给1个字节呢?因为如果一个字节不给,怎么表示对象存在过呢?所以这里给1字节纯粹是为了占位标识对象存在。
6、this指针
#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;
}
运行结果为: