C++ one week study summary (2021/02/28)

1. C++ class

  • The composition of the class

    It consists of methods and data. The method can be public or private, and the data is generally private.
Class Human{
    
    
public:
	void eat();
	void drink();
	void play();
	void sleep();
private:
	void seeSister();
	string name;
	int age;
}

2. Constructor

  • Classification of constructors

    Default constructor
    Custom constructor
    Copy constructor
    Assignment constructor

① Default constructor
When creating an object, the compiler automatically generates a default constructor, which is the synthetic default constructor.

class:

Class Human{
    
    
public:
	Human(); //默认构造函数
private:
	string name;
	int age;
}
Human h; //创建对象,编译器调用Human()合成的默认构造函数

When the variable data is initialized in the constructor, it is the default constructor defined manually.

class:

Class Human{
    
    
public:
	Human(); //默认构造函数
private:
	string name;
	int age;
}

achieve:

Human::Human(){
    
    
	name = "张三";
	age = 18;
}

transfer:

Human h; //创建对象,编译器调用Human()手动定义的默认构造函数

② Custom constructor
Add parameters to the constructor and initialize the data.

class:

Class Human{
    
    
public:
	Human(string name,int age); //自定义构造函数
private:
	string name;
	int age;
}

achieve:

Human::Human(string name,int age){
    
    
	this->name = name;
	this->age = age;
}

transfer:

Human h("李四",22); //创建对象,编译器调用自定义的构造函数

③ Copy constructor
When an object is created, initialized, and another object is assigned to the currently created object, the copy constructor is called.

class:

Class Human{
    
    
public:
	Human(const Human&); //拷贝构造函数Human(const Human& other) other可以省略
private:
	string name;
	int age;
}

achieve:

Human::Human(const Human& other){
    
    
	name = other.name;
	age = other.age;
}

transfer:

Human h1("王五",21); //创建对象,编译器调用自定义的构造函数
Human h2(h1); //调用拷贝构造函数
Human h3 = h1; //调用拷贝构造函数

The above copy function is a manually defined copy constructor. When the copy constructor is omitted, the compiler will also automatically generate and call the copy constructor, which is a synthetic copy constructor.

The usage scenario of the copy constructor:
When the actual parameter of the function is an object, the return type is class.
Array object, initialized.

Human test(Human human){
    
    
	return human;
}

Human test1(Human& human){
    
    
	return human;
}

Human h1,h2,h3;
test(h1); //调用两次拷贝函数
test1(h1); //调用一次拷贝函数

Human men[] = {
    
    h1,h2,h3}; //调用三次拷贝函数

④ Assignment constructor
When an object is created, when another object is assigned to the currently created object, the copy assignment constructor is called.

class:

Class Human{
    
    
public:
	Human& operator=(const Human&); //赋值构造函数
private:
	string name;
	int age;
}

achieve:

Human& Human::operator=(const Human& other){
    
    
	name = other.name;
	age = other.age;
}

transfer:

Human test(Human& human){
    
    
	return human;
}

Human h1("王五",21); //创建对象,编译器调用自定义的构造函数
Human h2(h1); //调用拷贝构造函数
h2 = h1; //调用赋值构造函数
Human h3;
h3 = test(h1); //调用赋值构造函数

3. Destructor

It is generally used to release resources (for example, when dynamic memory is applied for and needs to be released). When there is no resource to release and no destructor is written, the compiler will automatically call it when the object is destroyed.

class:

Class Human{
    
    
public:
	~Human(); //析构函数
}

4. Static data members and static member functions

-Static data members

definition:

Class Human{
    
    
public:
	void play();
private:
	static string name;	//静态数据成员
}

initialization:

string Human::name = "张三";	//初始化静态数据成员
void play(){
    
    
	return name;	//类的方法中可以直接读写静态数据成员
}

-Static member function

Static member functions can be accessed directly using the class name, or they can be accessed after the object is created.
Static member functions cannot access ordinary data members (instance members) and ordinary member methods (instance methods).

definition:

Class Human{
    
    
public:
	void play();
	static void drink();
private:
	static string name;	//静态数据成员
	int age;
}

achieve:

Human::drink(){
    
    
	return name;	//只能访问静态数据成员和静态成员方法
}

transfer:

Human::drink();	//可以直接用类型调用
Human h1;
h1.drink();	//对象也可以调用

5. const data members and const member functions

const data member

Data members of const type cannot be modified and can only be read.

definition:

Class Human{
    
    
public:
	void play();
private:
	static string name;	//静态数据成员
	int age;
	const string boolType;	//const数据成员
//	const string  boolType = "A";	//类内初始化
}

Initialization: you can use the value of the class, you can also use the initialization list of the constructor

Human::Hunman():boolType("未知"){
    
    
	return name;
}

const member function

In this function, no data members can be modified, only read. Also cannot call other non-const member functions

definition:

Class Human{
    
    
public:
	void play() const;
	void setName();
private:
	static string name;	//静态数据成员
	int age;
}

Initialization: you can use the value of the class, you can also use the initialization list of the constructor

void Human::play() const{
    
    
	return name;	//只能读,不能修改
	setName();	//不能调用其他的非const的方法
}

5. Combination and aggregation

combination

[Example] A computer category is composed of CPU chips, hard disks, memory, etc.
Among them, the CPU chip is also represented by a class, and the CPU class and the computer class are combined. When a computer object is created, the CPU object will also be created, and when the computer object is destroyed, the CPU object will also be destroyed.

CPU category:

Class CPU{
    
    
public:
	CPU(const string *name,const int version);
private:
	string name;
	int version;
}

Computer category:

#include "CPU.h"	//包含CPU类

Class Computer{
    
    
public:
	Computer(const string *name,const int version,
		int hardDisk,int memory);
private:
	CPU cpu;
	int hardDisk;
	int memory;
}

initialization:

Computer::Computer(const string *name,const int version,
		int hardDisk,int memory):cpu(name,version){
    
    
	this->hardDisk = hardDisk;
	this->memort = memory;
}

polymerization

[Example] A computer needs an external sound card, create a sound card class.
The sound card class and the computer class are in an aggregated relationship. When a computer object is created, the sound card object will also be created, and when the computer object is destroyed, the sound card object will not be destroyed.

Computer category:

class Voice;	//直接定义Voice类

Class Computer{
    
    
public:
	void addVoice(Voice *voice);	//添加声卡
private:
	CPU cpu;
	int hardDisk;
	int memory;
}

initialization:

void Computer::addVoice(Voice *voice){
    
    
	this->voice = voice;
}

transfer:

#include "Computer.h"	//包含计算机类
#include "Voice.h"	//包含声卡类

Computer cmp;
cmp.addVoice();	//调用添加声卡的方法

Guess you like

Origin blog.csdn.net/jjswift/article/details/114229942