简体   繁体   中英

Runtime polymorphism in C++ using data members

I was studying OOP from here .

It says that runtime polymorphism is possible in C++ using data members.

Now,consider this code:-

#include <iostream>    
using namespace std;    
class Animal {                                 //  base class declaration.  
    public:    
    string color = "Black";      
};     
class Dog: public Animal                       // inheriting Animal class.  
{      
    public:    
    string color = "Grey";      
};    
int main(void) {    
     Animal d= Dog(); 
     Animal d1 = Animal();     
     cout<<d.color<<endl;
     cout<<d1.color<<endl;    
}    

In both the cases above the color "black" is printed. So, how is this example of runtime polymorphism ?

how is this example of runtime polymorphism ?

It is not an example of runtime polymorphism but rather of object slicing .

This is one of the reason why a good C++ book is recommended over online sites like this.

That is, the first choice/preference should always be(assuming you want to have a deep understanding of the subject) a good book . And then only, after you've read up the topic in the book you can look it up on sites for practical examples of the same. If you want to refer to legit C++ documentation sites you can refer to cppreference and learncpp . These two sites are C++ dedicated sites so you can rely on most of the info/example given there. Again referring to a book first is recommended.

If this was runtime polymorphism you could write a function:

void foo(Animal& a) {
    std::cout << a.color << "\n";
}

And it would print the respective color of the object based on the dynamic type of the parameter. However, there is no such thing as "Runtime-polymorphism based on member variables" in C++. The tutorial is wrong and misleading.

If compile and execute this:

#include <iostream>    
using namespace std;    
class Animal {                                          //  base class declaration.  
    public:    
    string color = "Black"; 
};     
class Dog: public Animal                       // inheriting Animal class.  
{      
    public:    
    string color = "Grey";      

}; 

void foo(Animal& a) {
    std::cout << a.color << "\n";
}
int main(void) {    
     Dog d= Dog(); 
     Animal d1 = Animal();     
     foo(d);
     foo(d1);
} 

The output will be

Black
Black

There is no such thing as virtual member variables. Only for virtual member functions use virtual dispatch:

#include <iostream>    
using namespace std;    
class Animal {                                          //  base class declaration.  
    public:    
    virtual std::string color() const { return "Black"; }
};     
class Dog: public Animal                       // inheriting Animal class.  
{      
    public:    
    std::string color() const override { return "Grey"; }

}; 

void foo(Animal& a) {
    std::cout << a.color() << "\n";
}
int main(void) {    
     Dog d= Dog(); 
     Animal d1 = Animal();     
     foo(d);
     foo(d1);
}  

Output :

Grey
Black

The turial is wrong and misleading when it says that there would be runtime polymorphis with member variables. Moreover the example they present has object slicing. See here: What is object slicing? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM