In: Computer Science
Term Project C++ Pet Class
Problem Specification:
Design a class named Pet, which should have the following fields:
• name: The name field holds the name of a pet.
• type: The type field holds the type of animal that a pet is. Example values are “Dog”, “Cat”, and “Bird”.
• age: The age field holds the pet’s age. The Pet class should also have the following methods:
• setName: The setName method stores a value in the name field.
• setType: The setType method stores a value in the type field.
• setAge: The setAge method stores a value in the age filed.
• getName: The getName method returns the value of the name field.
• getType: The getType method returns the value of the type field.
• getAge: The getAge method returns the value of the age field.
Once you have designed the class, design a program that creates an object of the class and prompts the user to enter the name, type, and age of his or her pet.
This data should be stored in the object. Use the object’s accessor methods to retrieve the pet’s name, type, and age and display this data on the screen.
Program Output (with Input Shown in Bold): Enter a pet name: Lucky Enter a pet type: German Shepherd Enter a pet age: 2 The pet name is Lucky The pet type is German Shepherd The pet age is 2
#include <iostream>
#include<string>
using namespace std;
class Pet{//class defnition
int age;
string name,type;
public:
Pet(string n,string t,int a){//constructor of class
this->age=a;
this->name=n;
this->type=t;
}
void setAge(int a){//setter methods
this->age=a;
}
void setName(string a){
this->name=a;
}
void setType(string a){
this->type=a;
}
int getAge(){//getter methods
return this->age;
}
string getType(){
return this->type;
}
string getName(){
return this->name;
}
};
int main()
{
int a;
string n,t;
cout<<"Enter a pet name:";//read inputs from user
getline(cin,n);
cout<<"Enter a pet type:";
getline(cin,t);
cout<<"Enter a pet age:";
cin>>a;
Pet p(n,t,a);//create object and display attributes
cout<<"The pet name is "<<p.getName();
cout<<" The pet type is "<<p.getType();
cout<<" The pet age is "<<p.getAge();
return 0;
}
Screenshots:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Please upvote my answer .
Thank you.