C++类和对象(上)

类的定义

类定义格式

class为定义类的关键字,{}中为类的主体,注意类定义结束时后面分号不能省 略。类体中内容称为类的成员:类中的变量称为类的属性或成员变量;类中的函数称为类的方法或 者成员函数。

为了区分成员变量,⼀般习惯上成员变量会加⼀个特殊标识,如成员变量前面或者后面加_或者m 开头,注意C++中这个并不是强制的,只是⼀些惯例,具体看公司的要求。

C++中struct也可以定义类,C++兼容C中struct的用法,同时struct升级成了类,明显的变化是 struct中可以定义函数,⼀般情况下我们还是推荐用class定义类。

定义在类面的成员函数默认为inline。

c++中不需要再用typedef,结构名就可以定义类型了。

struct ListNodeCPP
{
 void Init(int x)
 {
 next = nullptr;
 val = x;
 }
 ListNodeCPP* next;
 int val;
};

访问限定符

C++⼀种实现封装的方式,用类将对象的属性与方法结合在⼀块,让对象更加完善,通过访问权限 选择性的将其接口提供给外部的用户使用。

public修饰的成员在类外可以直接被访问protectedprivate修饰的成员在类外不能直接被访问

访问权限作用域从该访问限定符出现的位置开始直到下⼀个访问限定符出现时为止,如果后面没有 访问限定符,作用域就到 } 即类结束。

class定义成员没有被访问限定符修饰时默认为private,struct默认为public

⼀般成员变量都会被限制为private/protected,需要给别人使用的成员函数会放为public。

类域

类定义了⼀个新的作用域,类的所有成员都在类的作用域中,在类体外定义成员时,需要用::作用域操作符指明成员属于哪个类域。

类域影响的是编译的查找规则,下⾯程序中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;
}

实例化

实例化概念

在类中定义成员时,只是声明该成员而已,在类中该成员是不占空间的,需要在主函数中创建类对象才能创建。

⽤类类型在物理内存中创建对象的过程,称为类实例化出对象。

类是对象进行一种抽象描述,是⼀个模型⼀样的东西,限定了类有哪些成员变量,这些成员变量只 是声明,没有分配空间,用类实例化出对象时,才会分配空间。

C++规定类实例化的对象也要符合内存对齐的规则

this指针

Date类中有Init与Print两个成员函数,函数体中没有关于不同对象的区分,那当d1调用Init和 Print函数时,该函数是如何知道应该访问的是d1对象还是d2对象呢?那么这里就要看到C++给了 ⼀个隐含的this指针解决这里的问题

编译器编译后,类的成员函数默认都会在形参第⼀个位置,增加⼀个当前类类型的指针,叫做this 指针。比如Date类的Init的真实原型为,void Init(Date* const this, int year, int month, int day)

C++规定不能在实参和形参的位置显示的写this指针(编译时编译器会处理),但是可以在函数体内显示使用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)
 {
 _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(2024, 3, 31);
 d1.Print();
 d2.Init(2024, 7, 5);
 d2.Print();
 return 0;
}

C++和C语言实现Stack对比

通过下面两份代码对⽐,我们发现C++实现Stack形态上还是发生了挺多的变化,底层和逻辑上没啥变 化。

C++中数据和函数都放到了类里面,通过访问限定符进行了限制,不能再随意通过对象直接修改数 据,这是C++封装的⼀种体现,这个是最重要的变化。这⾥的封装的本质是⼀种更严格规范的管 理,避免出现乱访问修改的问题。。

C++中有⼀些相对方便的语法,比如Init给的缺省参数会⽅便很多,成员函数每次不需要传对象地 址,因为this指针隐含的传递了,方便了很多,使⽤类型不再需要typedef用类名就很方便

C语言实现stcak代码

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDataType;
typedef struct Stack
{
 STDataType* a;
 int top;
 int capacity;
}ST;
void STInit(ST* ps)
{
 assert(ps);
 ps->a = NULL;
 ps->top = 0;
 ps->capacity = 0;
}
void STDestroy(ST* ps)
{
 assert(ps);
 free(ps->a);
 ps->a = NULL;
 ps->top = ps->capacity = 0;
}
void STPush(ST* ps, STDataType x)
{
 assert(ps);
if (ps->top == ps->capacity)
 {
 int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
 STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * 
sizeof(STDataType));
 if (tmp == NULL)
 {
 perror("realloc fail");
 return;
 }
 ps->a = tmp;
 ps->capacity = newcapacity;
 }
 ps->a[ps->top] = x;
 ps->top++;
}
bool STEmpty(ST* ps)
{
 assert(ps);
 return ps->top == 0;
}
void STPop(ST* ps)
{
 assert(ps);
 assert(!STEmpty(ps));
 ps->top--;
}
STDataType STTop(ST* ps)
{
 assert(ps);
 assert(!STEmpty(ps));
 return ps->a[ps->top - 1];
}
int STSize(ST* ps)
{
 assert(ps);
 return ps->top;
}

C++实现stack代码

#include<iostream>
using namespace std;
typedef int STDataType;
class Stack
{
public:
 // 成员函数 
 void Init(int n = 4)
 {
 _a = (STDataType*)malloc(sizeof(STDataType) * n);
 if (nullptr == _a)
 {
 perror("malloc申请空间失败");
 return;
 }
 _capacity = n;
 _top = 0;
 }
void Push(STDataType x)
 {
 if (_top == _capacity)
 {
 int newcapacity = _capacity * 2;
 STDataType* tmp = (STDataType*)realloc(_a, newcapacity * 
sizeof(STDataType));
 if (tmp == NULL)
 {
 perror("realloc fail");
 return;
 }
 _a = tmp;
 _capacity = newcapacity;
 }
 _a[_top++] = x;
 }
 void Pop()
 {
 assert(_top > 0);
 --_top;
 }
 bool Empty()
 {
 return _top == 0;
 }
 int Top()
 {
 assert(_top > 0);
 return _a[_top - 1];
 }
 void Destroy()
 {
 free(_a);
 _a = nullptr;
 _top = _capacity = 0;
 }
private:
// 成员变量 
 STDataType* _a;
 size_t _capacity;
 size_t _top;
};

  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值