欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++的多态与模板函数代码实例

程序员文章站 2022-06-21 21:13:33
c++的多态与模板函数代码实例 #include using namespace std; /* 继承 class human{ public: void say(){ c...

c++的多态与模板函数代码实例

#include
using 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"
#include
using 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");
}