C++一周学习总结(2021/02/28)

1. C++类

  • 类的构成

    由方法和数据构成,方法可以为public,也可以为private,数据一般为private。
Class Human{
    
    
public:
	void eat();
	void drink();
	void play();
	void sleep();
private:
	void seeSister();
	string name;
	int age;
}

2. 构造函数

  • 构造函数的分类

    默认构造函数
    自定义构造函数
    拷贝构造函数
    赋值构造函数

① 默认构造函数
当创建对象时,编译器自动生成默认构造函数,即为合成的默认构造函数。

类:

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

当在构造函数中初始化变量数据时,即为手动定义的默认构造函数。

类:

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

实现:

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

调用:

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

② 自定义构造函数
在构造函数中添加参数,并初始化数据。

类:

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

实现:

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

调用:

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

③ 拷贝构造函数
当创建对象,初始化时,将另一个对象赋值给当前创建的对象的时候,调用拷贝构造函数。

类:

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

实现:

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

调用:

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

上面的拷贝函数为手动定义的拷贝构造函数,当省略拷贝构造函数不写时,编译器也会自动生成和调用拷贝构造函数,即为合成的拷贝构造函数。

拷贝构造函数的使用场景:
当函数的实参是对象,返回类型为类。
数组对象,初始化。

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}; //调用三次拷贝函数

④ 赋值构造函数
当创建对象后,将另一个对象赋值给当前创建的对象的时候,调用拷贝赋值构造函数。

类:

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

实现:

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

调用:

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

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

3. 析构函数

一般用来释放资源(例如申请了动态内存,需要释放),当没有资源需要释放而不写析构函数时,编译器也会在对象销毁时自动调用。

类:

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

4. 静态数据成员和静态成员函数

- 静态数据成员

定义:

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

初始化:

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

- 静态成员函数

静态成员函数可以用类名直接访问,也可以创建对象后进行访问。
静态成员函数不能访问普通的数据成员(实例成员)和普通的成员方法(实例方法)。

定义:

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

实现:

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

调用:

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

5. const数据成员和const成员函数

const数据成员

const类型的数据成员无法修改,只能读。

定义:

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

初始化:可以使用类内值,也可以使用构造函数的初始化列表

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

const成员函数

在这个函数中,不能修改任何数据成员,只能读。也不能调用其他的非const的成员函数

定义:

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

初始化:可以使用类内值,也可以使用构造函数的初始化列表

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

5. 组合与聚合

组合

【例】一个计算机类,由CPU芯片,硬盘,内存等组成。
其中CPU芯片也使用类来表示,则CPU类与计算机类为组合关系。计算机对象被创建时,CPU对象也会被创建,计算机对象被销毁时,CPU对象也会被销毁。

CPU类:

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

计算机类:

#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;
}

初始化:

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

聚合

【例】一个计算机需要外接声卡,创建一个声卡类。
声卡类与计算机类为聚合关系。计算机对象被创建时,声卡对象也会被创建,计算机对象被销毁时,声卡对象不会被销毁。

计算机类:

class Voice;	//直接定义Voice类

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

初始化:

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

调用:

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

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

猜你喜欢

转载自blog.csdn.net/jjswift/article/details/114229942