昆山网站建设培训学校,一个好网站应具备哪些条件,苏州app开发,做同城网站需要哪些手续C 1、函数重载2、类2.1、类的方法和属性2.2、类的方法的定义2.3、构造器和析构器2.4、基类与子类2.5、类的public、protected、private继承2.6、类的方法的重载2.7、子类方法的覆盖 1、函数重载
函数重载大概可以理解为#xff0c;定义两个名字一模一样#xff0c;但形参不一… C 1、函数重载2、类2.1、类的方法和属性2.2、类的方法的定义2.3、构造器和析构器2.4、基类与子类2.5、类的public、protected、private继承2.6、类的方法的重载2.7、子类方法的覆盖 1、函数重载
函数重载大概可以理解为定义两个名字一模一样但形参不一样的函数。通过传入参数可以判别具体执行哪一个函数。并且在这两个函数中理论上可以执行截然不同的程序。 以下是一个简单的demo。
/*
函数重载小实验——写一段代码求两个数或者三个数的平均值
2023.9.7
*/
#include iostreamusing namespace std;float average(float ,float ,float);
float average(float ,float);float average(float a,float b,float c)
{;return (abc)/3;
}float average(float a,float b)
{return (ab)/2;
}int main()
{int choice;float temp1,temp2,temp3;float result;coutplease choose two or three numbersendl;cin choice;if (choice 2){coutplease enter two numbers:endl;cin temp1 temp2; result average(temp1,temp2);}else if(choice 3){coutplease enter three numbers:endl;cin temp1 temp2 temp3;result average(temp1,temp2,temp3); }else {coutyour input is wrong!endl;} coutplease result is:resultendl;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)
{coutrunning!endl;
}2.3、构造器和析构器
Ⅰ、类的构造器是指在实例化这个类之后程序会先执行一个和类同名的方法。 Ⅱ、类的析构器是指当类被被使用完毕之后程序会自动执行一个名字为“类名字前加个~”的方法。 但如果像2.1那样没有定义构造器和析构器那程序其实也会去执行构造器和析构器里面的程序的只是此时的程序为空。 注实例化是将一个类变成一个实体一个类可以被无限次实例化。
#include iostreamusing namespace std;class car
{
public:int num;car(int); //构造器可以传输参数~car(void); //析构器不可以传输参数void run();
};//构造器
car::car(int temp)
{num temp;coutthis cars license number is:numendl;
}//析构器本代码是在main函数执行结束后才调用析构器方法的。
car::~car(void)
{coutend.endl;
}void car::run(void)
{coutnum is running!endl;
}int main()
{class car mycar(666); //构造器的参数在实例化的时候就得赋上了class car yourcar(888); //再次实例化一个类对象mycar.run();yourcar.run();return 0;
}2.4、基类与子类
基类又称父类也就说我们可以再定义一个类继承父类的变量和方法。
#include iostreamusing namespace std;//父类
class car
{
public:int num;void run();
};//子类
class motorcycle:public car
{
public:void hand_brake();
};//父类的run方法
void car::run(void)
{coutrunningendl;
}//子类的hand_brake方法
void motorcycle::hand_brake(void)
{coutnotice! Iam 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可以被类的函数成员访问coutrunningendl;
}//子类的hand_brake方法
void motorcycle::hand_brake(void)
{test1 1; //✔ protected可以被子类的函数成员访问//test2 2; × private不可以被子类的函数成员访问coutnotice! Iam 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)
{coutrunningendl;
}void car::run(int temp)
{coutspeed runningendl;
}2.7、子类方法的覆盖
子类再次声明一个和父类一模一样的方法用以覆盖父类的方法。但值得注意的是这里说的覆盖并不完全准确。当我们对父类进行实列化之后再次调用run方法执行依旧的父类中的run方法而不是覆盖之后子类的run。这点从子类的函数成员可以调用父类的run方法也可以看出来。通过以下两段代码希望可以加强各位的理解。 代码一
//父类
class car
{
public:void run();
};//子类
class motorcycle:public car
{
public:void run();
};//父类的run方法
void car::run(void)
{coutrunningendl;
}//子类的run方法。
void motorcycle::run(void)
{car::run(); //子类的函数成员可以直接调用子类的方法coutthe motorcycle is runningendl;
}代码二
#include iostreamusing namespace std;//父类
class car
{
public:void run();
};//子类
class motorcycle:public car
{
public:void run();
};//父类的run方法
void car::run(void)
{coutthe car is runningendl;
}//子类的run方法
void motorcycle::run(void)
{coutthe motorcycle is runningendl;
}int main()
{class car my_car;class motorcycle my_motor;my_car.run(); //调用的是父类的run方法my_motor.run(); //调用的是子类的run方法return 0;
}