Question

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:
media%2F512%2F5123b802-87d1-4dc8-a6e7-91
media%2F707%2F7072d8d8-d5b3-4f9f-9fa8-82
media%2Fa41%2Fa41f1549-acec-4412-9052-f1I do not understand the output when using dog.print()

Can you explain what happens when Dogtype dog("Tommy", "German....."); and dog.print() executes.
This is c++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
I do not understand the output when using dog.print() Can you explain what happens when Dogtype...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi all, I need help to do a project based on C++ smart pointers. I have...

    Hi all, I need help to do a project based on C++ smart pointers. I have to implement the class template my_unique_ptr which is a pointer management class template. Also, it's required to implement the following public methods: a. Default constructor that initializes the object to point to nullptr. b. Constructor that takes a pointer Type * as a parameter and sets the object to point there. We say that the newly created object takes ownership of the pointed memory....

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • what does this output? Derived *d = new Derived; delete d; and Base b; Derived d;...

    what does this output? Derived *d = new Derived; delete d; and Base b; Derived d; given: class Base { public: Base() {std::cout << "Base Default Constructor" << std::endl;} Base(Base &base) {std::cout << "Base Copy Constructor" << std::endl;} virtual ~Base() {std::cout << "Base Destructor" << std::endl;} Base &operator=(const Base &base) {std::cout << "Base Overloaded Assignment" << std::endl; return *this;} }; class Derived : public Base { public: Derived() {std::cout << "Derived Default Constructor" << std::endl;} ~Derived() {std::cout << "Derived Destructor"...

  • please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG can be instantiated. 2. To do so, we must override the pure virtual function clone() in the base class. • The class declaration is given below. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

  • C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test...

    C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test it) :- -You may need getters and setters also. 1. Declare and define an animal base class: animal stores an age (int), a unique long ID (a boolean that is true if it is alive, and a location (a pair of double). animal requires a default constructor and a 3 parameter constructor. Both constructors should set the unique ID automatically. They should also set...

  • PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed...

    PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed with a list of nodes, and the first node pointer points to the front I a) (10 pts) Complete the following class declaration which is similar to Linklist class ecter class (Write only the prototypes and the private data field; definitions will follow the class declaration) template stypenane T> olass Linkedveoter t private olass Node publie T info Node "next typedet Node *nodePtr publie...

  • This is assignment and code from this site but it will not compile....can you help? home...

    This is assignment and code from this site but it will not compile....can you help? home / study / engineering / computer science / computer science questions and answers / c++ this assignment requires several classes which interact with each other. two class aggregations ... Question: C++ This assignment requires several classes which interact with each other. Two class aggregatio... (1 bookmark) C++ This assignment requires several classes which interact with each other. Two class aggregations are formed. The program...

  • Code in C++. Can someone make it so that the code below can be compiled? ▪...

    Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...

  • Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car...

    Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car class //Defined enum here enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger}; //Defined array of strings string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"}; class Car { private: string reportingMark; int carNumber; Kind kind; //changed to Kind bool loaded; string destination; public: //Defined setKind function Kind setKind(string knd) { for(int i=0;i<8;i++) //repeat upto 8 kinds { if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array kind=(Kind)i; //setup that kind } return kind; } //Default constructor Car() { } //Parameterized constructor...

  • Please use C++ and Please do all file separate separately. And also I need output too....

    Please use C++ and Please do all file separate separately. And also I need output too. thanks. Please do it complete work. ShapeNodePoly.cpp Avaliable from: Wednesday, July 27, 2016, 10:20 AM Requested files: Point.h, Point.cpp, Shape.h, Shape.cpp, Polygon.h, Polygon.cpp, Ellipse.h, Ellipse.cpp, ShapeNodePoly.cpp, ShapeNodePoly_test.cpp (Download) Type of work: Individual work In this assignment, you will create a class that can behave as any of the shapes listed below. You will use object inheritance to enable all shapes to be managed using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT