In: Computer Science
HW_7d - Cat class
Create 3 files:
the weight is assigned to Fluffy’s weight.
the color is assigned to Fluffy’s color
of each cat.
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.
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
