Can you explain what happens when Dogtype dog(\"Tommy\", \"German.....\"); and d
ID: 3886738 • Letter: C
Question
Can you explain what happens when Dogtype dog("Tommy", "German....."); and dog.print() executes. This is c++ For classes 1. Include the destruc 2. 3. Include the copy constructor. tor in lI Overload the assignment operator for the class r 13 discusses overloading the assignment operator. Until Chapte discuss classes with pointer member variables, out of the three iten we will implement only the destructor and the copy constructor. Inheritance, Pointers, and Virtual Fu Recall that as a parameter, a class object can be passed either by valu chapters also said that the types of the actual and formal parameters r the case of classes, C++ allows the user to pass an object of a derived class base dass type. This is because the derived class has the base class as i parameter recognizes the base class portion of the derived class an First, let us discuss the case in which the formal parameter is eit or a pointer. To be specific, let us consider the following class class petType public:
Explanation / Answer
Note : if u didnt understood this explanation just tell me.So that i can explain more clearly...Thank You.
___________
Here in this program petType class is the base class and dogType class is the child class of petType class.
In the main() function when the statement
petType pet(“Lucky”);
is executed ,actually it is creating an instance of petType class by using its parameter constructor by passing the name as “Lucky”.
By doing this an object to base class will be created and its instance variable name is set to “Lucky”
When Coming to the next statement ,
dogType dog(“Tommy”,”German Shepherd”);
is executed subclass (dogType) instance will be created .But while creating the sub class object ,a call to base class will create base class object and sets its instance variable name =”Tommy” (As we are inheriting the base class from child class)
and also sets the child class instance variable breed = “German Shepherd”;
Next when pet.print() ; statement is executed, as we are using the reference of base class(pet) while calling the print() method then base class(i.e petType) print() method will be called.
So this method displays the name of the pet.So it prints Name : Lucky
Next when the statement dog.print() method is executed,as we are calling the print() method by using the child class reference (i.e dog).Now the child class print() method will be executed.
So the block of code wil be executed.
void dogType:: print()
{
petType::print();
cout<<”, Breed: “<<breed<<endl;
}
But inside this block again a call to the base call will be created .so it prints
Name : Tommy and later the next statement is executed
cout<<”, Breed: “<<breed<<endl;
so it prints ,Breed : German Shepherd.
So overall when we call the sub class print() method
Name : Tommy , Breed : German Shepherd will be displayed in the output screen.
____________________
New added Explanation regarding to ur doubt
//This is the child class parameter constructor implementation
dogType::dogType(string n, string b) : petType(n)
{
this->breed = b;
}
When we call this statement ----> dogType dog(“Tommy”,”German Shepherd”); German Shepard is assigend to the variabe breed and Tommy is passed to the base class petType(n) so that name will be assigne to "Tommy"
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.