#pragma once
#ifndef CATS_H
#define CATS_H
class Cat {
public:
Cat();
const char* getFurColor();
const char* getHairLength();
const char* getEyeColor();
private:
void setFurColor();
void setHairLength();
void setEyeColor();
char* furColor;
char* hairLength;
char* eyeColor;
};
#endif
// catsM.cpp
#include "cats.h"
#include
#include
#include
Cat::Cat()
{
srand(time(0));
setFurColor();
setEyeColor();
setHairLength();
}
const char* Cat::getFurColor()
{
/* Write function implementation here */
return furColor;
}
const char* Cat::getHairLength()
{
/* Write function implementation here */
return hairLength;
}
const char* Cat::getEyeColor()
{
/* Write function implementation here */
return eyeColor;
}
void Cat::setFurColor()
{
int x = rand() % 3;
if (x == 0)
furColor = "black";
else if (x == 1)
furColor = "gray";
else
furColor = "brown";
}
void Cat::setEyeColor()
{
int x = rand() % 3;
if (x == 0)
eyeColor = "blue";
else if (x == 1)
eyeColor = "brown";
else
eyeColor = "green";
}
// cats.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <cstring>
#include "cats.h"
void check(Cat* pen[], int);
int main()
{
const int PEN_SIZE = 7;
Cat* pen[PEN_SIZE];
for (int c = 0; c < PEN_SIZE; c++) {
/* Construct a new cat object */
*(pen + 1) = new Cat;
/* Print the new cat object抯 attributes */
/* Write call to function check here */
cout << "Press a key to add the next cat." << endl;
cin.get();
}
/* Write a statement to delete all cat objects */
return 0;
}
void check(Cat* catPen[], int numberOfCats)
{
int brownCats = 0, grayCats = 0, blueEyes = 0,
greenEyes = 0, brownEyes = 0;
for (int x = 0; x <= numberOfCats; x++) {
/* Write a series of statements which sum the total
number of each type of cat (i.e., brownCats,
greyCats, blueEyes, etc.) currently in the pen */
if (strcmp(catPen[x]->getFurColor(), "gray") == 0)
grayCats = grayCats + 1;
if (strcmp(catPen[x]->getFurColor(), "brown") == 0)
++brownCats;
if (strcmp(catPen[x]->getEyeColor(), "blue") == 0)
++blueEyes;
if (strcmp(catPen[x]->getEyeColor(), "brown") == 0)
++brownEyes;
if (strcmp(catPen[x]->getEyeColor(), "green") == 0)
++greenEyes;
}
if (grayCats > brownCats/* Write a condition that tests if */
/* gray cats are fighting with brown cats */)
cout << "The gray cats are fighting with the brown cat"
<< (brownCats > 1 ? "s." : ".") << endl;
if ((greenEyes >= 1 || blueEyes >= 1) && brownEyes/* Write a condition that tests if black cats are fighting */)
cout << "The black cat(s) with blue eyes and the black "
<< "cat(s) with green eyes are\nfighting with the black "
<< "cat that has brown eyes." << endl;
}
每次按下功能键(如Enter键)时就将一只猫放在一个围栏里,这个围栏可以容纳7只猫。每只猫都是一个类Cat的实例化对象,而该类是在cat.h中定义的。每个猫对象在动态地实例化时,都会随机地赋予一种皮毛颜色[如black(黑色)、gray(灰色)或者brown(棕色)等],还将被随机地赋予一种眼睛映射[如green(绿色)、blue(蓝色)或者brown(棕色)等]以及毛长[short(短)和long(长)]
在将猫放在围栏中时,某些组合会使其发生争斗。例如,如果灰猫的数量超过棕色猫的数量,灰猫就会与棕色猫打架。如果在围栏里有一只黑毛、棕色眼睛的猫,同时还有至少一只黑色的绿眼睛猫和至少一只黑色蓝眼睛猫,也会发生争斗。编写一段名为check的非成员函数确定猫之间是否会打架。
实例输出:
A long-haired black cat with green eyes has been added to the pan.
Press a key to add the next cat.
A short-haired black cat with blue eyes has been added to the pan.
Press a key to add the next cat.
A short-haired black cat with blue eyes has been added to the pan.
Press a key to add the next cat.
A short-haired black cat with brown eyes has been added to the pan.
The black cat(s) with blue eyes and the black cat(s) with green eyes are fighting with the black cat that has brown eyes.
Press a key to add the next cat.
A short-haired black cat with brown eyes has been added to the pan.
Press a key to add the next cat.
A short-haired gray cat with blue eyes has been added to the pan.
Press a key to add the next cat.
A short-haired gray cat with blue eyes has been added to the pan.
Press a key to add the next cat.