c++ int main() { //step 1: set up dog class, implement default constructors dog bonny; dog wimpy; //step 2: set two different parameterized constructors dog fido("rottweiler", 200); dog snuffles(15); //step 3: create "set" methods bonny.setBreed("pit bull"); bonny.setWeight(500); //Make your "set" method such that //you can't set the dog's weight to be negative. wimpy.setWeight(-300); //step 4: create a speak method bonny.speak(); //I'm a 500 lbs pit bull wimpy.speak(); //I'm a 0 lbs mutt fido.speak(); //I'm a 200 lbs rottweiler snuffles.speak(); //I'm a 15 lbs mutt //step 5: switch bodies! bonny.switchBodies(fido); bonny.speak(); //I'm a 200 lbs rottweiler fido.speak(); //I'm a 500 lbs pit bull //step 6: love is in the air dog puppy; puppy = fido.mateWith(wimpy); puppy.speak(); //I'm a 250 lbs pit bull-mutt //step 7: uh oh, they're fighting... puppy.bite(fido); puppy.speak(); //I'm a 260 lbs pit bull-mutt fido.speak(); //I'm a 490 lbs pit bull //Advanced stuff (for students seeking an A) //step 8: let's feed that puppy puppy++; puppy.speak(); //I'm a 360 lbs pit bull-mutt return 0; }
#include <iostream>
using namespace std;
//class dog implementation
class dog{
public:
//default constructor
dog(){
this->weight = 0;
this->breed = "mutt";
}
//single parameter constructor
dog(int weight){
this->weight = weight;
this->breed = "mutt";
}
//two parameter constructor
dog(string breed, int weight){
this->breed = breed;
this->weight = weight;
}
//to set the breed of the dog
void setBreed(string breedName){
breed = breedName;
}
//to set the weight of the dog
void setWeight(int weightValue){
if (weightValue < 0){
//cout<<"you can't set the dog's weight to be
negative."<<endl;
weight = 0;
return;
}
weight = weightValue;
}
//prints the weight and breed of the dog
void speak(){
cout<<"I'm a "<<this->weight << " lbs "
<< this->breed<<endl;
}
//swaps the dog's characteristics
void switchBodies(dog &obj){
dog tmp;
tmp.weight = this->weight;
tmp.breed = this->breed;
this->weight = obj.weight;
this->breed = obj.breed;
obj.weight = tmp.weight;
obj.breed = tmp.breed;
}
//mate with other dog
dog mateWith(dog &obj){
dog newPuppy;
newPuppy.weight = (int)(this->weight + obj.weight)/2;
newPuppy.breed = this->breed + "-" + obj.breed;
return newPuppy;
}
//bites the other dog
void bite(dog &obj){
this->weight = this->weight+10;
obj.weight = obj.weight-10;
}
//operator overloading of post increment operator
dog operator ++ (int){
dog temp ;
temp.weight = this->weight+100;
this->weight = temp.weight;
return temp;
}
//variables
private:
string breed;
int weight;
};
int main()
{
//step 1: set up dog class, implement default constructors
dog bonny;
dog wimpy;
//step 2: set two different parameterized constructors
dog fido("rottweiler", 200);
dog snuffles(15);
//step 3: create "set" methods
bonny.setBreed("pit bull");
bonny.setWeight(500);
//Make your "set" method such that
//you can't set the dog's weight to be negative.
wimpy.setWeight(-300);
//step 4: create a speak method
bonny.speak(); //I'm a 500 lbs pit bull
wimpy.speak(); //I'm a 0 lbs mutt
fido.speak(); //I'm a 200 lbs rottweiler
snuffles.speak(); //I'm a 15 lbs mutt
//step 5: switch bodies!
bonny.switchBodies(fido);
bonny.speak(); //I'm a 200 lbs rottweiler
fido.speak(); //I'm a 500 lbs pit bull
//step 6: love is in the air
dog puppy;
puppy = fido.mateWith(wimpy);
puppy.speak(); //I'm a 250 lbs pit bull-mutt
//step 7: uh oh, they're fighting...
puppy.bite(fido);
puppy.speak(); //I'm a 260 lbs pit bull-mutt
fido.speak(); //I'm a 490 lbs pit bull
//Advanced stuff (for students seeking an A)
//step 8: let's feed that puppy
puppy++;
puppy.speak(); //I'm a 360 lbs pit bull-mutt
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.