- Code: Select all
#include <iostream>
using std::cout;
using std::cin;
class Cow
{
const char *name;
int Utters;
bool alive;
public:
Cow(const char *_name) : name(_name), Utters(4), alive(true) {}
const char * getName() const {return name;}
void KillCow() {alive = false;}
bool isAlive() const {return alive;}
};
int main(int argc, char *argv[])
{
Cow cow("Mooo");
cout << "COW "<< cow.getName() << " IS ALIVE\n";
cow.KillCow();
if (!cow.isAlive()) cout << "COW "<< cow.getName() << " HAS DIED\n";
else cout << "COW "<< cow.getName() << " HAS SURVIVED\n";
cin.get();
}
I detect five problems
1. Class cow is undefined. It doesn't have a status. You mention it as if the class itself has a status. It's merely a blueprint.
2. Your instance of Cow does not have a status either, so you can't state it's alive, because you don't have a constructor that initializes the instance.
3. A ; was missing after } of class Cow.
4. bool alive is private and you can only interact with it through a method.
5. char f[] is undefined. The compiler doesn't know the size of f.
Some recommendations...
Use cin.get() instead of declaring a char to get input you don't need to store.
Don't bring in the entire namespace std. Doing that will bring in every symbol and library implementations into your program.
Instead tell the compiler what symbols to use (example: std::cout;) or just use the std:: before a symbol you use.