C++类与对象-基础篇

目录

一、什么是类

1.1 语法定义

1.2 访问限定符

1.3 类域

二、类的实例化

2.1 什么是实例化

2.2 类的大小

三、this指针

3.1 引入

3.2 this指针的使用


一、什么是类

1.1 语法定义

class 类名
{


};

说明

  1. 类似于C语言中的结构体,括号后分号不能丢
  2. 类内成员可以为变量,函数(与结构体不同)
  3. C++中struct也可以定义类,C++兼容C中struct的⽤法,同时struct升级成了类:struct中也可以定义函数

举例(以顺序表为例)

#include<iostream>

using namespace std;

class SeqList
{
public:
	void Init(int n = 4)
	{
		arr = (int*)malloc(sizeof(int) * n);
		if (arr == nullptr)
		{
			perror("malloc");
			exit(1);
		}
		capacity = n;
		size = 0;
	}
	void Destory()
	{
		free(arr);
		arr = nullptr;
		size = capacity = 0;
	}
private:
	int* arr;
	int size;
	int capacity;
};

1.2 访问限定符

  1. public修饰的成员在类外可以直接被访问;protected和private修饰的成员在类外不能直接被访问,protected和private是⼀样的,在继承章节才能体现出他们的区别。
  2. class定义成员没有被访问限定符修饰时默认为private,struct默认为public。

1.3 类域

类定义了⼀个新的作⽤域,类的所有成员都在类的作⽤域中。当声明和定义分离时,需要指定类域。

#include<iostream>

using namespace std;

class SeqList
{
public:
	void Init(int n = 4);
	void Destory()
	{
		free(arr);
		arr = nullptr;
		size = capacity = 0;
	}
private:
	int* arr;
	int size;
	int capacity;
};

void SeqList::Init(int n = 4)
{
	arr = (int*)malloc(sizeof(int) * n);
	if (arr == nullptr)
	{
		perror("malloc");
		exit(1);
	}
	capacity = n;
	size = 0;
}

类域影响的是编译的查找规则,如果不指定类域中的函数,编译器就会把它当成全局函数,那么编译时,找不到该成员的声明/定义,就会报错。类内访问类似于C语言中结构体的访问,使用符号.

#include<iostream>

using namespace std;

class SeqList
{
public:
	void Init(int n = 4)
	{
		arr = (int*)malloc(sizeof(int) * n);
		if (arr == nullptr)
		{
			perror("malloc");
			exit(1);
		}
		capacity = n;
		size = 0;
	}
	void Destory()
	{
		free(arr);
		arr = nullptr;
		size = capacity = 0;
	}
private:
	int* arr;
	int size;
	int capacity;
};
int main()
{
	SeqList sq;
	sq.Init();
	return 0;
}

二、类的实例化

2.1 什么是实例化

  • 类似于C语言中的结构体,类也相当于图纸,仅仅限定了有哪些变量,这些只是声明,没有分配空间。⽤类类型在物理内存中创建对象的过程,称为类实例化出对象。
  • ⼀个类可以实例化出多个对象,实例化出的对象占用实际的物理空间,存储类成员变量。
#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;
	Date d2;
	d1.Init(2005,6, 8);
	d1.Print();
	d2.Init(2024, 7, 10);
	d2.Print();
	return 0;
}

2.2 类的大小

类的大小类似于C语言中的结构体内存计算,遵循内存对齐的原则。详解请见:

C语言自定义数据类型(1)结构体_数据改成结构体c语言-CSDN博客文章浏览阅读1k次,点赞43次,收藏27次。本文是C语言自定义类型中结构体的详解,囊括了结构体的所有知识,同时配上了大厂笔试重点题的讲解。_数据改成结构体c语言https://blog.csdn.net/paradiso989/article/details/136855002

  1. 结构体的第⼀个成员要对⻬到和结构体变量起始位置偏移量为0的地址处。
  2. 其他成员变量要对⻬到对⻬数的整数倍的地址处。
  3. 对⻬数 = 编译器默认的⼀个对齐数 与 该成员变量大小的较⼩值。
  4. 结构体总⼤⼩为最⼤对⻬数(结构体中每个成员变量都有⼀个对⻬数,所有对齐数中最⼤的)的整数倍
  5. 如果嵌套了结构体的情况,嵌套的结构体成员对齐到⾃⼰的成员中最⼤对齐数的整数倍处,结构体的整体大小就是所有最⼤对⻬数(含嵌套结构体中成员的对⻬数)的整数倍。

唯一有区别的是:类内存在函数,函数不和变量存在一块空间中,计算空间占用不用考虑函数

三、this指针

3.1 引入

当同一类实例化出多个对象时,调用函数,由于函数体中没有关于不同对象的区分,那当不同对象调用函数时,该函数是如何知道应该访问的是哪个对象呢?

实际上编译器编译后,类的成员函数默认都会在形参第⼀个位置,增加⼀个当前类类型的指针,叫做this指针。类的成员函数中访问成员变量,本质都是通过this指针访问的。

以上述Date类为例

class Date
{
public:
	void Init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//实际上是
	void Init(Date* const this,int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
}

3.2 this指针的使用

C++规定不能在实参和形参的位置显⽰的写this指针(编译时编译器会处理),但是可以在函数体内
⽰使⽤this指针。

基础用法:使用this指针可以访问对象的成员变量和成员函数:

class MyClass {
public:
    int value;
    void setValue(int value) {
        this->value = value;
    }
};

更高阶的用法见博主后续博客更新

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 21:40:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 21:40:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 21:40:01       58 阅读
  4. Python语言-面向对象

    2024-07-10 21:40:01       69 阅读

热门阅读

  1. postman接口测试工具

    2024-07-10 21:40:01       25 阅读
  2. python使用tkinter添加下载进度UI

    2024-07-10 21:40:01       25 阅读
  3. 计算机网络-HTTP常见面试题

    2024-07-10 21:40:01       22 阅读
  4. c++实战-多子棋

    2024-07-10 21:40:01       28 阅读
  5. 访问者模式在金融业务中的应用及其框架实现

    2024-07-10 21:40:01       20 阅读
  6. PyTorch清理CPU缓存

    2024-07-10 21:40:01       19 阅读
  7. qt 自定义信号和槽举例

    2024-07-10 21:40:01       21 阅读