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...


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<><>


 cout<><>


 cout<><>


 cout<><>


 cout<><>


 Dog d1 = d,d2;


 d1.setBreed("Akita");


 cout<><>


 cout<><>


 cout<><>


 cout<><>


 cout<><>


 d2 = d1;


 d2.setName("King");


 cout<><>


 cout<><>


 cout<><>


 cout<><>


 cout<><>


}



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


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"<>


      name = pt.name;


      age = pt.age;


      weight = pt.weight;


    }


    void operator= (Pet &pt){


      cout<"==>(pet)Assignment operator was called "<>


      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<>


      return "Approximately 7 years";


      }


      else{


     return "Approximately 13 years";


      }


    }


    Dog(Dog &d){


       cout<"==>(Dog)Copy Constructor was called"<>


      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 "<>


      name=d.name;


      age=d.age;


      weight=d.weight;


      setValues(name,age,weight);


      breed=d.breed;


    }


    ~Dog(){


      cout<"\n==>(Dog)Destructor was called";


    }


};

Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here