C + + Learning Note 10--polymorphism

Source: Internet
Author: User

Polymorphic

What is polymorphic: When you issue the same instruction to various objects, the individual objects can respond according to their own circumstances.

If you want to call the function when the system calls the corresponding function according to the object's true type, you need to declare that function as virtual virtual function. Subclasses can override this function and automatically become virtual functions. Overwrite (owerride) requires that both the function name and the parameter table have the same return type (for example, a parent can return a child class pointer in a child class)

#include <iostream>using namespacestd;classvehicle{ Public:    Virtual voidStop () {cout<<"Transport Stops"<<endl;}//Virtual Functions};classCar: Publicvehicle{ Public:    voidStop () {cout<<"Car Stop"<<Endl;}};classBike: Publicvehicle{ Public:    voidStop () {cout<<"Bicycle Stop"<<Endl;}};classlight{//Traffic Lights Public :    voidStopvehicle (vehicle&v) {v.stop ();}};intMain () {light L;    Car BMW;    Bike YJ;    L.stopvehicle (BMW);    L.stopvehicle (YJ); System ("Pause");}

Virtual function table: The compiler stores the addresses of all virtual functions of this class in a single table. Each object in this class hides a pointer to the virtual function (which is done automatically in the constructor), and the object length is added 4 bytes.

Using polymorphic implementations for type recognition:

dynamic_cast< Subclass Type &> (an object of the parent class type) Converts an object that appears to be a parent class type to an object of subclass type, typically using this result to initialize a reference to an object of a subclass type or to use it on the spot. Success is all well and failure throws an exception.

dynamic_cast< Subclass *> (which is the object address of the parent class type) succeeds with the result of a normal subclass address, and the result of failure is null, which is often used

dynamic_cast requires a virtual function

#include <iostream>using namespacestd;classperson{ Public:    Virtual voidShow () {}};classTeacher: Publicperson{};classStudent: Publicperson{};classCppteacher: Publicteacher{};classcomputer{ Public:    voidstart () {};};classNotebook: Publiccomputer{};classcompany{ Public:    voidTest (person*p) {cout<<"------------------------"<<Endl; Student* P1 = dynamic_cast<student*>(P); if(p1) {cout<<"is a student"<<Endl;} Teacher* P2 = dynamic_cast<teacher*>(P); if(p2) {cout<<"is the teacher"<<Endl;} Cppteacher* P3 = dynamic_cast<cppteacher*>(P); if(p3) {cout<<"It's a C + + teacher"<<Endl;} }};intMain () {computer* C =NewNotebook (); //dynamic_cast<notebook*> (c);//no false function, non -polymorphicperson* PS =NewStudent (); person* pt =NewTeacher (); person* CPT =NewCppteacher ();    Company AUO;    Auo.test (PS);    Auo.test (PT);    Auo.test (CPT); //Delete ...System"Pause"); return 0;}

The typeID keyword implements type recognition:

#include <typeinfo>

typeID (type or data) returns a type_info&,type_info that has a member name () that represents the name of the type, type_info supports = = and! = depends on the compiler. If there are virtual functions, typeID also uses polymorphism for type recognition

Class a{

Public

A (int n) {}

A (double d) {}

A (const a& x) {}

};

A A1 (1); A A2 (1.1); A A3 (A1); Copy constructor

void func (A obj);

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

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

Func (A1);//a obj (A1) Call copy Constructor

#include <iostream>using namespacestd; #include<typeinfo>classperson{Virtual voidF () {}};//Introducing Polymorphic PropertiesclassStudent: Publicperson{};intMain () {intA; Doubleb; CharC;    Person D;    unsigned u;    Student s; person* E =NewStudent (); 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 + + Learning Note 10--polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.