C++的多态与模板函数代码实例
#includeusing namespace std;
/* 继承 class Human{ public: void say(){ cout << "说话" << endl; } protected: char* name; int age; }; class Man:public Human{ public: void chaing(){ cout << "泡妞" << endl; } protected: char* brother; }; void work(Human& h){ h.say(); } void main(){ Man m1; //m1.say(); //1.父类类型的引用或指针 Human* m2 = &m1; m2->say(); Human &m3 = m1; m3.say(); //子类对象初始化父类类型的对象 Human h1 = m1; h1.say(); system("pause"); }*/
/*向父类构造函数传参 class Human{ public: Human(char* name, int age){ this->name = name; this->age = age; } void say(){ cout << name<<"说话" <brother = brother; } void chaing(){ cout << "泡妞" << endl; } protected: char* brother; }; void main(){ Man m1("jack","tom",20); m1.say(); system("pause"); }*/
/*构造函数与析构函数调用的顺序 class Human{ public: Human(char* name, int age){ this->name = name; this->age = age; cout << "Human的构造函数" << endl; } ~Human(){ cout << "Human的析构函数" << endl; } void say(){ cout << name << "说话" << age << endl; } protected: char* name; int age; }; class Man :public Human{ public: //给父类构造函数传参,同时给属性对象赋值 Man(char* brother, char* name, int age) :Human(name, age){ this->brother = brother; cout << "man的构造函数" << endl; } ~Man(){ cout << "man的析构函数" << endl; } void chaing(){ cout << "泡妞" << endl; } protected: char* brother; }; void work(Human &h){ h.say(); } void main(){ //父类构造函数先调用 //子类的析构函数先调用 Man m1("jack", "tom", 20); system("pause"); }*/
//子类对象调用父类的成员 /* class Human{ public: Human(char* name, int age){ this->name = name; this->age = age; cout << "Human的构造函数" << endl; } ~Human(){ cout << "Human的析构函数" << endl; } void say(){ cout << name << "说话" << age << endl; } protected: char* name; int age; }; class Man :public Human{ public: //给父类构造函数传参,同时给属性对象赋值 Man(char* brother, char* name, int age) :Human(name, age){ this->brother = brother; cout << "man的构造函数" << endl; } ~Man(){ cout << "man的析构函数" << endl; } void chaing(){ cout << "泡妞" << endl; } void say(){ cout << "我是男人我会装逼" << endl; } protected: char* brother; }; void work(Human &h){ h.say(); } void main(){ Man m1("jack", "tom", 20); m1.say();//这个调用的是Man的say方法 Human &h1 = m1; h1.say();//这个调用的是Human的say方法 m1.Human::say();//这个调用的是Human的say方法 system("pause"); }*/
//多继承 /* //人 class Person{ }; //公民 class Citizen{ }; //学生,既是人,又是公民 class Student : public Person, public Citizen{ }; */
//继承的访问修饰 //基类中继承方式 子类中 //public & public继承 = > public //public & protected继承 = > protected //public & private继承 = > private // //protected & public继承 = > protected //protected & protected继承 = > protected //protected & private继承 = > private // //private & public继承 = > 子类无权访问 //private & protected继承 = > 子类无权访问 //private & private继承 = > 子类无权访问
//继承的二义性 //虚继承,不同路径继承来的同名成员只有一份拷贝,解决不明确的问题 /* class A{ public: char* name; }; class A1 : virtual public A{ }; class A2 : virtual public A{ }; class B : public A1, public A2{ }; void main(){ B b; //如果A1 A2 继承至A 没有使用virtual的话 会报错说name不明确 b.name = "jason"; //指定父类显示调用 b.A1::name = "jason"; b.A2::name = "jason"; system("pause"); }*/
//虚函数 //多态(程序的扩展性) //动态多态:程序运行过程中,觉得哪一个函数被调用(重写) //静态多态:重载 //发生动态的条件: //1.继承 //2.父类的引用或者指针指向子类的对象 //3.函数的重写 /* Plane.h: #pragma once class Plane{ public: virtual void fly(); virtual void land(); }; Plane.c++: #include"Plane.h" #includeusing namespace std; void Plane::fly(){ cout << "起飞" << endl; } void Plane::land(){ cout << "着路" << endl; } Jet.h: #pragma once #include "Plane.h" class Jet:public Plane{ virtual void fly(); virtual void land(); }; Jet.c++: #include"Jet.h" #include using namespace std; void Jet::fly(){ cout << "直升机起飞" << endl; } void Jet::land(){ cout << "直升机着路" << endl; } #include"Plane.h" #include"Jet.h" void work(Plane &p){ p.fly(); p.land(); } void main(){ Plane p; work(p); //这里,如果Plane的函数不是虚函数,则调用的是Plane中的fly和land方法 //只有Plane的函数是虚函数 这样调用才会调用子类的fly和land方法 Jet j; work(j); system("pause"); }*/
//纯虚函数(抽象类) //1.当一个类具有一个纯虚函数,这个类就是抽象类 //2.抽象类不能实例化对象 //3.子类继承抽象类,必须要实现纯虚函数,如果没有,子类也是抽象类 //抽象类的作用:为了继承约束,根本不知道未来的实现 /*class Shape{ public: virtual void area() = 0; }; class Circle:public Shape{ public: Circle(int r){ this->r = r; } //这里实现了纯虚函数,如果不实现这个类也是抽象类 void area(){ cout << "圆的面积:" << 3.14*r*r << endl; } private: int r; }; void main(){ Circle c(10); c.area(); system("pause"); }*/ //接口(只是逻辑上的划分,语法上跟抽象类的写法没有区别) //可以当做一个接口,内部全是纯虚函数的类 //模板函数 void tswap(int &a, int &b){ int tmp = 0; tmp = a; a = b; b = tmp; } void tswap(char &a, char &b){ char tmp; tmp = a; a = b; b = tmp; } //上面两个只是类型的不同 template//定义泛型 void myswap(T &a, T &b){ T c; c = a; a = b; b = c; } void main(){ /*int a = 2, b = 3; tswap(a,b); cout << a << "," << b << endl; char c1 = 'a'; char c2 = 'b'; tswap(c1, c2); cout << c1 << "," << c2 << endl;*/ int a = 2, b = 3; myswap (a, b);//可以声明类型 cout << a << "," << b << endl; char c1 = 'a'; char c2 = 'b'; myswap(c1, c2);//这里可以自动推断出来 cout << c1 << "," << c2 << endl; system("pause"); }