In: Computer Science
Short Summary:
**************Please upvote the answer and appreciate our time.************
UML DIAGRAM:
Source Code:
Pet.h:
// Create a Pet class that contains the attributes in the class
diagram.
#ifndef PET_H
#define PET_H
#include <iostream>
using namespace std;
class Pet{
public:
// default constructor
Pet();
// parameterized constructor
Pet(string,string,float);
// mutators
void setType(string);
void setBreed(string);
void setWeight(float);
// Accessors
string getType();
string getBreed();
float getWeight();
// displays all the data about the pets
void displayPetData();
private:
string petType;
string petBreed;
float weight;
};
#endif
Pet.cpp:
#include "Pet.h"
#include <iostream>
using namespace std;
// default constructor
Pet :: Pet(){
petType = "";
petBreed = "";
weight = 0;
}
// parameterized constructor
Pet :: Pet(string type,string breed,float wt){
petType = type;
petBreed = breed;
weight = wt;
}
// mutators
void Pet :: setType(string type){
petType = type;
}
void Pet :: setBreed(string breed){
petBreed = breed;
}
void Pet :: setWeight(float wt){
weight = wt;
}
// Accessors
string Pet :: getType(){
return petType;
}
string Pet :: getBreed(){
return petBreed;
}
float Pet :: getWeight(){
return weight;
}
// displays all the data about the pets
void Pet :: displayPetData(){
cout << "Type : " << getType() << "\nBreed : "
<< getBreed() << "\nWeight : " << getWeight()
<< endl;
}
main.cpp:
#include "Pet.h"
#include <iostream>
using namespace std;
int main()
{
// first object should be named dog and use the default
constructor.
Pet dog = Pet();
// second object should be named cat and use the parameterized
constructor
Pet cat = Pet("cat","Siamese",3);
// A call to set the type of the dog to "Large Dog".
dog.setType("Large Dog");
// A call to set the breed of the dog to "German Shephard".
dog.setBreed("German Shephard");
// A call to set the weight of the dog to 50.
dog.setWeight(50);
// A Statement that displays the breed of the cat, using the
appropriate method call.
cout << "Cat Breed : " << cat.getBreed() <<
endl;
// A call to change the weight of the dog to 45.
dog.setWeight(45);
// A statement that displays the weight of the dog, using the
appropriate method call.
cout << "Dog Weight : " << dog.getWeight() <<
endl;
// A call for the cat object to the method that displays all the
information about the cat.
cout << "**** CAT DATA ****" << endl;
cat.displayPetData();
// A call for the dog object to the method that displays all of the
information about the dog.
cout << "**** DOG DATA ****" << endl;
dog.displayPetData();
return 0;
}
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************