class className { };这是类的基本定义,和C语言结构体定义类似, C++中struct也可以定义类,C++兼容C中struct的用法,同时struct升级成了类,明显的变化是struct中可以定义函数,⼀般情况下我们还是推荐⽤class定义类
_进行标识inline// 定义一个类
class stuInfo
{
public:
// 将实现接口公开
// 成员函数
private:
// 将信息数据隐藏
unsigned int _age;
unsigned int _numArr[8];
char* _nameArr[10];
};再比如:实现一个简单的栈
// 实现一个简单的栈
class Stack
{
public:
// 成员函数
void Init(int n = 4)
{
_valArr = (int*)malloc(sizeof(int) * n);
if (_valArr == nullptr)
{
perror("malloc err!");
return;
}
_top = 0;
_capacity = n;
}
void push(int inPut = 1)
{
_valArr[_top++] = inPut;
}
size_t top()
{
assert(_top > 0);
return _valArr[_top - 1];
}
void Destroy()
{
assert(_valArr);
free(_valArr);
_valArr = nullptr;
_capacity = _top = 0;
}
private:
size_t _top;
size_t _capacity;
int* _valArr;
};
int main()
{
Stack st;
st.Init(12);
st.push(0);
st.push(1);
st.push(2);
st.push(3);
st.top();
st.Destroy();
return 0;
}
在上面的代码中,已经出现了public和private
public:修饰的成员在类外可以访问
private/protected:受保护,修饰的成员不能在类外访问}结束class的类没有public默认都是被保护的,struct的类默认是公开的,是为了向下兼容C :: 域作用操作符指明成员属于哪个类域。Init如果不指定类域Stack,那么编译器就把Init当成全局函数,那么编译时,找不到_valArr等成员的声明/定义在哪里,就会报错。指定类域Stack,就是知道Init是成员函数,当前域找不到的_valArr等成员,就会到类域中去查找。class Stack
{
public:
// 成员函数
void Init(int n = 4);
void push(int inPut = 1);
size_t top();
void Destroy();
private:
size_t _top;
size_t _capacity;
int* _valArr;
};
// 声明和定义分离
// 缺省参数声明给
void Stack::Init(int n)
{
_valArr = (int*)malloc(sizeof(int) * n);
if (_valArr == nullptr)
{
perror("malloc err!");
return;
}
_top = 0;
_capacity = n;
}
void Stack::push(int inPut)
{
_valArr[_top++] = inPut;
}
size_t Stack::top()
{
assert(_top > 0);
return _valArr[_top - 1];
}
void Stack::Destroy()
{
assert(_valArr);
free(_valArr);
_valArr = nullptr;
_capacity = _top = 0;
}
int main()
{
Stack st;
st.Init(12);
st.push(0);
st.push(1);
st.push(2);
st.push(3);
st.top();
st.Destroy();
return 0;
}class Date
{
public:
void Init(size_t year, size_t month, size_t day)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
// 声明
size_t _year;
size_t _month;
size_t _day;
};
int main()
{
// 实例化
Date d1;
d1.Init(2025, 7, 3);
d1.Print();
return 0;
}


分析一下类对象里的成员:

Person实例化出两个对象p1和p2,p1和p2都有各自独立的成员变量_name _gender _age存储各自的数据,但是p1和p2成员函数PrintPersonInfo SetPersonInfo的指针却是一样的

这里调用的是同一个函数,如果用Person实例化出100个对象,按照方式一,指针存储相同的地址存储100次,太浪费了,所以采用方式二,将函数地址存到公共代码区,所以得出结论:成员函数不参与类大小的计算
类的大小的计算遵循结构体内存对齐原则:
// 计算⼀下A/B/C实例化的对象是多⼤?
// 8
class A
{
public:
void Print()
{
cout << _ch << endl;
}
private:
char _ch;
int _i;
};
// 1
class B
{
public:
void Print()
{
//...
}
};
// 1
class C
{
};
int main()
{
A a;
B b;
C c;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
cout << sizeof(c) << endl;
return 0;
}注意:空类大小默认为1字节,这个字节不存储有效数据,只表明类被定义了
Date类实例化出两个对象d1和d2,d1和d2怎么区分成员函数Print()和Init()来进行调用呢,C++引入了this指针来解决this指针即void Print(Date* const this),注意:这个指针无法修改;形参和实参的位置不能显式写出 this指针,编译器会默认生成,但是在函数体内可以显式写this指针来实现,例如:this->_year = yearusing namespace std;
class Date
{
public:
// void Init(Date* const this, size_t year, size_t month, size_t day)
void Init(size_t year, size_t month, size_t day)
{
// this->_year = year
_year = year;
_month = month;
_day = day;
}
// void Print(Date* const this)
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
size_t _year;
size_t _month;
size_t _day;
};
int main()
{
Date d1;
d1.Init(2025, 7, 5);
Date d2;
d2.Init(2008, 1, 1);
d1.Print();
d2.Print();
}再来看两个程序,问该程序是正常运行,还是编译错误,还是运行崩溃
#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;
}分析:A类型实例化出一个p指针,这个指针为空,然后p指针调用Print(),回到Print()执行函数体,正常输出A::Print(),这里没有解引用空指针,所以程序正常运行
#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;
}分析:A类型实例化出一个p指针,这个指针为空,然后p指针调用Print(),回到Print()执行函数体,正常输出A::Print(),而继续下一行p->_a空指针解引用了,运行崩溃

this指针存在栈区和ecx寄存器中