Click here to Skip to main content
16,020,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
#include <conio.h>
#include <string>
#include <cstdlib>

using namespace std;
 class Dog
{
private:
    string breed;
    string EyeColor;
public:
    Dog(string Breed="",string eyeColor="") {breed=Breed;EyeColor=eyeColor;}
     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 i=rand()%2;
        if(i==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 saheb;
    saheb=d1*d2;
    cout<<"Result of pair dog: ";
    saheb.display();
    cout<<"dog #3: ";d3.display();
    cout<<"dog #4: ";d4.display();
    cout<<"Result of pair dog: ";
    (d3*d4).display();
    getch();
    return 0;

}


What I have tried:

i tried to solve this
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 eye colors. In a loop, multiply each pair eight times, and display the results.
Posted
Updated 24-Dec-17 19:30pm
Comments
Nakhia_ind 26-Dec-17 9:40am    
how can multiple two objects like saheb=d1*d2;

1 solution

Read your homework question:
In a loop, multiply each pair eight times, and display the results.

For that, you will need a for loop, and to multiply d1 by d2, then d1 by d3, d1 by d4, d2 by d3, d2 by d4, and d3 by d4 inside the loop, printing the result each time.
 
Share this answer
 
Comments
Member 13592359 25-Dec-17 4:52am    
// so far i have done this and everything is ok now i want to Include extraction and insertion operators for breed and eyecolor. Can you please tell me how to do it.
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Dog
{
string breed;
string eyeColor;
public:
void extInsrt(int i)
{
cout << "Enter the breed of "<<i+1 <<" no dog: ";
cin >> breed;
cout << "Enter eyecolor of " << i+1 << " no dog: ";
cin >> eyeColor;
}
void showData(int i, Dog obj[4])
{
cout <<i+1<< ". The breed of " << i+1 << " no dog is: " << obj[i].func_breed()<<endl;
cout << " The eyecolor of " << i+1 << " no dog is: " << obj[i].func_eyeColor()<<endl;

}
string func_breed()
{
return breed;

}
string func_eyeColor()
{
return eyeColor;
}
Dog operator* (Dog ob)
{
Dog obj;

if (this->breed == ob.breed)
{
obj.breed = breed;
return obj;
}
else
{
obj.breed = "Mixed";
return obj;
}
}

};



int main()
{
Dog obj[4];
for (int i = 0; i < 4; i++)
{
obj[i].extInsrt(i);
}
for (int i = 0; i < 4; i++)
{
obj[i].showData(i, obj);
}
for (int i = 0; i < 8; i++)
{

int opt1;
int opt2;
int random_Number = rand() % 2;
cout << "Enter the number of two dogs [Example: for 1 no dog, input 1]";
cin >> opt1;
cin >> opt2;
Dog ob;
ob = obj[opt1 - 1] * obj[opt2 - 1];
cout <<"Breed: "<< ob.func_breed() << endl;
if (random_Number == 0)
{
cout <<"Eyecolor: "<< obj[opt1 - 1].func_eyeColor() << endl;
}
else {
cout << "Eyecolor: " << obj[opt2 - 1].func_eyeColor() << endl;
}

}


return 0;

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900