What does mean from. Animal d=Dog(); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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

13th Sep 2020, 1:47 PM
Amit
8 Answers
+ 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..?
13th Sep 2020, 3:25 PM
Jayakrishna 🇮🇳
+ 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...
16th Sep 2020, 8:23 PM
Jayakrishna 🇮🇳
+ 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...
13th Sep 2020, 2:20 PM
Jayakrishna 🇮🇳
+ 1
OK. That all matches...Amit What is actually doubt here, .. Explanation is not there?
13th Sep 2020, 4:30 PM
Jayakrishna 🇮🇳
+ 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(); "
13th Sep 2020, 4:36 PM
Amit
0
It's in c++ language
13th Sep 2020, 2:46 PM
Amit
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;      }    
13th Sep 2020, 3:37 PM
Amit