1.类的定义
1.1类的定义格式
• class为定义类的关键字,后跟一个类的名字,{}中为类的主体,注意类定义结束时后⾯分号不能省 略。类体中内容称为类的成员:类中的变量称为类的属性或成员变量;类中的函数称为类的⽅法或 者成员函数。
• 为了区分成员变量,⼀般习惯上成员变量会加⼀个特殊标识,如成员变量前⾯或者后⾯加_或者m 开头,注意C++中这个并不是强制的,只是⼀些惯例,具体看公司的要求。
• C++中struct也可以定义类,C++兼容C中struct的⽤法,同时struct升级成了类,明显的变化是 struct中可以定义函数,⼀般情况下我们还是推荐⽤class定义类。
• 定义在类⾯的成员函数默认为inline。
通俗点来说说呢,类就是struct的升级plus版本,将成员由c语言中struct只能够是变量增添了函数,下面我们写一个简单的日期类
class Date
{
public:void Init(int year, int month, int day){_year = year;_month = month;_day = day;}
private:// 为了区分成员变量,⼀般习惯上成员变量 // 会加⼀个特殊标识,如_ 或者 m开头 int _year; // year_ m_yearint _month;int _day;
};
int main()
{Date d;d.Init(2024, 3, 31);return 0;
}
可以看出相较于c语言中的struct,类可以以函数作为成员
1.2访问限定符
我们在上述代码中,我们看到了其中有public和private,其中是什么意思呢?
• C++⼀种实现封装的⽅式,⽤类将对象的属性与⽅法结合在⼀块,让对象更加完善,通过访问权限 选择性的将其接⼝提供给外部的用户使⽤。
• public修饰的成员在类外可以直接被访问;protected和private修饰的成员在类外不能直接被访 问,protected和private是⼀样的,以后继承章节才能体现出他们的区别。
• 访问权限作⽤域从该访问限定符出现的位置开始直到下⼀个访问限定符出现时为⽌,如果后⾯没有 访问限定符,作⽤域就到}即类结束。
• class定义成员没有被访问限定符修饰时默认为private,struct默认为public。
• ⼀般成员变量都会被限制为private/protected,需要给别⼈使⽤的成员函数会放为public
在如图所示我们就可以看到在main函数中,_year并不能被识别,这就是因为_year是被private修饰的
1.3类域
• 类定义了⼀个新的作⽤域,类的所有成员都在类的作⽤域中,在类体外定义成员时,需要使⽤::作 ⽤域操作符指明成员属于哪个类域。
• 类域影响的是编译的查找规则,下⾯程序中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 = n;top = 0;
}
int main()
{Stack st;st.Init();return 0;
}
2.this指针
• Date类中有Init与Print两个成员函数,函数体中没有关于不同对象的区分,那当d1调⽤Init和 Print函数时,该函数是如何知道应该访问的是d1对象还是d2对象呢?那么这⾥就要看到C++给了 ⼀个隐含的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;
}
我们可以看到d1和d2都是同样的参数,那么类是怎么区分d1和d2的呢,这就因为其中存在一个隐形指针this
⽐如Date类的Init的真实原型为
void Init(Date* const this, int year, int month, int day)
C++规定不能在实参和形参的位置显⽰的写this指针(编译时编译器会处理),但是可以在函数体内显 ⽰使⽤this指针。
能够准精确的分辨d1和d2就是因为有this指针
2.1this指针示例
让我们看看下列题目
1.下⾯程序编译运⾏结果是()
A、编译报错 B、运⾏崩溃 C、正常运⾏
#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;
}
答案是C正常运行
在看看下面这题
#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;
}
答案是B运行错误,这是为什么呢?
原因就在于
下面的代码比上面的代码多了一行
cout << _a << endl
其中涉及了成员_a,所以这里进行访问时对空指针进行了解引用,因此报错。
本次的分享就到这里结束,后续会继续分享,感谢阅读!