C++学习笔记(一)

C++

1、函数重载

函数重载大概可以理解为,定义两个名字一模一样,但形参不一样 的函数。通过传入参数可以判别具体执行哪一个函数。并且在这两个函数中,理论上可以执行截然不同的程序。

以下是一个简单的demo。

/* 
函数重载小实验------写一段代码求两个数或者三个数的平均值 
2023.9.7 
*/
#include <iostream>

using namespace std;

float average(float ,float ,float); 
float average(float ,float);

float average(float a,float b,float c)
{;
	return (a+b+c)/3;
}

float average(float a,float b)
{
	return (a+b)/2;
}


int main()
{
	int choice;
	float temp1,temp2,temp3;
	float result;
	
	cout<<"please choose two or three numbers"<<endl;
	cin >> choice;
	
	if (choice == 2)
	{
		cout<<"please enter two numbers:"<<endl;
		cin >> temp1 >> temp2;  
		result = average(temp1,temp2);
	}
	else if(choice == 3)
	{
		cout<<"please enter three numbers:"<<endl;
		cin >> temp1 >> temp2 >> temp3;
		result = average(temp1,temp2,temp3);	
	}
	else {cout<<"your input is wrong!"<<endl;} 
	
	cout<<"please result is:"<<result<<endl;
	
	return 0;
}

注:cin可以通过enter或tab来分割输入。

2、类

2.1、类的方法和属性

通过class定义一个叫car的类。类里面的变量就叫做属性,函数就叫做方法。

class car
{
public:
	int num;
	void run();		
};

2.2、类的方法的定义

在2.1中,我们声明了类中的有一个叫run的方法,但是具体到run函数里面到底在执行什么,还得再次定义一下。

void car::run(void)
{
	cout<<"running!"<<endl;
}

当然,如果类的方法中所实现的内容并不复杂,也大可在声明的时候直接定义了。如下:

class car
{
public:
	int num;
	void run(){ cout<<"running"<<endl; };		
};

2.3、构造器和析构器

Ⅰ、类的构造器是指,在实例化这个类之后,程序会先执行一个和类同名的方法。

Ⅱ、类的析构器是指,当类被被使用完毕之后,程序会自动执行一个名字为"类名字前加个~"的方法。

但如果像2.1那样,没有定义构造器和析构器,那程序其实也会去执行构造器和析构器里面的程序的,只是此时的程序为空。

注:实例化是将一个类变成一个实体,一个类可以被无限次实例化。

#include <iostream>

using namespace std;


class car
{
public:
	int num;
	car(int);   //构造器可以传输参数
	~car(void);  //析构器不可以传输参数
	void run();		
};

//构造器
car::car(int temp)
{
	num = temp;
	cout<<"this car's license number is:"<<num<<endl;
}

//析构器,本代码是在main函数执行结束后才调用析构器方法的。
car::~car(void)
{
	cout<<"end."<<endl;
}


void car::run(void)
{
	cout<<num<<" is running!"<<endl;
}


int main()
{
	class car mycar(666);    //构造器的参数在实例化的时候就得赋上了
	class car yourcar(888); //再次实例化一个类对象
	
	mycar.run();
	yourcar.run();
	
	return 0;
}

2.4、基类与子类

基类又称父类,也就说我们可以再定义一个类,继承父类的变量和方法。

#include <iostream>

using namespace std;


//父类 
class car
{
public:
	int num;
	void run();		
};

//子类 
class motorcycle:public car
{
public:
	void hand_brake();
};

//父类的run方法 
void car::run(void)
{
	cout<<"running"<<endl;
}

//子类的hand_brake方法 
void motorcycle::hand_brake(void)
{
	
	cout<<"notice! I'am using the hand brake!"<<endl;
}


int main()
{
	class motorcycle my_motor;
	my_motor.run();
	my_motor.hand_brake();

	return 0;
}

2.5、类的public、protected、private继承

public:可以被任何实体访问,也可以被子类访问,以及类的函数成员访问。

protected :不能被类的实体访问,但可以被子类访问,也可以类的函数成员访问。
private:不能被类的实体访问,不可以被子类访问,但可以被类的函数成员访问。

#include <iostream>
using namespace std;

//父类 
class car
{
public:
	int num;
	void run();	
protected:
	int test1;	
private:
	int test2;
};

//子类 
class motorcycle:public car
{
public:
	void hand_brake();
};

//父类的run方法 
void car::run(void)
{
	test1 = 1;   //✔ protected可以被类的函数成员访问
	test2 = 2;   //✔ private可以被类的函数成员访问
	
	cout<<"running"<<endl;
}

//子类的hand_brake方法 
void motorcycle::hand_brake(void)
{
	test1 = 1;   //✔ protected可以被子类的函数成员访问
	//test2 = 2;  × private不可以被子类的函数成员访问
	
	cout<<"notice! I'am using the hand brake!"<<endl;
}


int main()
{
	class motorcycle my_motor;
	my_motor.run();
	my_motor.num = 888;    //✔ public可以被实体访问
	//my_motor.test1 = 1;     × protected不可以被实体访问
	//my_motor.test2 = 2;     × private不可以被实体访问
	my_motor.hand_brake();

	return 0;
}

2.6、类的方法的重载

这里其实和第1章中的函数重载是一样的,只不过是类中定义两个名字一样的方法而已。

class car
{
public:
	void run();
	void run(int);	//定义两个同名的方法,一个有整形形参,一个没有。
};


void car::run(void)
{
	
	cout<<"running"<<endl;
}

void car::run(int temp)
{
	
	cout<<"speed running"<<endl;
}

2.7、子类方法的覆盖

子类再次声明一个和父类一模一样的方法,用以覆盖父类的方法。但值得注意的是,这里说的覆盖并不完全准确。当我们对父类进行实列化之后,再次调用run方法,执行依旧的父类中的run方法,而不是覆盖之后子类的run。这点从子类的函数成员可以调用父类的run方法也可以看出来。通过以下两段代码希望可以加强各位的理解。

代码一:

//父类
class car
{
public:
	void run();
};

//子类 
class motorcycle:public car
{
public:
	void run();
};

//父类的run方法 
void car::run(void)
{
	cout<<"running"<<endl;
}


//子类的run方法。
void motorcycle::run(void)
{
	car::run();         //子类的函数成员可以直接调用子类的方法
	cout<<"the motorcycle is running"<<endl;
}

代码二:

#include <iostream>

using namespace std;


//父类
class car
{
public:
	void run();
};

//子类 
class motorcycle:public car
{
public:
	void run();
};

//父类的run方法 
void car::run(void)
{
	cout<<"the car is running"<<endl;
}


//子类的run方法
void motorcycle::run(void)
{
	cout<<"the motorcycle is running"<<endl;
}



int main()
{
	class car my_car;
	class motorcycle my_motor;
	
	my_car.run();   //调用的是父类的run方法
	my_motor.run();  //调用的是子类的run方法

	return 0;
}
相关推荐
yunhuibin14 分钟前
ffmpeg面向对象——拉流协议匹配机制探索
学习·ffmpeg
Ljubim.te16 分钟前
软件设计师——数据结构
数据结构·笔记
hengzhepa24 分钟前
ElasticSearch备考 -- Search across cluster
学习·elasticsearch·搜索引擎·全文检索·es
黑不溜秋的24 分钟前
C++ 语言特性29 - 协程介绍
开发语言·c++
一丝晨光29 分钟前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
￴ㅤ￴￴ㅤ9527超级帅43 分钟前
LeetCode hot100---二叉树专题(C++语言)
c++·算法·leetcode
_GR1 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
蜡笔小新星1 小时前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
攸攸太上1 小时前
JMeter学习
java·后端·学习·jmeter·微服务
Death2001 小时前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#