类与对象 Part.I

文章介绍了C++中的面向对象特性,包括类的引入、定义、访问限定符(公有、私有、保护)、实例化和对象模型。重点讨论了类的封装概念,以及this指针在成员函数中的作用。还通过栈(Stack)类的例子展示了如何在C++中实现数据结构并管理内存。
摘要由CSDN通过智能技术生成


0 this

nullptr NULL本质是0


void f(int)
{
	cout << "f(int)" << endl;
}
void f(int*)
{
	cout << "f(int*)" << endl;
}
int main()
{
	f(0);
	f(NULL);
	f(nullptr);
	return 0;
}

1 面向过程和面向对象初步认识

C语言是面向过程的,关注的是过程
C++是基于面向对象的,关注的是对象

2 类的引入

C语言结构体中只能定义变量

ListNode in C with typedef
typedef struct ListNode
{
	int val;
	struct ListNode* next;
}LN;

在C++中,结构体内不仅可以定义变量,也可以定义函数

struct Stack
{
	void Init(int capacity)
	{
		_arr = (DataType*)malloc(sizeof(DataType) * capacity);
		if (_arr == nullptr)
		{
			perror("malloc failed");
			exit(-1);
		}
		_size = 0;
		_capacity = 0;
	}
	void Destroy()
	{
		if (_arr)
		{
			free(_arr);
			_arr = nullptr;
			_size = 0;
			_capacity = 0;
		}
	}
	void Push(const DataType& data)
	{
		//check capacity
		_arr[_size] = data;
		_size++;
	}
	DataType Top()
	{
		return _arr[_size - 1];
	}

	DataType* _arr;
	int _size;
	int _capacity;
};
 

以下最好在.h和.cpp实现


class Stack
{
public:
	void Init(int capacity)
	{
		_arr = (DataType*)malloc(sizeof(DataType) * capacity);
		if (_arr == nullptr)
		{
			perror("malloc failed");
			exit(-1);
		}
		_size = 0;
		_capacity = 0;
	}
	void Destroy()
	{
		if (_arr)
		{
			free(_arr);
			_arr = nullptr;
			_size = 0;
			_capacity = 0;
		}
	}
	void Push(const DataType& data)
	{
		//check capacity
		_arr[_size] = data;
		_size++;
	}
	DataType Top()
	{
		return _arr[_size - 1];
	}
private:
	DataType* _arr;
	int _size;
	int _capacity;
};
int main()
{
	Stack st;
	st.Init(10);
	st.Push(1);
	st.Push(2);
	st.Push(3);
	cout << st.Top() << endl;
	st.Destroy();
	return 0;
}

3 类的定义

类声明放在.h文件中,成员函数定义放在.cpp文件中,注意:成员函数名前需要加类名

4 类的访问限定符及封装

public修饰的成员在类外可以直接被访问
protected和private修饰的成员在类外不能直接被访问
class的默认访问权限为private,struct为public

封装的比喻: 人不需要知道计算机内部,知道开关机、鼠标以及键盘插孔等可以与计算机进行交互即可

5 类的作用域

头文件

class Date
{
public:
	void Init(int year, int month, int day);
	void Print();
	void TestThis();
private:
	int _year;
	int _month;
	int _day;
};

.cpp

//Date
void Date::Init(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
void Date::Print()
{
	cout << _year << " " << _month << " " << _day << endl;
}
void Date::TestThis()
{
	cout << this << endl;
	cout << "TestThis" << endl;

}

使用

Date class
int main()
{
	Date d1;
	d1.Init(2023, 2, 6);
	d1.Print();
	cout << sizeof(d1) << endl;
	Date d2;
	cout << sizeof(d2) << endl;
	return 0;
}

6 类的实例化

类是对对象进行描述的,是一个模型一样的东西,限定了类有哪些成员,定义出一个类,并没有分配实际的内存空间来存储它
类实例化出对象就像现实中使用建筑设计图建造出房子,类就像是设计图

7 类对象模型

一个类的大小,实际就是该类中”成员变量”之和.空类大小是1
class A1
{
public:
	void f1(){}
private:
	int _a;
};
class A2
{
public:
	void f2() {}
};
class A3
{
};
int main()
{
	A1 a1;
	A2 a2;
	A3 a3;
	cout << sizeof(a1) << endl;
	cout << sizeof(a2) << endl;
	cout << sizeof(a3) << endl;
	return 0;
}

8 this指针

this pointer
int main()
{
	//Date d1, d2;
	//d1.Init(2022, 1, 1);
	//d2.Init(2023, 2, 6);
	//d1.Print();
	//d2.Print();
	Date* p = nullptr;
	p->TestThis();//OK
	(*p).TestThis();//OK
	//p->Init(2022, 1, 1); breakdown
	return 0;
}

C++
C++中通过类可以将数据 以及 操作数据的方法进行完美结合,通过访问权限可以控制那些方法在 类外可以被调用,即封装
实现
Stack.h

//Stack in C++
typedef int DataType;
class Stack
{
public:
	void Init();
	void Push(DataType data);
	void Pop();
	DataType Top();
	int Empty();
	int Size();
	void Destroy();
private:
	void CheckCapacity();
private:
	DataType* _array;
	int _capacity;
	int _size;
};


Stack.cpp

//Stack in C++
void Stack::Init()
{
	_array = (DataType*)malloc(sizeof(DataType) * 3);
	if (NULL == _array)
	{
		perror("malloc failed");
		return;
	}
	_capacity = 3;
	_size = 0;
}
void Stack::Push(DataType data)
{
	CheckCapacity();
	_array[_size] = data;
	_size++;
}
void Stack::Pop()
{
	if (Empty())
		return;
	_size--;
}
DataType Stack::Top() { return _array[_size - 1]; }
int Stack::Empty() { return 0 == _size; }
int Stack::Size() { return _size; }
void Stack::Destroy()
{
	if (_array)
	{
		free(_array);
		_array = NULL;
		_capacity = 0;
		_size = 0;
	}
}
	void Stack::CheckCapacity()
	{
		if (_size == _capacity)
		{
			int newcapacity = _capacity * 2;
			DataType* temp = (DataType*)realloc(_array, newcapacity *
				sizeof(DataType));
			if (temp == NULL)
			{
				perror("realloc failed");
				return;
			}
			_array = temp;
			_capacity = newcapacity;
		}
	}

main.cpp

int main()
{
	Stack st;
	st.Init();
	st.Push(1);
	st.Push(2);
	st.Push(3);
	st.Push(4);
	st.Push(50);
	cout << st.Top() << " " << st.Size() << endl;
	st.Pop();
	st.Pop();
	cout << st.Top() << " " << st.Size() << endl;
	st.Destroy();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值