C++ 的类(Class)是面向对象编程(OOP)的核心,用于封装数据和函数,实现封装、继承、多态三大特性。
重要程度排序:
1. 类与对象 基础语法(最高频)
2. 构造函数& 析构函数(必考)
3. 封装(OOP 三大特性之一)
4. this 指针(高频选择 / 判断题)
5. 静态成员 static(重点)
6. 继承(六级核心难点)
7. 多态 基础(仅入门考点,不考虚函数复杂用法)
8. 特殊成员 & 补充考点
- 拷贝构造函数(概念、触发场景:对象赋值、传参、返回对象)
- 常成员
const:常成员函数、常对象规则 - 友元
friend(概念、简单语法,判断权限突破规则)
一、基础语法
1. 类的定义格式
class 类名 {
// 访问修饰符
public:
// 公有成员:外部可访问
private:
// 私有成员:仅类内部访问(默认)
protected:
// 保护成员:类内部 + 子类可访问
// 成员变量(属性)
// 成员函数(方法)
}; // 末尾分号不能漏访问权限说明
-
private(默认):私有,外部、子类都无法访问 -
public:公有,任意地方都能访问 -
protected:保护,子类可访问,外部不可访问
2. 最简单示例:定义 & 使用类
#include <iostream>
using namespace std;
// 定义一个学生类
class Student {
// 私有成员变量(默认 private)
string name;
int age;
public:
// 成员函数:设置信息
void setInfo(string n, int a) {
name = n;
age = a;
}
// 成员函数:打印信息
void showInfo() {
cout << "姓名:" << name << ",年龄:" << age << endl;
}
};
int main() {
// 创建类的【对象】(实例化)
Student s1;
// 调用公有成员函数
s1.setInfo("张三", 18);
s1.showInfo();
return 0;
}二、构造函数 & 析构函数
1. 构造函数(Constructor)
- 作用:创建对象时自动调用,初始化成员变量
- 规则:
- 函数名 = 类名
- 无返回值(连
void都不写) - 可重载(多个构造函数)
写法 1:普通构造函数
class Student {
private:
string name;
int age;
public:
// 构造函数
Student(string n, int a) {
name = n;
age = a;
}
void showInfo() {
cout << name << " " << age << endl;
}
};
int main() {
// 创建对象时直接传参初始化
Student s1("李四", 20);
s1.showInfo();
return 0;
}写法 2:初始化列表(推荐,效率更高)
Student(string n, int a) : name(n), age(a) {}默认构造函数
无参数的构造函数,编译器默认自动生成(如果没写任何构造函数):
// 无参构造
Student() : name(""), age(0) {}补充:拷贝构造函数
- 作用:用已有对象初始化新对象,属于构造函数的一种重载
- 格式:
类名(const 类名 & 对象) - 触发场景:对象初始化、函数传参(值传递)、函数返回对象
- 编译器会默认生成简易拷贝构造函数
class Student {
private:
string name;
public:
// 普通构造
Student(string n) : name(n) {}
// 拷贝构造函数
Student(const Student &s) {
name = s.name;
cout << "调用拷贝构造函数" << endl;
}
void show() {
cout << name << endl;
}
};
int main() {
Student s1("小明");
Student s2(s1); // 触发拷贝构造
s2.show();
return 0;
}2. 析构函数(Destructor)
- 作用:对象销毁时自动调用,释放资源(堆内存、文件等)
- 规则:
- 函数名:
~类名 - 无参数、无返回值、不能重载
- 函数名:
class Student {
public:
// 析构函数
~Student() {
cout << "对象被销毁" << endl;
}
};三、成员函数:类内 / 类外实现
1. 类外定义成员函数(常用,代码解耦)
使用作用域限定符 ::
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
public:
// 函数声明
void setName(string n);
void showName();
};
// 类外实现成员函数
void Student::setName(string n) {
name = n;
}
void Student::showName() {
cout << "名字:" << name << endl;
}
int main() {
Student s;
s.setName("王五");
s.showName();
return 0;
}补充:常成员函数 & 常对象
const 修饰成员函数:保证函数不会修改类的成员变量
常对象:const 类名 对象,只能调用常成员函数
语法:返回值 函数名(参数) const
class Student {
private:
int age;
public:
Student(int a) : age(a) {}
// 常成员函数
void show() const {
cout << age << endl;
}
};
int main() {
const Student s(18); // 常对象
s.show(); // 常对象只能调用常成员函数
return 0;
}四、封装(类的核心特性)
把成员变量设为 private,通过 public 函数读写,禁止外部直接修改数据,保证数据安全。
class Person {
private:
int age; // 私有变量,外部不能直接访问
public:
// 读:getter
int getAge() {
return age;
}
// 写:setter(可加数据校验)
void setAge(int a) {
if (a > 0 && a < 150) { // 合法性判断
age = a;
} else {
cout << "年龄不合法" << endl;
}
}
};
int main() {
Person p;
p.setAge(25);
cout << p.getAge() << endl;
p.setAge(200); // 触发校验
return 0;
}补充:友元 friend(GESP6 级概念 + 语法考点)
- 作用:突破访问权限限制,让外部函数 / 类访问类的
private成员 - 分类:友元函数、友元类
- 注意:破坏封装,考试以判断、代码阅读为主
class Person {
private:
int age = 20;
// 声明全局函数为友元
friend void printAge(Person &p);
};
// 友元函数,可以访问私有成员
void printAge(Person &p) {
cout << p.age << endl;
}
int main() {
Person p;
printAge(p);
return 0;
}五、this 指针
- this 指向当前对象本身,是隐含指针
- 用途:区分成员变量和局部 / 形参重名
- 补充考点:静态成员函数没有 this 指针
class Person {
private:
int age;
public:
void setAge(int age) {
// this->age 代表对象的成员变量
this->age = age;
}
};六、静态成员 static
1. 静态成员变量
- 属于整个类,不属于某个对象,所有对象共享同一份数据
- 必须类内声明,类外初始化
class Student {
public:
static int num; // 类内声明
};
int Student::num = 0; // 类外初始化
int main() {
Student s1, s2;
Student::num++; // 推荐:通过类名访问
cout << Student::num << endl;
return 0;
}2. 静态成员函数
- 只能访问静态成员,不能使用
this指针 - 调用方式:
类名::函数名()
class Student {
public:
static int num;
static void add() {
num++;
}
};
int Student::num = 0;
int main() {
Student::add();
cout << Student::num << endl;
return 0;
}七、继承(Class 进阶)
一个类(子类)继承另一个类(父类),复用代码。
语法:class 子类 : 继承方式 父类
继承方式:public / private / protected(常用 public)
// 父类
class Person {
public:
string name;
void say() {
cout << "人类" << endl;
}
};
// 子类 Student 公有继承 Person
class Student : public Person {
public:
int id;
void study() {
cout << "学习" << endl;
}
};
int main() {
Student s;
s.name = "小明"; // 继承父类成员
s.say();
s.study();
return 0;
}补充:继承中构造与析构顺序(六级高频考点)
- 构造顺序:先调用父类构造 再调用子类构造
- 析构顺序:先调用子类析构 再调用父类析构
补充:多态、虚函数 virtual、抽象类(GESP6 级核心难点)
1. 虚函数与多态
- 多态:父类指针 / 引用指向子类对象,调用子类重写的函数
- 关键字
virtual:声明虚函数,实现动态多态
#include <iostream>
using namespace std;
// 父类
class Animal {
public:
// 虚函数
virtual void cry() {
cout << "动物叫声" << endl;
}
};
// 子类
class Cat : public Animal {
public:
// 重写虚函数
void cry() {
cout << "喵喵叫" << endl;
}
};
int main() {
Animal *a = new Cat(); // 父类指针指向子类对象
a->cry(); // 触发多态,调用子类方法
delete a;
return 0;
}2. 纯虚函数 & 抽象类(概念题必考)
- 纯虚函数格式:
virtual 返回值 函数名() = 0; - 包含纯虚函数的类称为抽象类
- 规则:抽象类不能实例化对象,只能被继承
class Animal {
public:
// 纯虚函数
virtual void cry() = 0;
};
// 子类必须重写所有纯虚函数
class Dog : public Animal {
public:
void cry() {
cout << "汪汪叫" << endl;
}
};
int main() {
// Animal a; // 错误:抽象类无法创建对象
Dog d;
d.cry();
return 0;
}八、常用总结
- 类 = 属性 (成员变量) + 行为 (成员函数)
- 访问权限:
private(默认) >protected>public - 构造函数:初始化,对象创建自动调用;析构函数:释放资源,对象销毁自动调用
- 封装:变量私有,通过公有函数访问
static成员属于类,全局共享- 继承实现代码复用,
public继承最常用
完整综合案例
#include <iostream>
#include <string>
using namespace std;
class Dog {
private:
string breed; // 品种
int age; // 年龄
public:
// 构造函数-初始化列表
Dog(string b, int a) : breed(b), age(a) {}
// 成员函数
void bark() {
cout << breed << " 在汪汪叫" << endl;
}
void show() {
cout << "品种:" << breed << ",年龄:" << age << endl;
}
// 析构函数
~Dog() {
cout << "狗狗对象销毁" << endl;
}
};
int main() {
Dog dog1("金毛", 3);
dog1.show();
dog1.bark();
return 0;
}