Xaryu Xaryu - 1 month ago 12
C++ Question

Overloading the << operator and problems with add function

So I recently got back into programming, in prep for my last year of physics and i'm having some trouble with an example.

It says I need to add the specific Exhibit class shown in the code, and then implement the functions. Then it wants me to display this as the output;


Max says Eeeeep


Jack says Roar


Petals says Woof


{ Max Jack Batty Sheepy Hippopotamusesy Turkey }


{ Max Jack Batty Sheepy Hippopotamusesy Turkey }



By overloading the
<<
operator.
I've had a go, but im just stuck I believe i'm also doing the
add()
function incorrectly, so if someone could provide me with some insight it would be much appreciated.

Code:

#include <iostream>
#include <string>

using namespace std;
//------------------------------------------------------------
class Animal
{
private:
string name;
string sound;
public:
Animal(const string& n = "", const string& s = "");
~ Animal();
string getName() const {return name;}
string makeSound() const{return sound;}
};
Animal::Animal(const string&n, const string &s)
{
name=n;
sound=s;
}
Animal:: ~ Animal() //Destructor
{
}

ostream &operator <<(ostream& out, Animal & obj)
{
cout <<obj.getName()<<" says "<<obj.makeSound() ;
return out;
}
//------------------------------------------------------------
class Exhibit{
public:
Exhibit(int maxEnt = 10);
~Exhibit();
void add(const Animal& e);
Animal& operator [](size_t i){ return entries[i];};
friend ostream& operator <<(ostream& out, const Exhibit& obj);
private:
int MaxNumberOfAnimals;
int CurrentNumberOfAnimals;
Animal* entries;
};
Exhibit::Exhibit(int maxEnt)
{
MaxNumberOfAnimals=maxEnt;
}
Exhibit:: ~ Exhibit()
{

}

void add(const Animal& e)
{
int j=0;
entries[j]=e;
j++;
return;
}

ostream& operator <<(ostream& out, const Exhibit& obj)
{
cout <<"{ ";
for(int i=0;i++;i!=obj.MaxNumberOfAnimals)
{
cout << obj.entries[i] << ", ";
}
cout << "}"<<endl;
return out;
}
//------------------------------------------------------------
int main()
{
Animal* monkey = new Animal("Max", "Eeeeep");
Animal* tiger = new Animal("Jack", "Roar");
Animal* Dog = new Animal("Petals", "Woof");

cout << *monkey <<endl;
cout << *tiger <<endl;
cout << *Dog << endl;

Exhibit cage(10);
cage.add(*monkey);
cage.add(*tiger);
/*cage.add(Animal("Batty", "Screech"));
cage.add(Animal("Sheepy", "Bleat Bleat"));
cage.add(Animal("Hippopotamusesy", "growl"));
cage.add(Animal("Turkey","Gobble"));*/
cout << cage << endl;
cout << cage << endl;


delete monkey;
delete tiger;
delete Dog;
system("pause");
return 0;
}
//------------------------------------------------------------

Answer

One things that could be causing the problem with your << overloading is that you are sending everything to cout, rather than the out stream you pass in. A potential fix there could be changing "cout" to "out", as below:

ostream &operator <<(ostream& out, Animal & obj)
{
    out <<obj.getName()<<" says "<<obj.makeSound() ;
    return out;
}

As for your add function in your Exhibit class, it looks like you are initializing your counter 'j' each time you call the function, which means that it will always be 0. You can fix this by making your counter a private data member of the Exhibit class, or perhaps just by using your 'int CurrentNumberOfAnimals' to keep track, like below:

void add(const Animal& e)
{
    entries[CurrentNumberOfAnimals]=e;
    CurrentNumberOfAnimals++;
    return;
}

There may be other errors in there that I didn't notice, but there are some.

Edit: As posted in an above answer, entries is not initialized correctly. If you can, use a vector of Animals, like below:

#include <vector> //This should be in your Exhibit header file

class Exhibit{
  public:
    Exhibit(int maxEnt = 10);
    ~Exhibit();
    void add(const Animal& e);
    Animal& operator [](size_t i){ return entries[i];};
    friend ostream& operator <<(ostream& out, const Exhibit& obj);
  private:
    int MaxNumberOfAnimals;
    int CurrentNumberOfAnimals;
    vector<Animal> entries;
};

This also simplifies your add function, though, see below:

 void add(const Animal& e)
 {
    entries.push_back(e);
    return;
 }

2nd Edit: So far as I can tell, this is how to do it using arrays. This is assuming that you need at most 10 animals in your "entries" array, since you have "int maxent = 10" in your original Exhibit class.

Your Exhibit class declaration could look like this (we shouldn't need the "int maxent" in your constructor anymore unless you need it for other reasons):

class Exhibit{
  public:
    Exhibit();
    ~Exhibit();
    void add(const Animal& e);
    Animal& operator [](size_t i){ return entries[i];};
    friend ostream& operator <<(ostream& out, const Exhibit& obj);
  private:
    int MaxNumberOfAnimals;
    int CurrentNumberOfAnimals;
    Animal entries[10];
};

Your add function could then look like the first add function above, repasted here for convenience:

void add(const Animal& e)
{
    entries[CurrentNumberOfAnimals]=e;
    CurrentNumberOfAnimals++;
    return;
}
Comments