Question
Convert a structure into a class implementation. Start with the code provided to you Modify all of the functions so that they are member functions.
Convert a structure into a class implementation.
- Start with the code provided to you
- Modify all of the functions so that they are member functions. Use the scope resolution operator (::) to show that an implementation of a function is part of a class
- Change the initialization function into a constructor with arguments.
- Add a default constructor that initializes things to 0 or empty string. Please note that you should use a for loop to initialize all the values of furColours
- Add one getter to return the first furColour.
- Modify the calls in main to reflect the change in the functions.
- Be sure to add the keyword const after functions that do not change the data.
- If you want, you can turn the isTaller function into an overloaded operator>
Cat.h
#ifndef __CAT_H__
#define __CAT_H__
#include
using namespace std;
const int NUM_COL = 5;
class Cat
{
private:
double length;
double height;
double tailLength;
string eyeColour;
string furClassification; //none, short, medium, long
string furColours[5];
public:
Cat ();
string getfurColours () const;
Cat (double, double, double, string, string, const string[]);
void readCat ();
void printCat () const;
bool isCalico () const;
bool isTaller (const Cat&) const;
};
#endif
Cat.cpp
#include "Cat.h"
#include
using namespace std;
Cat::Cat()
{
length = 0;
height = 0;
tailLength = 0;
eyeColour = "";
furClassification = "";
for (int i = 0; i < NUM_COL; i++)
{
furColours[i] = "";
}
}
Cat::Cat (double l, double h, double tL, string eC, string fCl, const string furCol[])
{
int i = 0;
length = l;
height = h;
tailLength = tL;
eyeColour = eC;
furClassification = fCl; //none, short, medium, long
while (furCol[i] != "")
{
furColours[i] = furCol[i];
i++;
}
furColours[i] = "";
}
//implement the getter function
string Cat::getfurColours () const
{
return furColours[0];
}
void Cat::readCat ()
{
int i = 0;
cout << "Please describe the cat" << endl;
cout << "Please enter a length: ";
cin >> length;
cout << "Please enter a height: ";
cin >> height;
cout << "Please enter a tail length: ";
cin >> tailLength;
cout << "Please enter an eye colour: ";
cin >> eyeColour;
cout << "Please enter a description of the fur (long, medium, short, none): ";
cin >> furClassification;
cout << "Please enter the colours of the fur (separated by a space or a newline character). ";
cout << "Add "done" at the end: ";
cin >> furColours[i];
while (furColours[i] != "done")
{
i++;
cin >> furColours[i];
}
furColours[i] = "";
}
void Cat::printCat () const
{
int i = 0;
cout << "Length: "<< length << " Height: "<< height
<< " Tail Length: " << tailLength << endl;
cout << "Eye Colour: " << eyeColour
<< " Fur Classification: " << furClassification << endl;
cout << "Cat Colours: ";
while (furColours[i] != "")
{
cout << furColours[i++] << " ";
}
cout << endl;
}
bool Cat::isCalico () const
{
if (furColours[3] != "")
return false;
for (int i=0; i< 3; i++)
{
if (furColours[i] != "black" &&
furColours[i] != "orange" &&
furColours[i] != "white")
return false;
}
return true;
}
bool Cat::isTaller (const Cat& cat2) const
{
//modify
return (cat.height > cat2.height);
}
main.cpp
#include "Cat.h"
#include
using namespace std;
int main()
{
Cat myCat;
string colours[5];
colours[0] = "white";
colours[1] = "";
Cat averageCat(44, 24, 28, "blue", "medium", colours);
myCat.readCat ();
cout << "The average cat's first colour is " << averageCat.furColours[0] << endl;
cout << "-------------------------------------------------------"<< endl;
cout << "This is myCat:" << endl;
myCat.printCat ();
if (isTaller ())
{
cout << "My cat is taller than the average cat";
}
else
{
cout << "My cat is shorter than the average cat";
}
if (isCalico (myCat))
{
cout << "My cat is a calico";
}
return 0;
}
Could you please help me to fix errors in my code above..I am facing errors in isCalico function and isTaller function in main.cpp and Cat.cpp
Step by Step Solution
3.58 Rating (176 Votes )
There are 3 Steps involved in it
Step: 1
Here is the modified code with the requested changes Cath ifndef CATH define CATH include added using namespace std const int NUMCOL 5 class Cat private double length double height double tailLength s...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started