Question

In: Computer Science

Term Project C++ Pet Class Problem Specification: Design a class named Pet, which should have the...

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

Solutions

Expert Solution

#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.


Related Solutions

Design a class named Pet, which should have the following fields: Name – The name field...
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...
Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should contain: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point at (0, 0). A constructor that creates a point with specified coordinates. Get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point with specified x- and y-coordinates. Use the formula: root (x2-x1)2 +...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
In c++ Design a class named Account that contains: a.An int data field named id for...
In c++ Design a class named Account that contains: a.An int data field named id for the account. b.A double data field named balancefor the account. c.A double data field named annualInterestRate that stores the current interestrate. d.A no-arg constructor that creates a default account with id 0, balance 0, andannualInterestRate 0. e.The set and get functions for id,balance, and annualInterestRate. f.A functionearnedAmount()that calculates and returns the amount of dollars earned after one year. g.A function named printAccountInfo() that print...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT