c ++ polymorphism and polymorphism compile-time polymorphism of the running time

Polymorphism is defined:

The same operation and effect of different objects can have different interpretations, produce different execution result.

(1) The compiler polymorphic / static binding
means build is completed at compile time, i.e. determines the relationship between the operation of the calling program code execution of the operation at compile time based pointer type or a reference to an object of type .
(Today's C ++ is no longer a mere "band class C" language, it has developed into a collection of language more than one kind of secondary language thereof, wherein generic programming based on its STL is that part of the C ++ development the most out of color in object-oriented programming in C ++, polymorphism is one of three characteristic OO, this operation is called polymorphism of polymorphism, also known as dynamic polymorphism; in the generic programming, polymorphism based template (template) of Analysis and instantiated overloaded functions, such polymorphic performed at compile time, the compiler of so called static polymorphism or polymorphic.)

(2) Run-time polymorphism / dynamic binding
means dynamically build the program is running, calls to functions based on the type of the object.

Difference summary

Here Insert Picture Description

Code

(1) polymorphism Compiled
1) function overloading

// compile-time polymorphism, function calls, and pointer type related.
#include
the using namespace STD;
class A {
public:
void Print () {COUT << "A" << endl;} // overloads
};
class B: A public {
public:
void Print () {<< COUT "B" << endl;} // function overloading
};

main int () {
A * p; // pointer p is type A
A A;
B B;
p = & A;
p-> Print ();
p = & B;
p-> Print ();
return 0;
}
// output results:
A
A

2) Examples of templates instantiated

#include
#include
using namespace std;
class animal{
public:
void voice(){
cout<<“动物叫声”<<endl;
}
};
class Cat:public animal{
public:
void voice(){
cout<<“喵喵~”<<endl;
}
};
class Dog:public animal{
public:
void voice(){
cout<<“汪汪!”<<endl;
}
};
class Bunny:public animal{
public:
void voice(){
cout<<“唧唧”<<endl;
}
};
template //类模板
void pVoice(T&tmp){
tmp.voice();
}

main int () {
Animal Anim;
Cat CAT;
Dog Dog;
Bunny Bunny;
pVoice (Anim); // compiler template parameter estimation, to invoke different functions
pVoice (CAT);
pVoice (Dog);
pVoice (Bunny);
return 0;
}
// run results:
animal sounds
meow ~
woof!
Haw

(2) runtime polymorphism
1) using base class pointer to the derived class

// Run-time polymorphism, the function call pointer referents type related
#include
the using namespace STD;
class A {
public:
Virtual void Print () {COUT << "A" << endl;} // virtual function
};
B class: public A {
public:
virtual void Print () {COUT << "B" << endl;} // virtual function
};

main int () {
A * p;
A A;
B B;
p = & A; // pointer p points a, a base pointer to a base class
p-> Print ();
p = & B; // pointer p to point b, subclass pointer to the base class, subclass virtual function call
p-> Print ();
return 0;
}
// run results:
A
B

Use base class reference

#include
using namespace std;
class animal{
public:
virtual void voice(){
cout<<“动物叫声”<<endl;
}
};
class Cat:public animal{
public:
virtual void voice(){
cout<<“喵喵~”<<endl;
}
};
class Dog:public animal{
public:
virtual void voice(){
cout<<“汪汪!”<<endl;
}
};
class Bunny:public animal{
public:
virtual void voice(){
cout<<“唧唧”<<endl;
}
};

void pVoice (animal & tmp) { // reference to the base class
tmp.voice ();
}

int main(){
Cat cat;
Dog dog;
Bunny bunny;
pVoice(cat);
pVoice(dog);
pVoice(bunny);

return 0;

}
// results are as follows:
Meow ~
woof!
Haw

Heterogeneous collection process

#include
#include
using namespace std;
class animal{
public:
virtual void voice(){
cout<<“动物叫声”<<endl;
}
};
class Cat:public animal{
public:
virtual void voice(){
cout<<“喵喵~”<<endl;
}
};
class Dog:public animal{
public:
virtual void voice(){
cout<<“汪汪!”<<endl;
}
};
class Bunny:public animal{
public:
virtual void voice(){
cout<<“唧唧”<<endl;
}
};

main int () {
Vector <* Animal> anims; // use containers
Dog D; Cat C; Bunny B;
Animal Dog = & D;
Animal
CAT = & C;
Animal & B * = Bunny;
// set of classes of heterogeneity

anims.push_back(cat);
anims.push_back(dog);
anims.push_back(bunny);
for(int i;i<anims.size();++i)
    anims[i]->voice();
return 0;

}
// results are as follows:
Meow ~
woof!
Haw

Additional:
Run claim polymorphic base class corresponding to the derived class virtual function while having the same:
a function name
2 function parameter type, number and order
3 return value
if the above conditions are not met, the derived class virtual function will lose its virtual property, and employs polymorphism at compile-time call.

Sample code is as follows:

#include
using namespace std;

class base{
public:
virtual void fun1(){cout<<“baseFun1”<<endl;}
virtual void fun2(){cout<<“baseFun2”<<endl;}
void fun3(){cout<<“basefun3”<<endl;}
void fun4(){cout<<“baseFun4”<<endl;}
virtual void fun5(){cout<<“baseFun5”<<endl;}
};

derived class: public Base {
public:
Virtual void fun1 () {COUT << "derivedFun1" << endl;} // Run-time polymorphism. It is defined as the base class virtual function similar
virtual void fun2 (int x) { cout << "derivedFun2" << endl;} // compile-time polymorphism. Although the base class, the derived class virtual functions are defined, but both having different number of parameters,
// derived class virtual functions therefore lose their characteristic virtual using polymorphic compile-time
virtual fun3 () {cout << " derivedFun3" << endl;} // compile-time polymorphism. General base class member function is defined as the derived class virtual function, but the characteristics described as the standard base class.
void fun4 () {cout << " derivedFun4" << endl;} // compile-time polymorphism. Base class, the derived class member functions are typically
void fun5 () {cout << " derivedFun5" << endl;} // Run-time polymorphism. Base class virtual function, derived class member function in general, illustrate still prevail base class
};

main int () {
Base * P;
Base A; B derived;
P = & B;
p-> fun1 ();
p-> fun2 ();
p-> FUN3 ();
p-> FUN4 ();
p-> fun5 ();
return 0;
}
// output results are as follows:
derivedFun1
baseFun2
baseFun3
baseFun4
derivedFun5
----------------
copyright: CSDN bloggers above as "cluster stiff stiff" in original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/pilipilipan/article/details/79675653

The following is another article, the main advantages and disadvantages of polymorphic introduce compile and run-time polymorphism

C ++ Compiler for Multi-state and multi-state operation of the
read directory

Introduction
run polymorphic
compile Polymorphism
Polymorphism compile multi-state operation of advantages and disadvantages of
operating advantages of polymorphism
run disadvantages of polymorphic
compile disadvantages of polymorphic
explicit and implicit interface on the interface

Preface
Today's C ++ is no longer a mere "band class C" language, it has developed into a collection of language more than one kind of secondary language thereof, wherein generic programming based on its STL is that part of the C ++ development the most out of color . In object-oriented programming in C ++, OO polymorphism is one of the three characteristics of such a multi-state multi-state operation is called, also known as dynamic polymorphism; in the generic programming, polymorphism based Template (template) WITH overload now a function of the resolution, this polymorphism performed at compile time, the compiler of the so called polymorphism or polymorphic static. In this article, we will learn:

What is the run-time polymorphism
what is compile-time polymorphism
their advantages and disadvantages in which

Run-time polymorphism
run-design polymorphic comes down to design up class inheritance system. For objects with a set of related functions, we always hope their common abstract set of functions, these functions declared in the base class for the virtual interface (virtual function), then the subclass inherits the base class to override these virtual interfaces, to achieve a unique subclass specific functions. Typically we will cite the following example:

class Animal
{
public :
virtual void shout() = 0;
};
class Dog :public Animal
{
public:
virtual void shout(){ cout << “汪汪!”<<endl; }
};
class Cat :public Animal
{
public:
virtual void shout(){ cout << “喵喵~”<<endl; }
};
class Bird : public Animal
{
public:
virtual void shout(){ cout << “叽喳!”<<endl; }
};

int main()
{
Animal * anim1 = new Dog;
Animal * anim2 = new Cat;
Animal * anim3 = new Bird;

// by pointer (or reference) call interface, determining at run pointer (or reference) referred to the actual type of the object, the call corresponding to the type of the interface
anim1-> Shout ();
anim2-> Shout ();
anim3 -> shout ();

//delete 对象
...

0 return;
}
implement multi-state operation of the mechanism depends on the virtual function. When a class declares a virtual function, the compiler will class object placed a virtual function table pointer, and a unique set of virtual function tables for the class, virtual function tables are stored in the class virtual function address. During operation to determine the real class virtual function table by the virtual function table pointer and the virtual function.

Polymorphic runtime advantage is that it makes possible the processing heterogeneous collection of objects known as:

// we have a zoo, there are a bunch of animals
int main ()
{
the Vector <* Animal> anims;

Animal * anim1 = new Dog;
Animal * anim2 = new Cat;
Animal * anim3 = new Bird;
Animal * anim4 = new Dog;
Animal * anim5 = new Cat;
Animal * anim6 = new Bird;

//处理异质类集合
anims.push_back(anim1);
anims.push_back(anim2);
anims.push_back(anim3);
anims.push_back(anim4);
anims.push_back(anim5);
anims.push_back(anim6);

for (auto & i : anims)
{
    i->shout();
}
//delete对象
//...
return 0;

}
Summary: run through the virtual function polymorphism occurs in the runtime

Compile-time polymorphism

For the template parameters, polymorphism is instantiated template and functions implemented overload resolution. With different template parameters instantiated lead to different calling function, which is called the compile-time polymorphism.
Compared to the run-time polymorphism, achieved between classes compile-time polymorphism does not need to be an inheritance hierarchy can be no relationship between them, but they all have the same constraints implicit interface. We will rewrite the above example:

class Animal
{
public :
void shout() { cout << “发出动物的叫声” << endl; };
};
class Dog
{
public:
void shout(){ cout << “汪汪!”<<endl; }
};
class Cat
{
public:
void shout(){ cout << “喵喵~”<<endl; }
};
class Bird
{
public:
void shout(){ cout << “叽喳!”<<endl; }
};
template
void animalShout(T & t)
{
t.shout();
}
int main()
{
Animal anim;
Dog dog;
Cat cat;
Bird bird;

animalShout(anim);
animalShout(dog);
animalShout(cat);
animalShout(bird);

getchar();

}
Before compiling function templates t.shout () call is not sure which interface. During compilation, the compiler deduce template argument, therefore calls shout determine which specific type of interface. Different inferences call different functions, this is the compiler polymorphism. This is similar to overloaded functions derived by the compiler, to determine which function is called.

Polymorphism and compile a multi-state operation of the advantages and disadvantages of
run-time polymorphism advantages of
OO design of important features, intuitive understanding of the objective world.
Able to handle the same set of heterogeneous classes in an inheritance hierarchy.
Run-time polymorphism drawback
During operation of virtual functions bind to improve the program runs overhead.
Large class hierarchies, to modify the interface is likely to affect the class inheritance hierarchy.
Because virtual functions at runtime is determined, so the compiler can not optimize the virtual function.
Virtual table pointer increase the volume of the object, but also more than one class virtual function table, of course, this is as it should be worthwhile resource consumption, little disadvantage as a reluctant.

>> compile-time polymorphism advantage

It brings the concept of generic programming, C ++ has a powerful weapon makes this generic programming and STL.
In the compiler to complete the multi-state, improve runtime efficiency.
With strong adaptability and loose coupling, by a particular type of partial template specialization, full specialization treated.

Compile-time polymorphism drawback
program reduces readability, the code difficult to debug.
Separate compilation of templates can not be achieved when a large project, compile time should not be overlooked.
We can not handle heterogeneous collections of objects.

Annex: About explicit and implicit interface Interface

The so-called explicit interface refers to the interface or an interface to a specific class of class hierarchies defined in the offer, all in all, we were able to find this interface in the source code. Explicit interface to function signature as the center, for example,

AnimalShot void (Animal & Anim)
{
anim.shout ();
}
we call an explicit interface to shout. In operation of the interface of the polymorphic are all explicit interface.

While for template parameters, the interface is implicit, founded on valid expression. E.g:

Template
void AnimalShot (T & anim)
{
anim.shout ();
}
for anim, the interfaces which must be supported in the body anim operation is determined to be executed by the template parameters, in the above example, T must support shout () operation, then T is a shout of implicit interface.

Original articles published 0 · won praise 0 · Views 71

Guess you like

Origin blog.csdn.net/weixin_44212827/article/details/104423296
Recommended