Hey guys,

I was just reading again about classes and their functions and i made some code, the thing i want to do is have a function, eg.

void Cat::Sleep()

and have it to have three cout's and choose one of them to say at random,

Heres my code.

#include <iostream>
#include <conio.h>

using namespace std;

class Cat
{
      public:
      
      unsigned int age;
      unsigned int weight;
      
      void Meow();
      void Feed();
      void Sleep();
};

void Cat::Meow()
{
     cout << "Meow" << endl;
}

void Cat::Feed()
{
     cout << "Meoww (Cat's Happy To Eat)" << endl;
     cout << "Psshhh (Cat Is Not Hungry!)" << endl;
     cout << "Zzz (The Cat's Asleep, Leave It Alone)" << endl;
}

void Cat::Sleep()
{
     cout << "Meowww Zzz (Cat Has Fallen Asleep)" << endl;
     cout << "Psshhh (The Cat Is Not Tired)" << endl;
     cout << "Zzz (The Cat Is Already Asleep)" << endl;
}

int main()
{
    Cat BlackMagic;
    
    BlackMagic.Feed();
    BlackMagic.Sleep();
    
    getch();
}

Thank you!

#include <iostream>
#include <conio.h>

using namespace std;

class Cat
{
      public:
      
      unsigned int age;
      unsigned int weight;
      
      void Meow();
      void Feed();
      void Sleep();
};

void Cat::Meow()
{
     cout << "Meow" << endl;
}

void Cat::Feed()
{
     
     const char *Feed[] = 
     { 
           "Meoww (Cat's Happy To Eat)\n", 
           "Psshhh (Cat Is Not Hungry!)\n",
           "Zzz (The Cat's Asleep, Leave It Alone)\n"
           }; 
           
           cout << Feed[ rand() % 3 ];
}

void Cat::Sleep()
{
     cout << "Meowww Zzz (Cat Has Fallen Asleep)" << endl;
     cout << "Psshhh (The Cat Is Not Tired)" << endl;
     cout << "Zzz (The Cat Is Already Asleep)" << endl;
}

int main()
{
    Cat BlackMagic;
    
    BlackMagic.Feed();
    BlackMagic.Sleep();
    
    getch();
}

I didn't get the outcomes i wanted, it gave me some answers from Sleep and i put *Feed[] with the random ones?

This article has been dead for over six months. Start a new discussion instead.