(Q.) Design and Implement an ADT class Pet for an animal hospital using separate
ID: 3852471 • Letter: #
Question
(Q.) Design and Implement an ADT class Pet for an animal hospital using separate compilation. You are required to write syntactically corret C++ code. Specifically, the animal hospital will consider the following attributes of a pet ( 1) name, (2) age in years, (3) type, which could be one of the following values.: {cat, dog, bird, others} and (4) wieght. Make sure that your ADT class will include the following member function int its interface
(1) A default constructor that initializes all the data members accordingly.
(2) A read-only accessor to return the name of a pet
(3) A read-only accessor to return the type of a pet
(4) A mutator to change the weight of a pet
(5) An overloadeded == (equal) operator to compare two pets. This operator will return true if two pets are of the same age and type, false other otherwise. This operator is not allowed to change either of the two participating Pet objects.
(6) An overloaded put operator (<<) as a friend function to print out a pet's information .
(Q.I) Write down the header file pet h . Consise commetns are required.
(Q.2)Implement the following member functions declared in the above header file in diffrent file pet.cpp
//pet.cpp starts here
#include "pet.h"
a) Implement the default constructor using an initializer(i.e., initialization segment). Feel free to choose the initial value of each data member as you see fit.
b)Implement the accessor that returns the name of a pet
c)Implement the overloaded ==(equal) operator
d)Implement the put(<<) operator
Explanation / Answer
//pet.h
#include<string>
#include<iostream>
using namespace std;
class pet
{
string name;
string type;
int age;
float weight;
public:
pet();
//overloaded constructor
void set_pet(string n, string t, int ag, float w);
string getName() const;
string getType() const;
int getAge();
float getWeight();
void setWeight(float wt);
friend void operator<<(ostream &out, pet &obj);
bool operator==(pet &rhs);
};
------------------------------------------------------
//pet.cpp
#include"pet.h"
pet::pet()
{
string name = "";
string type ="";
age = 0;
weight = 0;
}
string pet::getName() const
{
return name;
}
string pet::getType() const
{
return type;
}
int pet::getAge()
{
return age;
}
float pet::getWeight()
{
return weight;
}
void pet::set_pet(string n, string t, int ag, float w)
{
name = n;
type = t;
age = ag;
weight = w;
}
void pet::setWeight(float wt)
{
weight = wt;
}
void operator<<(ostream &out, pet &obj)
{
out << obj.getName() << " " << obj.getType() << " " << obj.getAge() << " " << obj.getWeight() << endl;
}
bool pet::operator==(pet &rhs)
{
if (age == rhs.age && type == rhs.type)
{
return true;
}
else
return false;
}
-------------------------------------------------------------------------------
//main.cpp
#include"pet.h"
int main()
{
pet pet1, pet2,pet3;
//set pet1,pet2.pet3
pet1.set_pet("cat", "domestic", 1, 5);
pet2.set_pet("lion", "wild", 1, 10);
pet3.set_pet("cat", "domestic", 1, 5.5);
cout << pet1;
cout << pet2;
cout << pet3;
if (pet1 == pet3)
{
cout << "Two pets are of same age and type" << endl;
}
else
cout << "Two pets are not of same age and type" << endl;
if (pet2 ==pet3)
{
cout << "Two pets are of same age and type" << endl;
}
else
cout << "Two pets are not of same age and type" << endl;
}
--------------------------------------------------------------------------------------------------------------------
//output
cat domestic 1 5
lion wild 1 10
cat domestic 1 5.5
Two pets are of same age and type
Two pets are not of same age and type
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.