What does mean from. Animal d=Dog(); | Sololearn: Learn to code for FREE!

0

What does mean from. Animal d=Dog();

9/13/2020 1:47:11 PM

Amit

8 Answers

New Answer

+2

In c++, also concept is same almost but syntax for code in different.. Animal a = dog(); here Animal is parent class.. And dog() is subclass constructor.. I think correct way is Animal *a = &Dog(); Now super class object is assigned to sub class and first prefer to subclass methods.. Can you add link..?

+2

Yes. Because you are accessing data properties.. Eventhough you assigned subclass object or down casting.. Parent class Refference points to parent class data.. Only methods will be overridden.. If you have methods in subclass and super class then by the statement Animal d=dig(); now methods called by object d refers dog() class methods... Check out that in the next examples...

+1

In which Language? Tag the language also.. Try to add the link of page where you found! I think, you are about java code Animal d = new Dog(); Hoping Dog class is subclass, and Animal is parent class.. Then it is casting to sub class object to parent class Refference variable. So here d Refference variable act as different type on different object initialization.. Now object through d calls refer to sub class methods If Animal d = new Animal() ; Now d object refer to Animal class properties. So depends upon initialization, it act ls different.. Called as polymorphism...

+1

OK. That all matches...Amit What is actually doubt here, .. Explanation is not there?

+1

If i don't use " =Dog() " in main() it will also give same output then why use it. Also i don't understand the meaning and use of " Animal d= Dog(); "

0

It's in c++ language

0

https://www.javatpoint.com/cpp-polymorphism

0

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();           cout<<d.color;      }