Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Written Assignment: Pet Class Design a class named Pet, which should have the fo

ID: 3626963 • Letter: W

Question

Written Assignment: Pet Class

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.
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 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.
You are to submit, as a Microsoft Word Document, the following for this assignment:
1. Pseudocode
Remember to follow the guidelines of good program design. Make sure to use meaningful variable names and thoroughly comment each line of your code. You may only use techniques learned in Chapters 1-7.

Explanation / Answer

Since you're designing a class, naturally you're going to want to start with a class definition:

 

class Pet {

public:

private:

}

 

Now that we have that out of the way, lets look at the fields that need to be added; for your assignment you need name, type, and age. Name and type would be strings according to your specification, and age could be any type of number (I will use integer). All we have to do is declare these in the private area of the class:

 

class Pet {

public:

private:

string name;

//etc...

}

 

Now you have to create the accessor and mutator functions, which will be in the public area so that the program can call them later on. Accessor functions will return the values of the fields you defined in the private area, and mutator functions will not return anything, but will modify the value of the fields:

 

class Pet {

  public:

void setName(string value){

//change the value of the field in the class to be the value in the function

}

//etc...

string getName(){

//return the name field's contents

}

  private:

  string name;

  //etc...

}

 

Now, once you have your class created, you're going to be writing a program to create an object of this class. Therefore, you will need to create an object of type Pet, for example:

myClass c();

In this case, c would be an instance of myClass, and you would be able to use "dot-notation" in order to call the functions, for example:

string f = c.getSomething();

I hope this puts you in the right direction :)