Question #50418

A. Design a Dog class that includes fields for a breed (for example, “Labrador”) and eye color.
Include extraction and insertion operators.
B. Overload an operator*()function for the Dog class. When you multiply two Dogs, the
result is a Dog. If the two operand Dogs are the same breed, then the resulting Dog is that
breed; if the two operand Dogs have different breeds, then the resulting Dog is “Mixed”
breed. Obtain a random number of 0 or 1 for the resulting Dog’s eye color. (See Appendix E
for instructions on creating random numbers.) When the random number chosen is 0, use
the eye color from the first operand Dog for the resulting Dog’s eye color; when the random
number is 1, use the eye color of the second Dog for the result Dog.
C. Write a main()function in which you declare four parent Dogs. When you execute the
program, make sure you assign the same breed to two Dogs, and different breeds to each of
the other two Dogs. (You will have a total of three breeds for the four Dogs.) Also assign a
variety of
1

Expert's answer

2015-01-15T09:43:15-0500

Program

dogs.cpp


#include <iostream>
#include <conio.h>
#include <string>
#include <cstdlib>
using namespace std;
class Dog
{
private:
    string breed;//fields for a breed (for example, "Labrador")
    string EyeColor;//eye color
public:
    Dog(string Breed="", string eyeColor="")
    {breed=Breed;EyeColor=eyeColor;}
    //Include extraction and insertion operators.
    string getBreed() {return breed;}
    string setBreed(string Breed) {breed=Breed;}
    string getEyeColor() {return EyeColor;}
    string setEyeColor(string eyeColor) {EyeColor=eyeColor;}
    friend Dog operator *(Dog d1,Dog d2);
    void display(){cout<<"Breed: "<<breed<<"; eye color: "<<EyeColor<<endl;}
};
Dog operator *(Dog d1,Dog d2)
{
    Dog newDog;
    if(d1.getBreed() == d2.getBreed())
        newDog.setBreed(d1.getBreed());
    else
        newDog.setBreed("Mixed");
    int k = rand() %2;
    if(k == 0)
        newDog.setEyeColor(d1.getEyeColor());
    else
        newDog.setEyeColor(d2.getEyeColor());
    return Dog(newDog.getBreed(), newDog.getEyeColor());
}
int main()
{
    Dog d1("Labrador","Yellow"),
        d2("Labrador","Green"),
        d3("Dog","Yellow"),
        d4("Mops","Black");
    cout <<"dog #1: ";d1.display();
    cout <<"dog #2: ";d2.display();
    Dog dr1;
    dr1=d1*d2;
    cout<<"Result of pair dog: ";dr1.display();
    cout<<"dog #3: ";d3.display();
    cout<<"dog #4: ";d4.display();
    cout<<"Result of pair dog: ";(d3*d4).display();
    getch();
    return 1;
}

Example of execute program:

dog #1: Breed: Labrador; eye color: Yellow
dog #2: Breed: Labrador; eye color: Green
Result of pair dog: Breed: Labrador; eye color: Yellow
dog #3: Breed: Dog; eye color: Yellow
dog #4: Breed: Mops; eye color: Black
Result of pair dog: Breed: Mixed; eye color: Black


http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS