Classes and objects in c++ (1)

What is a class?

The c language is compatible with the c language, and thestruct is upgraded to a class
The structure of the c language is a class in c++
a> At the same time, class in c++ also means class
. So what is the difference?
struct means when no qualifier is written< a i=10>All public, can be called externallyclassin When a>does not write the qualifier, it means that is all private, Cannot be called externally

What is a qualifier?

Qualifier: Access permissions are set in the class
private (private) cannot be directly accessed outside the class
public (public) outside the class Can be used
protected (protected) cannot be directly accessed outside the class

c++兼容c语言struct 的所有语法
struct 同时升级为了类
类名就是 类型 Stack就是类型 不需要加struct
类里面 可以定义函数
定义类可以用 struct 也可以用 class
不写限定符的时候 class 表示 私有 struct 表示公用
class Stack
{
    
    
	//类中的访问限定符 
	//private(私有) 不能在类外被直接访问
	//public(公用)类外可以使用 
	//protected(保护) 不能在类外被直接访问
	
private:
	int* a;
	int top;
	int capacity;

public:
	//默认定义在类里面的函数 是 内联函数 但不一定展开
	//正确用法,长的函数声明和定义分离
	//短小的函数可以在类里面直接定义x
	void Init();
	void Push(int x);
	bool Empty()
	{
    
    
		return top = 0;
	}
};

What can be done in the class?

Function can be defined in a class
This function defined can only be used by this classfunction belongs to which class, then the file in which the function is defined is To indicate which declaration and definition in the class are separated
At the same time, if the function

#include"Stack.h"

//如果声明和定义分离的话 要讲明属于那个类
void Stack::Init()
{
    
    
	a = nullptr;
	top = 0;
	capacity = 10;
}
void Stack::Push(int x)
{
    
    

}

struct Queue
{
    
    
	void Init()
	{
    
    

	}
};

struct ListNode
{
    
    
	ListNode* next;
	int val;
};

What is an object?

Object: variable defined with class

The relationship between classes and objects

The relationship between classes and objects is more than 1v

//类和对象的 关系是 1v多
// 设计图 和 房子
int main()
{
    
    
	Date d;//对象
	Date D;
	
}

functions in class

The functions in the class willhide a this which is aformal parameter < a i=4>exists on the stack frame He canpoint to the passed object butCannot be written on the parameters of the function

class Date
{
    
    
public:
	void Init(int year, int month, int day)
	{
    
    
		// _在c++中惯例中 表示类部的 
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
    
    
		cout << this->_year <<endl << this->_month<<endl << this->_day << endl;
		cout << _year << _month << _day << endl;
	}
	//不能写出来 this
	//可以在类里面显示的使用
	/*void Print(Date* this)
	* //this 是形参 存在在栈帧上的
	{
		cout << this->_year << this->_month << this->_day << endl;
	}*/
	//声明不会开辟空间
private:
	int _year;
	int _month;
	int _day;
};

class B
{
    
    

};

类和对象的 关系是 1v多
 设计图 和 房子
int main()
{
    
    
	在类中 只计算变量 不计算函数
	类中函数放在公共区域 编译完之后是 一段指令
	无成员变量的类,对象大小开一个字节,这个字节不存储有效数据
	表示定义的对象存在过
	Date d;//对象
	Date D;
	cout << sizeof(d);
	cout << sizeof(B);
	d.Init(2023, 10, 7);
	D.Init(2022, 10, 7);
	
	d.Print();
	D.Print();
	不能显示的写 this 类似相关的形参和实参
	d.Print(&d1);
}

What is a constructor?

Constructor:
The function name is the same as the class name
The constructor exists in the class
It is equivalent to a Class initialization
No return value
Object instantiation can be called automatically
Can be overloaded
If each class does not write its own constructor, it will generate a constructor by default

But there cannot be two constructors without parameters in the class

class Date
{
    
    
public:
	//勾成函数重载 但是无参调用的时候会有歧义
	/*Date()
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}	*/
	/*Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}*/
	void Init(int year, int month, int day)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
    
    
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	//C++11 开始支持
	//还是声明 ,只是声明时给了缺省值
	int _year = 1;
	int _month = 1;
	int _day = 1;
};

Constructor summary

Generally, you have towrite your own constructor
if the members are allcustom types, or The default value was given when declaring, then can consider the compilation Automatically generated by the constructor
If has a constructor, then the declared default value is invalid
Built-in types are not processed

What is default construction?

We do not write the constructor generated by default during compilation, which is called the default constructor
no-argument constructor< /span>, also called default construction
full default, also called default construction

Constructs that are called without passing parameters are called default constructs.

What is a destructor?

Destructor:
~Class name
No return value
Automatically called
Similar to destruction
When not written, the compiler automatically generates
Dynamically developed classes are required
Built-in types do not Processing
Custom type processing
A class can only have one destructor

class Date
{
    
    
public:
	//勾成函数重载 但是无参调用的时候会有歧义
	/*Date()
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}	*/
	/*Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}*/
	void Init(int year, int month, int day)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
    
    
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	//~
	~Date()
	{
    
    
		cout << "~Date()" << endl;
	}

What is copy construction?

Copy construction:
Same as class name
No return value
Copy built-in type< a i=4> Automatically call When passing parameters to a custom type object, the copy constructor must be called


Built-in types can be directly copied by value
Custom type members call the copy constructor in the class

If the class is dynamically opened, then the copy constructor must be written by itself

Shallow copy:
The built-in type is directly called by value without changing its own content

void Test(Date d)
{
    
    
	//Date d1;
	d.Print();
}

Deep copy:
The essence is to copy the resource pointed to, so that you have the same space and the same value as me

//深拷贝
//本质是拷贝指向的资源,让我跟你有一样的空间,一样的值
Stack(const Stack& d)
{
    
    
	_array = (int*)malloc(sizeof(int) * d._capacity);
	if (_array == NULL)
	{
    
    
		perror("malloc 失败");
		return;
	}
	memcpy(_array, d._array, sizeof(int) * d._size);
	_size = d._size;
	_capacity = d._capacity;
}

Guess you like

Origin blog.csdn.net/dabai__a/article/details/134320624