码迷,mamicode.com
首页 > 编程语言 > 详细

C++学习笔记10--多态

时间:2016-05-13 20:45:52      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

多态

  什么是多态:对各种对象发出同一种指令时,各个对象能根据自身的情况作出相应的响应

  如果希望在调用函数时系统根据对象真是类型去转调用相应的函数,需要把那个函数声明为virtual虚函数。子类中可以覆盖这个函数,也自动成为虚函数。覆盖(owerride)要求函数名和参数表都相同返回类型也要一致(比如父亲中返回父类指针子类中可以返回子类指针)

#include<iostream>
using namespace std;

class Vehicle{
public:
    virtual void stop(){cout<<"交通工具停止"<<endl;} //虚函数
};
class Car:public Vehicle{
public:
    void stop(){cout<<"汽车停止"<<endl;}
};
class Bike:public Vehicle{
public:
    void stop(){cout<<"自行车停止"<<endl;}
};
class light{//交通灯
public :
    void stopvehicle(Vehicle& v){v.stop();}
};

int main()
{
    light L;
    Car bmw;
    Bike YJ;
    L.stopvehicle (bmw);
    L.stopvehicle (YJ);
    system("pause");
}

 

  虚函数表:编译器把这个类的全部虚函数的地址都保存在一张表中。每个这种类的对象里会藏一个指针指向这个虚函数(在构造函数里自动做的),对象长度会加4个字节。

  利用多态实现类型识别:

 

    dynamic_cast<子类类型&>(是父类类型的对象)把看起来是父类类型的对象转换成子类类型的对象,一般用这个结果来初始化一个子类类型的对象的引用或者当场使用。成功则 一切顺利,失败则抛出异常。

    dynamic_cast<子类*>(是父类类型的对象地址)成功则结果为一个正常的子类地址,失败则结果为NULL,常常用这一种

    dynamic_cast要求有虚函数

#include <iostream>
using namespace std;

class Person{
public:
    virtual void show(){}
};

class Teacher : public Person{};
class Student : public Person{};
class CppTeacher : public Teacher{};

class Computer{
public:
    void start(){};
};

class Notebook :public Computer{};
class Company{
public:
    void test(Person* p){
        cout<<"------------------------"<<endl;
        Student* p1 = dynamic_cast<Student*>(p);
        if(p1){cout<<"是学生"<<endl;}

        Teacher* p2 = dynamic_cast<Teacher*>(p);
        if(p2){cout<<"是老师"<<endl;}

        CppTeacher* p3 = dynamic_cast<CppTeacher*>(p);
        if(p3){cout<<"是C++老师"<<endl;}
    }
};
int main()
{
    Computer* c = new Notebook();
    //dynamic_cast<Notebook*>(c);//错 无虚函数,非多态
    Person* ps = new Student();
    Person* pt = new Teacher();
    Person* cpt = new CppTeacher();
    
    Company AUO;
    AUO.test(ps);
    AUO.test(pt);
    AUO.test(cpt);

    //delete ...
    system("pause");
    return 0;
}

 

  typeid关键字实现类型识别:

    #include<typeinfo>

    typeid(类型或者数据)返回一个type_info&,type_info里有一个成员name()表示类型的名字,type_info支持 == 和 !=  依赖于编译器。如果有虚函数,typeid也会利用多态进行类型识别

    class A{

    public:

      A(int n){}

      A(double d){}

      A(const A& x){}

        };

    A a1(1);A a2(1.1); A a3(a1); //拷贝构造函数

    void func(A obj);

    func(1);//func(A(1))

    func(1.1);//func(A(1.1))

    func(a1);//A obj(a1) 调用拷贝构造函数

#include<iostream>
using namespace std;
#include<typeinfo>

class Person{virtual void f(){}};//引入多态特性
class Student:public Person{};

int main()
{
    int a;
    double b;
    char c;
    Person d;
    unsigned u;
    Student s;
    Person* e = new Student();

    cout<<typeid(a).name() << ","<< typeid(b).name() << ","<<typeid(c).name() << ","<<typeid(d).name() << ","<<typeid(u).name() << endl;
    cout<< typeid(s).name()<<endl;
    cout<< typeid(*e).name()<<endl;
    cout<< boolalpha << (typeid(*e)==typeid(Student)) << endl;
    system("pause");
}

 

 

 

C++学习笔记10--多态

标签:

原文地址:http://www.cnblogs.com/visions/p/5487135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!