#include
using namespace std; Class A { Public: Virtual void funA () { Cout & lt; <"Class A" funA call & lt; } Virtual ~ (A) {}; }; Class B: public A { Public: Virtual void funA () { Cout & lt; <"Class B funA calls" & lt; } Void funB () { Cout & lt; <"Class B funB calls" & lt; } }; Int main () { A * obj=new B; Obj - & gt; FunA (); The delete obj. return 0; } The code above rewritten in subclasses B funA, then use a parent class pointer obj to subclass B, realizing polymorphism, My question is, if I need to implement the function in a subclass B funB, not to create a class B pointer? Or, a parent class pointer to a subclass object, can only perform some method in the parent class, but cannot perform the unique method in a subclass, so the significance of polymorphism in where? , every brother please comment, thank you! CodePudding user response:
No, your understanding is wrong It is actually what type of object, what type of virtual function is executed CodePudding user response:
Don't have to create B pointer, pointer change my is ok, because not know funB, type A pointer to pointer type B can CodePudding user response:
refer to the second floor qybao response: don't have to create B pointer, pointer change my is ok, because not know funB, type A pointer to pointer type B can Directly from the start that define B class pointer is not more convenient? Why use polymorphism CodePudding user response:
The reference 3 floor qq_34319012 response: that directly from the start definition B class pointer is not more convenient? Why use polymorphic This is because you fully use the characteristics of the subclass (that is, subclasses peculiar function called) With the parent class at the beginning, because we wanted to show personality by common (is also called the parent of the same function, achieve the effect of different subclasses implement) Is unified invocation (unified to convenient automation), diversification as a result, such as A. A=new A B=new b A c=new c By calling the same function a.f unA (); B. unA (); C.f unA (), but the subclass overrides the function, so the result is not the same, the same function The benefits of this type of design, for will tell you not clear, you remember to use first, later you see more, nature will understand the benefits of the For example, Special testing A function A (regardless of what A subclass), void testA (A) A {a.f unA (); }, the user calls the API to you A can of all kinds, but as long as the guarantee to inherit A (i.e. have funA function), so no matter what kind of A object, can test this doA function, otherwise, you have to write three methods testA {a.f unA}, testB {b. the unB}, testC {c.f unC}, this code cannot be reused, redundant trival, CodePudding user response:
Polymorphic remember three points, we can understand nature: 1. As the upstairs said: use common show individual character, that is why polymorphism appear; 2. A virtual function, is a dynamic binding, it also realizes the use method can call the base class pointer to a derived class; 3. The heaviest which function call, which is by the base class pointer to a derived class, CodePudding user response:
I remember polymorphism is an example, the initial learning time: Is time again to bring out this example by revisit it, Such as A class of people { Virtual void to eat () {}=0//human affirmation can eat, Virtual void to speak () {}=0//human affirmation can talk, } Then And god said to classification, Class Chinese: is human { Talk () {say "Chinese"} } Class: Japanese is human { Talk () {said "Japanese"} } Ok -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- The main () { There is a class of human x =was a Japanese x because it is a human - & gt; So can speak ()//because x sure is Japanese, //so x this speak Japanese, believes it is said that the Japanese, } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Polymorphism is actually want to solve this problem, CodePudding user response:
reference qq_34319012 reply: 3/f Quote: refer to the second floor qybao response: Don't have to create B pointer, pointer change my is ok, because not know funB, type A pointer to pointer type B can Directly from the start that define B class pointer is not more convenient? Why use polymorphic The reference response: 6th floor thousand dream life I remember polymorphism is an example, the initial learning: Is time again to bring out this example by revisit it, Such as A class of people { Virtual void to eat () {}=0//human affirmation can eat, Virtual void to speak () {}=0//human affirmation can talk, } Then And god said to classification, Class Chinese: is human { Talk () {say "Chinese"} } Class: Japanese is human { Talk () {said "Japanese"} } Ok -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- The main () { There is a class of human x =was a Japanese x because it is a human - & gt; So can speak ()//because x sure is Japanese, //so x this speak Japanese, believes it is said that the Japanese, } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Polymorphism is actually trying to solve this problem, the The problem, very simple, There is now a man, In some cases need to talk, Can direct x - & gt; Talk, Don't need to care about he is Japanese or Chinese, or French... Because virtual classes, like humans, and cannot be directly created as an entity object, because humans will speak, but the human this virtual class () function, here is my definition to a pure virtual function