C++ Introduction to Programming It is short and simple practice. Please solve #1
ID: 3572179 • Letter: C
Question
C++ Introduction to Programming
It is short and simple practice.
Please solve #1,2,3,4,5,6,7
#include<iostream>
#include<vector>
...
using namespace std;
Operator Overloading; Inheritance and Abstract data types
Advanced inheritance, abstract data types, and templates
Here's a class hierarchy that will be used in several of the problems (Instead of writing a separate answer for each question, just the final rite answer after you've made all the modifications required by the problems struct animal float eight struct pet public animal string name; struct dog public pet bool can fetch false struct cat public pet int lives left 9 (Remember that struct is just a version of class which defaults to public rather than private' access.) 1) Add a virtual method is tame to the hierarchy which should return true for all pet's but false for animals in general 2) Add a virtual method fetch to pet By default this does nothing, but for dogs it should check can fetch and print a message if the dog actually can fetch 3) Add a pure virtual method speak to pet which should print a message typical of a particular kind of pet. E. g dogs bark while cats meow. d the existing virtual methods 3) Ad d a subclass t for rabbit d exten o pet an to it rite a class pet owner which HAS-A ector of p et pointers Give this 4) Wi class a method pets speak hich just has all the owned pets do their speak ing E.g., for a pet owner who had a cat and a dog, running the pets speak ethod might print Meow Woof 5) On the pet owner class overload the operator operator to take an unsigned in i' and return the i-th pet owned by that owner. (That is, this acts like at on the vector of pets. YOUR ANSWER HERE suppose we added a class animal owner which contained a vector o is this class related to pet owner? nimal pointers Conceptually how Can an animal owner do everything a pet owner can making animal owner a subtype of pet owner)? Or is it the other way around, that pet owner can do everything an animal owner class so that pet owner is a subtype of animal owner Or is it some other kind of relationship YOUR ANSWER HEREExplanation / Answer
As per the Chegg policy we can answer upto 4 questions. Please don't mind.
Here i have answered 5 questions.
#include<iostream>
using namespace std;
struct animal{
float weight;
virtual bool is_tame(void){ return false;}
};
struct pet:public animal{
string name;
bool can_fetch;
virtual bool is_tame(void){ return true;}
virtual void fetch(void){
if(can_fetch)
cout<<"The "<<name<<" can fetch"<<endl;
else
cout<<"The "<<name<<" can't fetch"<<endl;
}
virtual void speak(void) =0;
};
struct dog:public pet{
bool can_fetch;
virtual void speak(void){
cout<<"Dogs bark"<<endl;
}
};
struct cat:public pet{
int lives_left;
virtual void speak(void){
cout<<"Cat meow"<<endl;
}
};
struct rabbit:public pet{
virtual void speak(void){
cout<<"Rabbits snore"<<endl;
}
};
class pet_owner{
pet **p;
int n;//number of pets
void addPets(void)
{
int t=0;
for(int i=0;i<n;i++)
{
cout<<"1.Dog"<<endl;
cout<<"2.Cat"<<endl;
cout<<"3.Rabbit"<<endl;
cin>>t;
switch(t){
case 1:
p[i]=new dog;
break;
case 2:
p[i]=new cat;
break;
case 3:
p[i]=new rabbit;
break;
}
}
}
public:
pet_owner(int n){
this->n=n;
p= new pet*[n];
addPets();
}
void pet_speaks(){
for(int i=0;i<n;i++)
p[i]->speak();
}
pet * operator [](int i)
{
return p[i];
}
};
int main(void)
{
pet_owner pt(2);
pt.pet_speaks();
pet *p=pt[1];
p->speak();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.