Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++ to complete the following program: class Pet { protected: string type;

ID: 3573260 • Letter: U

Question

Using C++ to complete the following program:

class Pet
{
protected:
    string type;
    string name;
public:
    Pet(const string& arg1, const string& arg2);
    virtual void describe() const;
    virtual string speak() const = 0;
};

class Dog : public Pet
{
public:
    void describe() const; // override the describe() function
?
};

class Cat : public Pet
{
?
  // Do not override the describe() function
};

ostream& operator<<(ostream& out, const Pet& p)
{
  p.describe();
    out << "I say " << p.speak();
    return out;
}

int main()
{
    Dog fido("dog","Fido");
    Cat garfield("cat","Garfield");
    Pet* ptr = &fido;
    cout << *ptr << endl;
    ptr = &garfield;
    cout << *ptr << endl;
}

OUTPUT

I am an excellent dog and you may refer to me as Fido
I say woof
I am a cat and my name is Garfield
I say meow

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

#include <iostream>
#include <cstdlib>
using namespace std;

class Pet
{
protected:
string type;
string name;
public:
Pet(const string& arg1, const string& arg2);
virtual void describe() const;
virtual string speak() const = 0;
};

class Dog : public Pet
{
public:
Dog(const string& arg1, const string& arg2):Pet(arg1, arg2){

}
void describe() const{
cout<<"I am an excellent dog and you may refer to me as "<<name<<endl;
}

string speak() const{
return type;
}

};

class Cat : public Pet
{
public:
Cat(const string& arg1, const string& arg2):Pet(arg1, arg2){

}

void describe() const{
cout<<"I am an excellent dog and you may refer to me as "<<name<<endl;
}

string speak() const{
return type;
}
};

ostream& operator<<(ostream& out, const Pet& p)
{
p.describe();
out << "I say " << p.speak();
return out;
}

int main()
{
Dog fido("dog","Fido");
Cat garfield("cat","Garfield");
Pet* ptr = &fido;
cout << *ptr << endl;
ptr = &garfield;
cout << *ptr << endl;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote