Question

In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

In C++

***//Cat.h//***

#ifndef __Cat_h__
#define __Cat_h__
#include <string>
using namespace std;

struct Cat
{
double length;
double height;
double tailLength;
string eyeColour;
string furClassification; //long, medium, short, none
string furColours[5];
};

void initCat (Cat&, double, double, double, string, string, const string[]);
void readCat (Cat&);
void printCat (const Cat&);
bool isCalico (const Cat&);
bool isTaller (const Cat&, const Cat&);

#endif

***//Cat.cpp//***

#include "Cat.h"
#include <iostream>
using namespace std;

void initCat (Cat& cat, double l, double h, double tL, string eC, string fCl, const string furCol[])
{
int i = 0;
cat.length = l;
cat.height = h;
cat.tailLength = tL;
cat.eyeColour = eC;
cat.furClassification = fCl; //long, medium, short, none
while (furCol[i] != "")
{
cat.furColours[i] = furCol[i];
i++;
}
cat.furColours[i] = "";

}

void readCat (Cat& cat)
{
int i = 0;
cout << "Please decribe the cat" << endl;
cout << "Please enter a length: ";
cin >> cat.length;
cout << "Please enter a height: ";
cin >> cat.height;
cout << "Please enter a tail length: ";
cin >> cat.tailLength;
cout << "Please enter an eye colour: ";
cin >> cat.eyeColour;
cout << "Please enter a description of the fur (long, medium, short, none): ";
cin >> cat.furClassification;
cout << "Please enter the colours of the fur (separated by a space or a newline character). ";
cout << "Add \"done\" at the end: ";
cin >> cat.furColours[i];
while (cat.furColours[i] != "done")
{
i++;
cin >> cat.furColours[i];
}
cat.furColours[i] = "";
}

void printCat (const Cat& cat)
{
int i = 0;
cout << "Length: "<< cat.length << " Height: "<< cat.height
<< " Tail Length: " << cat.tailLength << endl;
cout << "Eye Colour: " << cat.eyeColour
<< " Fur Classification: " << cat.furClassification << endl;
cout << "Cat Colours: ";
while (cat.furColours[i] != "")
{
cout << cat.furColours[i++] << " ";
}
cout << endl;
}

bool isCalico (const Cat& cat)
{
if (cat.furColours[3] != "")
return false;
for (int i=0; i< 3; i++)
{
if (cat.furColours[i] != "black" &&
cat.furColours[i] != "orange" &&
cat.furColours[i] != "white")
return false;
}
return true;
}

bool isTaller (const Cat& cat1, const Cat& cat2)
{
return (cat1.height > cat2.height);
}

***// main.cpp //***

#include "Cat.h"
#include <iostream>
using namespace std;

int main()
{
Cat averageCat, myCat;

string colours[5];
colours[0] = "brown";
colours[1] = "";
initCat (averageCat, 46, 24, 30, "green", "medium", colours);

readCat (myCat);

cout << "The average cat has height " << averageCat.height << endl;

cout << "-------------------------------------------------------"<< endl;
cout << "This is myCat:" << endl;
printCat (myCat);
if (isTaller (myCat, averageCat))
{
cout << "My cat is taller than the average cat\n";
}
else
{
cout << "My cat is shorter than the average cat\n";
}
if (isCalico (myCat))
{
cout << "My cat is a calico\n";
}

return 0;
}

1) 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

2) Change the initialization function into a constructor with arguments.

3) Add a default constructor that initializes things to 0 or empty string.

4) Add one getter to return the height.

5) Modify the calls in main to reflect the change in the functions.

6) Be sure to add the keyword const after functions that do not change the data.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for Cat.h:

//***//Cat.h//***

#ifndef __Cat_h__
#define __Cat_h__
#include <iostream>
#include <cstring>
using namespace std;
class Cat
{
double length;
double height;
double tailLength;
string eyeColour;
string furClassification; //long, medium, short, none
string furColours[5];
public:
   Cat();
   Cat(double, double, double, string, string, const string[]);
   void readCat();
   void printCat();
   bool isCalico();
   bool isTaller(const Cat&);
   double getHeight();
};
#endif

Here is the code for Cat.cpp:

//***//Cat.cpp//***
#include "Cat.h"
#include <iostream>
using namespace std;

Cat::Cat()
{
length = 0;
height = 0;
tailLength = 0;
eyeColour = "";
furClassification = "none"; //long, medium, short, none
for(int i = 0; i < 5; 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; //long, medium, short, none
while (furCol[i] != "")
{
furColours[i] = furCol[i];
i++;
}
furColours[i] = "";
}
void Cat::readCat ()
{
int i = 0;
cout << "Please decribe 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 ()
{
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 ()
{
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)
{
return (height > cat2.height);
}

double Cat::getHeight()
{
   return height;
}

And the code for CatDemo.cpp is:

//***// main.cpp //***
#include "Cat.cpp"
//#include <iostream>
using namespace std;
int main()
{
Cat myCat;
string colours[5];
colours[0] = "brown";
colours[1] = "";
Cat averageCat (46, 24, 30, "green", "medium", colours);
myCat.readCat();
cout << "The average cat has height " << averageCat.getHeight() << endl;
cout << "-------------------------------------------------------"<< endl;
cout << "This is myCat:" << endl;
myCat.printCat();
if (myCat.isTaller(averageCat))
{
cout << "My cat is taller than the average cat\n";
}
else
{
cout << "My cat is shorter than the average cat\n";
}
if (myCat.isCalico())
{
cout << "My cat is a calico\n";
}
return 0;
}

And the output screenshot is:

Add a comment
Know the answer?
Add Answer to:
In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be...

    #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem;...

    #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem; double menuPrice; }; void getData(menuItemType menuList[]); void showMenu(menuItemType menuList[], int x); void printCheck(menuItemType menuList[], int menuOrder[], int x); int main() { const int menuItems = 8; menuItemType menuList[menuItems]; int menuOrder[menuItems] = {0}; int orderChoice = 0; bool ordering = true; int count = 0; getData(menuList); showMenu(menuList, menuItems); while(ordering) { cout << "Enter the number for the item you would\n" << "like to order, or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT