C++类和对象
1、类的定义
1.1类定义格式
-
class为定义类的关键字,Stack为类的名字,{}中为类的主体,注意类定义结束后面分号不能省略。类体中的内容称为类的成员;类的变量称为类的属性或成员变量;类中的函数成为类的方法或成员函数。有点类似于typedef struct,不过成员中还可以有函数。例如下文实现一个栈:
#include<iostream> 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; }
-
C++中struct也可以定义类,C++兼容C中struct的用法,同时将struct升级成了一个类,明显的区别是struct中可以定义函数,一般情况下还是用class定义类。
struct Person{ public: void Init(string name,int age){//将传入的参数赋值给结构体中的成员 strcpy(_name,name); _age=age; } string _name; int _age; };
在主函数中使用的时候可以直接使用类型名,类名代表类型,如下:
Person p1; p1.Init("zhangsan",18);
1.2访问限定符
-
C++⼀种实现封装的方式,用类将对象的属性与方法结合在⼀块,让对象更加完善,通过访问权限选择性的将其接口提供给外部的用户使用。
-
访问限定符一共有public、private、protected三个,public修饰的成员在类外可以直接被访问;protected和private修饰的成员在类外不能直接被访问,protected可以简单理解和private差不多,会在c++的继承中才体现出区别。
-
class定义成员没有被访问限定符修饰时默认为private,struct默认为public。
-
**访问限定符的作用域:**从当前限定符开始,一直到下一个访问限定符出现,当后面无访问限定符则到类的 } 即类的结束时。
下面以一个类举例:
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);
_capacity=n;
top=0;
}
int main(){
Stack st;
st.Init();
return 0;
}
在上述代码中,Init函数是在public下的,是可以在类外被访问,因此可以在类外编辑函数内容,但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、实例化
2.1实例化概念
-
用类类型在物理内存中创建对象的过程,成为实例化出对象。
例如将上面1.3中的stack实例化为一个对象:
Stack stk1;
-
类是对象进行⼀种抽象描述,是⼀个模型⼀样的东西,限定了类有哪些成员变量,这些成员变量只是声明,没有分配空间,用类实例化出对象时,才会分配空间。
3、this指针
#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(2024, 3, 31);
d1.Print();
d2.Init(2024, 7, 5);
d2.Print();
return 0;
}
- Date类中有 Init 与 Print 两个成员函数,函数体中没有关于不同对象的区分,那当d1调用Init和 Print函数时,该函数是如何知道应该访问的是d1对象还是d2对象呢?那么这里就要看到C++给了 ⼀个隐含的this指针解决这里的问题
- 编译器编译后,类的成员函数默认都会在形参的第一个位置,增加一个当前类类型的指针,叫做this指针,比如Date类的Init的真实原型应该是
void Init(Date* const this,int year,int month,int day)
- 类的成员函数中访问成员变量,本质都是通过this指针访问的,如Init函数中给_year赋值,
this->_year=year;
下面以几个例题来配合理解:
-
下面程序编译运行结果是():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
-
下面程序编译运行结果是():A 编译报错 B 运行崩溃 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
解析:在正常调用的时候,本质上调用过程应该如下:
A a1; a1.Print(&a1);
但是现在只创建了一个A*类型的空指针p,p->Print()语句中将p传递给()中的this指针,this指针为空,第一题中的Print函数没有访问类中的成员,只执行打印操作,不会报错。但是在第二题中,Print()函数访问了类下的_a成员,访问成员的本质应该是
this->_a
但是主函数中并未将A类型实例化,也就是并没有存储成员数据,而且this是个空指针,因此无法访问this->a,会报错。 -
this指针存在内存哪个区域的 () A. 栈 B.堆 C.静态区 D.常量区 E.对象里面
选A前面有说过this指针作为一个形参传递,而形参是一种局部变量,存储在栈帧里面。