Hello, I am having trouble with tis homework assignments for c++. I have provided my code below as I need help adding another ==> (Pet) Assignment operator was called. within the code and I am at a loss.
Hello, I am having trouble with tis homework assignments for c++. I have provided my code below as I need help adding another ==> (Pet) Assignment operator was called. within the code and I am at a loss.
2. Copy the previous program to a new file. Next, define a Dog class that is derived from Pet. The Dog class should have a private member variable named breed that stores the breed of the dog. Use dynamic variable for breed. Add mutator and accessor functions for the breed variable and appropriate constructors. Then, write the big three for Dog class. Redefine the getLifespan function to return "Approximately 7 years" if the dog's weight is over 100 pounds and "Approximately 13 years" if the dog's weight is under 100 pounds. (Please indicate call of copy constructor, assignment operator, and destructor)
Use following main() to test your class.
int main(){
Dog d("Doggy",2,10,"Boxer");
cout<<d.getName()<<endl;
cout<<d.getAge()<<endl;
cout<<d.getWeight()<<endl;
cout<<d.getBreed()<<endl;
cout<<d.getLifespan()<<endl;
Dog d1 = d,d2;
d1.setBreed("Akita");
cout<<d1.getName()<<endl;
cout<<d1.getAge()<<endl;
cout<<d1.getWeight()<<endl;
cout<<d1.getBreed()<<endl;
cout<<d1.getLifespan()<<endl;
d2 = d1;
d2.setName("King");
cout<<d2.getName()<<endl;
cout<<d2.getAge()<<endl;
cout<<d2.getWeight()<<endl;
cout<<d2.getBreed()<<endl;
cout<<d2.getLifespan()<<endl;
}
Output from main function above:
Doggy
2
10
Boxer
Approximately 7
==> (Dog) copy constructor was called
Doggy
2
10
Akita
Approximately 7
==> (Dog) Assignment operator was called
==> (Pet) Assignment operator was called ( This is what I am having trouble adding)
King
2
10
Akita
Approximately 7
==> (Dog) Destructor was called
==> (Pet) Destructor was called
==> (Dog) Destructor was called
==> (Pet) Destructor was called
==> (Dog) Destructor was called
==> (Pet) Destructor was called
This is the code I have so far :
#include<iostream>
using namespace std;
class Pet{
public:
char *name;
int age;
double weight;
public :
Pet(){
name = NULL;
age = 0;
weight = 0;
}
Pet(char *n , int a , double w){
name = n;
age = a;
weight = w;
}
char *getName(){return name;}
int getAge(){return age;}
double getWeight(){return weight;}
string getLifespan(){return "Unknown Lifespan";}
void setName(char *n){name=n;}
void setAge(int a){age=a;}
void setWeight(double w){weight=w;}
void setValues(char *n , int a, float w){
name = n;
age = a;
weight = w;
}
Pet(Pet &pt){
cout<<"==>(pet)Copy Constructor was called"<<endl;
name = pt.name;
age = pt.age;
weight = pt.weight;
}
void operator= (Pet &pt){
cout<<"==>(pet)Assignment operator was called "<<endl;
name = pt.name;
age = pt.age;
weight = pt.weight;
}
~Pet(){
cout<<"\n==>(pet)Destructor was Called";
}
};
class Dog : public Pet{
public:
char *breed, *name;
int age;
double weight;
public:
Dog() {
}
Dog(char *n, int a, double w, char * b){
name = n;
age = a;
weight = w;
setValues(name,age,weight);
breed = b;
}
char *getBreed(){
return breed;
}
void setBreed(char *b){
breed=b;
}
string getLifespan(){
if(weight<100){
return "Approximately 7 years";
}
else{
return "Approximately 13 years";
}
}
Dog(Dog &d){
cout<<"==>(Dog)Copy Constructor was called"<<endl;
name=d.name;
age=d.age;
weight=d.weight;
setValues(name,age,weight);
breed=d.breed;
}
void operator= (Dog &d){
cout<<"==>(Dog)Assignment operator was called "<<endl;
name=d.name;
age=d.age;
weight=d.weight;
setValues(name,age,weight);
breed=d.breed;
}
~Dog(){
cout<<"\n==>(Dog)Destructor was called";
}
};
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images