🏷️ 标签:C++、面向对象、类、构造函数、成员函数、封装、继承、多态
📅 更新时间:2025年6月15日
💬 欢迎在评论区留言交流你的理解与疑问!
文章目录
- 前言
- 一、什么是类?
- 二、类的定义
- [三、访问权限 public / private / protected](#三、访问权限 public / private / protected)
- 四、构造函数
- 五、析构函数
- 六、类的友元
- 总结
前言
C++ 入门老是绕不过类与对象 ?构造函数是什么 ?成员函数怎么写 ?访问权限有啥用 ?
今天我们就来学习一下c++中的类
一、什么是类?
在 C++ 中,类 (Class)是一种用户自定义的数据类型,用于将数据和行为封装在一起
类是模板,对象是实例
就像学生是一种类型,而"张三""李四"是一个个对象
二、类的定义
1.基本语法
cpp
class 类名 {
//访问权限!!!!!!!!
private/public/protected:
// 构造函数
// 成员函数
private/public/protected:
// 成员变量
};
2.示例
1.学生类
我们创建一个表示学生的类
cpp
#include <iostream>
using namespace std;
class Student {
public:
//构造函数
Student(string n, int a)
{
name = n;
age = a;
}
//成员函数
void introduce()
{
cout << "我是 " << name << ",今年 " << age << " 岁。" << endl;
}
//私有变量
private:
string name;
int age;
};
2.详解构造函数
构造函数是一个特殊的成员函数,当对象被创建时自动调用,常用于初始化成员变量
要求:
名称必须与类名相同
没有返回值
cpp
Student(string n, int a) {
name = n;
age = a;
}
同样初始化我们可以通过初始化列表的方式进行初始化
cpp
Student(string n, int a):name(n),age(a) {
// 构造函数体可留空,推荐用初始化列表初始化成员变量
}
我们还能用this
指针来初始化
在成员函数中,this
是一个指向当前对象本身的指针
cpp
Student(string n, int a) {
this->name = n;
this->age = a;
}
3.实例化对象
我们可以在主函数中创建一个实例对象,然后调用其中的成员函数
cpp
int main()
{
Student s1("小明",20);
s1.introduce();
return 0;
}
输出:
我是 小明,今年 20 岁。
三、访问权限 public / private / protected
1.各类权限含义
public
:公有成员,可以被所有代码访问。
private
:私有成员,仅能被类的成员函数和友元访问。
protected
:受保护成员,仅能被类的成员函数、友元和派生类访问。
默认是
private
,推荐显式写出访问权限,增强可读性
2.示例
我们还是拿前面的学生类来举例,我们在构造函数中对于成员变量 和成员函数 我们都用public
,这样我们就能在类外访问
到了
正确示范:
cpp
#include<iostream>
using namespace std;
class Student
{
public:
Student(string n, int a)
{
name = n;
age = a;
}
void introduce()
{
cout << "我是 " << name << ",今年 " << age << " 岁。" << endl;
}
public:
string name;
int age;
};
int main()
{
Student s1("小明",20);
s1.introduce();
cout<<"年龄:"<<s1.age<<endl;
cout<<"名字:"<<s1.name<<endl;
return 0;
}
输出:
我是 小明,今年 20 岁。
年龄:20
名字:小明
错误示范:
cpp
private://改为私有
string name;
int age;
此时会报错
cpp
a.cpp:27:24: error: 'int Student::age' is private within this context
27 | cout<<"年龄:"<<s1.age<<endl;
| ^~~
a.cpp:20:13: note: declared private here
20 | int age;
| ^~~
a.cpp:28:24: error: 'std::string Student::name' is private within this context
28 | cout<<"名字:"<<s1.name<<endl;
| ^~~~
a.cpp:19:16: note: declared private here
19 | string name;
| ^~~~
因为类外不可以访问!
四、构造函数
1.类型
默认构造函数 :没有参数的构造函数。
参数化构造函数 :接受参数以初始化对象。
拷贝构造函数 :用一个对象初始化另一个对象。
移动构造函数(C++11):从临时对象"移动"资源
2.示例
cpp
#include<iostream>
using namespace std;
class Student
{
public:
Student(string n, int a)
{
name = n;
age = a;
cout<<"参数化构造函数"<<endl;
}
Student()
{
name="小明";
age=10;
cout<<"默认构造函数"<<endl;
}
Student(const Student &other)
{
name=other.name;
age=other.age;
cout<<"拷贝构造函数"<<endl;
}
Student(Student&& other)
{
name=other.name;
age=other.age;
cout<<"移动构造函数"<<endl;
}
private:
string name;
int age;
};
int main()
{
Student s1;
Student s2("小明",20);
Student s3=s2;
Student s4=move(s3);
return 0;
}
输出:
cpp
默认构造函数
参数化构造函数
拷贝构造函数
移动构造函数
五、析构函数
析构函数(Destructor):在对象生命周期结束时调用,用于释放资源
1.语法
cpp
~ 类名 ()
{
}
2.示例:
cpp
#include<iostream>
using namespace std;
class Student
{
public:
Student(string n, int a)
{
name = n;
age = a;
p=new int(age);//手动开辟 在堆上
}
~Student()
{
delete p;//手动开辟必须手动释放
}
private:
string name;
int age;
int* p;
};
六、类的友元
1.什么是友元
友元(Friend) :可以访问类的私有
和保护成员
的非成员函数或另一个类
2.示例
cpp
#include<iostream>
using namespace std;
class Student
{
public:
Student(string n, int a)
{
name = n;
age = a;
}
~Student()
{
}
private:
string name;
int age;
friend class teacher;//关键声明!!!!!!!!!!!!
void play()
{
cout<<"hi"<<endl;
}
};
class teacher
{
public:
teacher(string name,int age)
{
this->name=name;
this->age=age;
}
~teacher()
{
}
void f( Student& a)
{
a.play();//直接访问私有成员函数
}
private:
string name;
int age;
};
int main()
{
teacher s1("小明",20);
Student s2("小明",20);
s1.f(s2);
return 0;
}
输出:hi
如果没有声明
cpp
friend class teacher;
那么在teacher的成员函数中这样直接访问是错误的
cpp
void f(Student& a)
{
a.play();//直接访问私有成员函数
}
3.友元函数
友元函数: 单个函数可以被声明为友元
这样我们可以在函数内访问私有变量
1.语法
cpp
friend 函数()
{}
然后函数如果要在类外实现的话
不需要
void 类:: 函数名(){}
因为友元函数并不是类的成员函数
2.示例
cpp
#include<iostream>
using namespace std;
class Student
{
public:
Student(string n, int a)
{
name = n;
age = a;
}
~Student()
{
}
friend void play(Student& a) ;//声明友元函数
private:
string name;
int age;
};
void play(Student& a)//类外实现友元函数
{
cout<<a.name<<endl;//调用私有变量
}
int main()
{
Student s2("小明",20);
play(s2);
return 0;
}
输出:小明
总结
类(Class)是 C++ 中封装数据和行为的基本单位,是面向对象编程
的核心概念。
对象是类的实例,通过构造函数进行初始化,支持默认构造、参数构造、拷贝构造和移动构造。
访问权限决定了类成员的访问范围:public(公有)、private(私有)、protected(保护),良好的封装性是面向对象设计的重要原则。
构造函数 负责初始化对象,析构函数负责资源释放,两者构成对象生命周期管理的基础。
友元(函数或类) 是类提供的一种特殊机制,允许非成员函数或其他类访问私有和保护成员
友元函数不是类的成员函数,定义时不需要加类名作用域限定符
如果你觉得本文对你有帮助,不妨点赞 + 收藏 + 关注,更多 C++ 系列教程将持续更新 🔥!