#include<iostream>
#include<string>
#include <ctime>
using namespace std;
class Dog
{
protected:
string name;
char gender;
public:
Dog(string theName, char theGender) /*constructor*/
{
name = theName;
gender = theGender;
}
~Dog()
{
cout << "good bye: " << name << endl;
}
void bark(int n)
{ cout << name << ":";
for (int i = 1; i <= n; i++)
cout << "rff" << endl;
}
void sleep()
{
cout << name << ": zzzz, zzzz" << endl;
}
void eat()
{
cout << name << ": slurp" << endl;
}
bool operator< (const Dog &other)
{
if(strcmp(this->name,
other.name) < 0)
return true;
else
return false;
}
};
class Lab: public Dog
{
public:
Lab();
private:
string color;
string getColor()
{ return color;}
void setColor()
{
int i;
cout << "1. YELLOW" << endl << "2. CHOCOLATE" << endl << "3. WHITE" << endl << "4. BLACK" << endl << "Enter the number of the color desired: ";
cin >> i;
switch(i)
{
case1:
color = "YELLOW";
break;
case2:
color = "CHOCOLATE";
break;
case3:
color = "WHITE";
break;
case4:
color = "BLACK";
break;
default:
cout << endl << "Invalid entry, please enter another number." << endl;
setColor();
}
}
};
int main ()
{ int i;
srand((unsigned)time(0));
Dog Smokey("Smokey", 'y');
Smokey.bark(i = rand() % 11);
Smokey.eat();
Smokey.sleep();
getchar();
return 0;
}
Just a question about the operator overload...I have no idea why that part won't compile. I'm trying to overload the '<' operator for use in comparing the dogs 'names', but I keep getting a
C2664 error saying that I can't convert std::string to a const char *.
Any help is greatly appreciated!