Question

In: Computer Science

HW_7d -   Cat class Create 3 files: Cat.h Include a class specification for a Cat class...

HW_7d -   Cat class

Create 3 files:

  1. Cat.h
    • Include a class specification for a Cat class
    • Data members include:
      • A cat’s weight
      • A cat’s color
  • Member functions (methods) include:
    • A function called setWeight, to set the cat’s weight
    • A function called setColor, to set the cat’s color
    • A function called displayInfo, that displays a cat’s age and weight on the screen
    • A function called meow, that lets the cat say “Meow” on the screen

  1. Cat.cpp
    • Include the method implementations for the four functions in this file
  1. Source.cpp
    • Include main()
      • In main(), declare three cats (3 cat objects)
      • Name them: Fluffy, Tom, and Kitty

  • Ask the user to describe Fluffy (get Fluffy’s weight and color)
  • Fluffy (the Fluffy object) calls setWeight to pass the weight to the method, where

    the weight is assigned to Fluffy’s weight.

  • Fluffy (the Fluffy object) calls setColor to pass the color to the method, where

    the color is assigned to Fluffy’s color

  • Repeat the above for the Tom and Kitty objects.

  • Have each of the Cat objects call the DisplayInfo method to output the age and color

   of each cat.

  • Have each cat say “MEOW” on the screen by having each Cat object call the

     meow method.

/* OUTPUT:

So you have three cats...

Describe Fluffy. What does she weight? 4

What color is she? brown

Describe Tom. What does he weight? 9

What color is he? orange

Describe Kitty. What does she weight? 5

What color is she? white

Fluffy weights 4 pounds and is brown.

Tom weighs 9 pounds and is orange.

Kitty weighs 5 pounds and is white.

Fluffy says: MEOW!

Tom says: MEOW!

Kitty says: MEOW!

Press any key to continue */

Code Language: C++

Please add comments for each code.

Solutions

Expert Solution

Cat.h

#include<string>

#ifndef _CAT_H

#define _CAT_H

using namespace std;

class Cat

{

//declare private members and member functions

public:

void setWeight(int);

void setColor(string);

void displayInfo();

void meow();

private:

int weight;

string color;

};

#endif

Cat.cpp

#include<string>

#include<iostream>

#include "Cat.h"

using namespace std;

void Cat::setWeight(int weight)

{

this->weight=weight;

}

void Cat::setColor(string color)

{

this->color=color;

}

void Cat::displayInfo()

{

cout<<"weighs "<<this->weight<<" pounds and its "<<this->color<<"."<<endl;

}

void Cat::meow()

{

cout<<"MEOW!"<<endl;

}

Source.cpp

#include <iostream>

#include "Cat.h"

using namespace std;

int main() {

//declare three objects and call member functions

int weight;

string color;

Cat Fluffy,Tom,Kitty;

cout<<"Describe Fluffy. What does she weight? ";

cin>>weight;

cout<<"What color is she? ";

cin>>color;

Fluffy.setWeight(weight);

Fluffy.setColor(color);

cout<<"Describe Tom. What does he weight? ";

cin>>weight;

cout<<"What color is he? ";

cin>>color;

Tom.setWeight(weight);

Tom.setColor(color);

cout<<"Describe Kitty. What does she weight? ";

cin>>weight;

cout<<"What color is she? ";

cin>>color;

Kitty.setWeight(weight);

Kitty.setColor(color);

cout<<"Fluffy ";

Fluffy.displayInfo();

cout<<"Tom ";

Tom.displayInfo();

cout<<"Kitty ";

Kitty.displayInfo();

cout<<"Fluffy says: ";

Fluffy.meow();

cout<<"Tom says: ";

Tom.meow();

cout<<"Kitty says: ";

Kitty.meow();

}

Output


Related Solutions

Refactor Assignment 1 into 3 project related files. Customer.h - Class Specification Customer.cpp - Class Implementation...
Refactor Assignment 1 into 3 project related files. Customer.h - Class Specification Customer.cpp - Class Implementation (Methods) TestCustomer.cpp - Your code that performs the logic from Assignment 1. The 3 files need to be named as listed above and should compile without errors. I am not understanding how to do this. Below is the code: #include #include using namespace std; const int NAME_SIZE = 20; const int STREET_SIZE = 30; const int CITY_SIZE = 20; const int STATE_CODE_SIZE = 3;...
Make Animal an abstract class. Create a Kennel Class Create 2-3 Dog objects Create 2-3 Cat...
Make Animal an abstract class. Create a Kennel Class Create 2-3 Dog objects Create 2-3 Cat objects Put your Dog and Cat objects in an Array of Animals Loop over your Animals and print the animal with Species, Age, and the status of the appropriate vaccines. Using the code below: public class Animal { //Declaring instance variables private int age; private boolean RabiesVaccinationStatus; private String name; private String ownerName; //Zero argumented constructor public Animal() { } //Parameterized constructor public Animal(int...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after a Cat. You should have instance variables as follows: The Cat’s name The number of mice caught by the Cat. Whether or not the Cat is secretly plotting to kill you Note that you will need to choose both good types and meaningful identifiers for each of these instance variables. You may also assume that the Cat is not automatically always secretly plotting to...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help...
Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file....
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an array of objects and practice passing objects to and return objects from functions. String functions practice has also been included. Extend the BankAccount class. The member fields of the class are:...
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter)...
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For...
In C++ Create a class called Rational (separate the files as shown in the chapter) for...
In C++ Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example,...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT