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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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

    }

};

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY