02.类和对象(上)

1.类的定义

(1)类的定义格式

类的定义应包含以下几部分:关键字,类的名字,类的主体
类的主体包括以下几个方面:类的方法(函数),类的的属性(变量),这些又被称为类的成员

代码示例:

#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;  
}  

class Date  
{ 
public:  
	void Init(int year, int month, int day)  
	{  
		_year = year;  
		_month = month;  
		_day = day;  
	}   
private:  
	// 为了区分成员变量,⼀般习惯上成员变量  
	// 会加⼀个特殊标识,如_ 或者 m开头  
	int _year; // year_ m_year  
	int _month;  
	int _day;  
};  
int main()  
{  
	Date d;  
	d.Init(2024, 3, 31);  
	return 0;  
}

(2)Class和Struct

class为定义类的关键字,Stack为类的名字,{}中为类的主体,注意类定义结束时后⾯分号不能省略,另外要注意成员变量的命名格式。像这些定义在类中的函数默认是inline,但编译器不一定会展开。
C++中struct也可以定义类,C++兼容C中struct的用法,同时struct升级成了类,明显的变化是struct中可以定义函数,⼀般情况下我们还是推荐用class定义类。
代码示例:

#include<iostream>  
using namespace std;  
// C++升级struct升级成了类  
// 1、类⾥⾯可以定义函数  
// 2、struct名称就可以代表类型  
// C++兼容C中struct的⽤法  
typedef struct ListNodeC  
{  
	struct ListNodeC* next;  
	int val;  
}LTNode;  
// 不再需要typedef,ListNodeCPP就可以代表类型  
struct ListNodeCPP  
{  
	void Init(int x)  
	{  
		next = nullptr;  
		val = x;  
	} 
	ListNodeCPP* next;//不用再写成struct ListNodeCPP* next  
	int val;  
};  
int main()  
{  
	return 0;  
}

从代码可以看出C++不仅兼容C中struct的定义方式,而且可以使用更简便的定义方式:不用typedef也可以直接使用结构体名称进行定义和实例化。

(3)访问限制符

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

在这里插入图片描述public修饰:成员在类外可以直接被访问
protected和private修饰:成员在类外不能直接被访问(关于它们之间的差异等到讲类的继承时再说吧,先暂且认为他俩一样)
代码示例:

class Stack  
{  
	// 成员变量  
	int* array; 
	size_t capacity;  
	size_t top;  
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;  
	} 

}; // 分号不能省略

有三点需要注意的:

  1. 访问权限作⽤域从该访问限定符出现的位置开始直到下⼀个访问限定符出现时为⽌,如果后⾯没有访问限定符,作⽤域就到}即类结束。如代码所示,再public之前都是默认的private访问权限作用域,而且访问权限作用域可以不止一个,我们可以选择定义多个。
  2. class定义成员没有被访问限定符修饰时默认为private,struct默认为public,如代码所示,如果我不添加访问限制符,那默认为private
  3. ⼀般成员变量都会被限制为private/protected,需要给别⼈使⽤的成员函数会放为public

(4)类域

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

#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.实例化

(1)实例化概念:

定义:用类类型在物理内存中创建对象的过程,称为类实例化出对象。
类和对象的关系:类是对象进行⼀种抽象描述,是⼀个模型⼀样的东西,限定了类有哪些成员变量,这些成员变量只是声明,没有分配空间,用类实例化出对象时,才会分配空间
代码示例:

#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;  
}

(2)对象⼤⼩

在C++中,对象只存储成员变量,那成员函数呢?仔细想一下,各个对象都调用的是同一些函数,如果分开存储各个成员函数实在没必要,把各个函数的指针存储到一个公共代码区,共同调用就行了。但实际上,函数指针也是不需要存储对象中的,函数指针是⼀个地址,调用函数被编译成汇编指令 call地址,其实编译器在编译链接时,就要找到函数的地址,不是在运行时找。

存储方式示例:
在这里插入图片描述

内存对齐规则:

  • 第一个成员在与结构体偏移量为0的地址处。
  • 其他成员变量要对齐到某个数字(对齐数)的整数倍的地址处。
  • 注意:对齐数=编译器默认的⼀个对齐数与该成员大小的较小值。
  • VS中默认的对齐数为8
  • 结构体总大小为:最大对齐数(所有变量类型最大者与默认对齐参数取最小)的整数倍。
  • 如果嵌套了结构体的情况,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整体大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍。
    图示:
    在这里插入图片描述

3.this指针

定义:编译器编译后,类的成员函数默认都会在形参第⼀个位置,增加⼀个当前类类型的指针,叫做this指针。
作用:

  • 前面说过,成员函数被存在一个公共的代码区域里, 当一个对象调用函数时,该函数需要知道应该访问哪个对象,而对象将this指针传给函数,让函数知道是哪个对象调用的它。
  • 类的函数成员需要访问对象的成员变量,本质都是通过 this指针访问。
    this指针的使用场景:
  • 函数定义表面原型:void Init(int year,int month, int day)
    实际原型:void Init(Date* const this,int year,int month, int day)
  • 成员函数访问成员变量,比如Init函数中给_year赋值
    表面:_year=year
    实际:`this->_year=year

注意:
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)
	{
		// 编译报错:error C2106: “=”: 左操作数必须为左值(this是常量本身不能发生改变)
		//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;
}

4.C++相对C语言封装性的体现

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

\\C语言实现Stack
#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;
} 
int main()
{
	ST s;
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);
	STPush(&s, 3);
	STPush(&s, 4);
	while (!STEmpty(&s))
	{
		printf("%d\n", STTop(&s));
		STPop(&s);
	}
	STDestroy(&s);
	return 0;
}
\\C++实现Stack
#include<iostream>
#include<assert.h>
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;
};
int main()
{
	Stack s;
	s.Init();
	s.Push(1);
	s.Push(2);
	s.Push(3);
	s.Push(4);
	while (!s.Empty())
	{
		printf("%d\n", s.Top());
		s.Pop();
	} s
		.Destroy();
	return 0;
}

封装性的具体体现:相比C语言,如果要获取或者栈顶元素,C语言可以直接通过s.a[top-1]直接获取或者改变。但在C++中,如果要获取或者栈顶元素,只通过目前学过的方法,在不改变成员变量的访问限制符的情况下,就只能通过成员方法获取或改变栈顶元素。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值