C++中的类的引入

一、类的引入

在C语言的结构体中只能定义变量;在C++的结构体中,不仅可以定义变量,还可以定义函数。

例如,在用C语言实现栈时,结构体struct中只能定义变量;但是如果用C++实现,结构体中还可以定义函数。

#include <iostream>
using std::cout;
using std::endl;

typedef int DataType;

struct Stack {

	DataType* _a;
	int _size;
	int _capacity;

	//初始化
	void Init(int capacity)
	{
		_a = (DataType*)malloc(sizeof(DataType) * capacity);
		if (_a == nullptr)
		{
			perror("malloc fail");
			exit(-1);
		}

		_capacity = capacity;
		_size = 0;
	}

	//扩容
	void check_capacity()
	{
		if (_size == _capacity)
		{
			//int newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			int newcapacity = 2 * _capacity;
			DataType* tmp = (DataType*)realloc(_a, sizeof(DataType) * newcapacity);
			if (tmp == nullptr)
			{
				perror("realloc fail");
				exit(-1);
			}
			else
			{
				_a = tmp;
				_capacity = newcapacity;
			}
		}
	}

	//入栈
	void Push(DataType data)
	{
		check_capacity(); 

		//扩容函数略
		_a[_size] = data;
		_size++;
	}

	//出栈
	DataType Top()
	{
		return _a[_size--];
	}

	//销毁
	void Destory()
	{
		if (_a)
		{
			free(_a);
			_a = nullptr;
			_size = 0;
			_capacity = 0;
		}
	}

	//打印
	void Print()
	{
		for (int i = 0; i < _size; i++)
		{
			cout << _a[i] << " ";
		}
		cout << endl;
	}
};

int main()
{
	struct Stack s;
	s.Init(5);

	s.Push(1);
	s.Push(2);
	s.Push(3);
	s.Push(4);
	s.Push(5);

	s.Print();

	s.Top();
	s.Top();

	s.Print();

	s.Push(4);
	s.Push(5);
	s.Push(6);
	s.Push(7);
	s.Push(8);

	s.Print();

	s.Destory();
	return 0;
}

在C++中,更喜欢用class来代替结构体struct的定义

class className {

	//类体:由成员函数和成员变量组成
};

二、类的定义

类的定义有两种方法:

1.声明和定义全部放在类体中(成员函数在类中定义,编译器可能会将其当成内联函数处理)

class Date {
private:
	int _year;
	int _month;
	int _day;

public:
	//初始化
	void Init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//打印
	void Print()
	{
		cout << _year << " " << _month << " " << _day << endl;
	}
};

2.声明和定义分离,类声明放在.h文件中,成员函数定义放在.cpp文件中

//func.h 类声明
class Date {
private:
	int _year;
	int _month;
	int _day;

public:
	//初始化
	void Init(int year, int month, int day);

	//打印
	void Print();
};

//func.cpp 成员函数定义
void Date::Init(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}

void Date::Print()
{
	cout << _year << " " << _month << " " << _day << endl;
}

//test.cpp 测试
int main()
{
	Date d1;
	d1.Init(2024, 1, 31);
	d1.Print();
	return 0;
}

三、类的访问限定符

C++中有三种访问限定符:public(公有)、private(私有)、protected(保护)

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

private和protected修饰的成员在类外不能直接被访问

访问限定符的作用域:

访问限定符的作用域是从该访问限定符出现的位置到下一个访问限定符出现的位置(如果后面没有访问限定符了,就到类结束位置)

四、类的实例化

通过类实例化出对象,其实类似于通过图纸建造出房子,类就是设计图,对象就是房子。

因此类并不占据实际的空间,只有当类实例化出对象了,才实际存储数据,占用内存空间。

所以,类中的成员变量其实只是声明,并不是定义,它们并不占据存储空间。

如上代码,Data即是一个类,而d1则是其实例化出的一个对象。

五、类对象的存储方式

类中只保存成员变量,而成员函数保存在公共的代码段

sizeof(类) 和 sizeof(对象) 意义是一样的,都是在计算类实例化出的对象的大小

一个类的大小,实际就是经过内存对齐后成员变量的大小之和。对于空类而言比较特殊,编译器会给予空类一个字节,来唯一标识这个类的对象

class A {
	int a;
	char b;
};

class B
{
	int a;
	int b;

	void func()
	{
		cout << "func()" << endl;
	}
};

class C
{

};

int main()
{
	cout << sizeof(A) << endl;//8,结构体对齐规则
	cout << sizeof(B) << endl;//8,成员函数存储在公共代码段,不在类中
    cout << sizeof(C) << endl;//1,空类编译器会给予1字节来唯一标识该空类
	return 0;
}
  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南林yan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值