In: Computer Science
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 is the pet. 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 field.
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.
please write in c++ format
Code :-
#include <iostream> // this is for standerd input output
using namespace std; // to create seprate namespace and use cout cin directly
class pet { // pet class with private data Age,Name and
Type
private: // declration of data member
int Age;
string Name;
string Type;
public: // this all method must be public as we are calling it
from outside of this class
// Setter
void setAge(int a) { // setter method to set a
Age of your pet it is void type as this return nothing
Age = a;
}
// Getter
int getAge() { // This is getter method to
access or get the value of the Age of pet
return Age;
}
void setName(string a) {// setter method
to set a Name of your pet it is void type as this return
nothing
Name = a;
}
// Getter
string getName() {// This is getter method to
access or get the value of the name of pet
}
void setType(string a) { // setter method
to set a Type of your pet it is void type as this return
nothing
Name = a;
Type = a;
}
// Getter
string getType() {// This is getter method to access
or get the value of the Type of your pet
return Type;
}
};
int main() // our main function
{
pet myObj; // creating object of
type pet
myObj.setType("dog"); // assign value of Age
,Name and Type of pet using getter methods
myObj.setAge(50000);
myObj.setName("jony");
cout <<" name of pet : "<<
myObj.getName()<<endl; // get back value of Age ,Name and
Type of pet using setter methods and print it
cout <<" type of pet : "<<
myObj.getType()<<endl;
cout <<" age of pet : "<<
myObj.getAge()<<endl;
return 0;
}
OUTPUT:--
UML DIagram of class pet: -