0

Hello forum. I cant figure out how to overload the '+' operator or maybe theres another error? Im posting from phone so excuse the slopyness. Thanks!

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

class Ferma
{
public:
    static int nr_rate;
    static int nr_gaini;
    static int nr_porci;
    static int nr_vaci;
    virtual void afisare()
    {
        cout<<"Ferma:\n";
    }
private:
    int id;
    string animal;
};

int Ferma::nr_rate=0;
int Ferma::nr_gaini=0;
int Ferma::nr_vaci=0;
int Ferma::nr_porci=0;
class Pasari:public Ferma
{
public:

    Pasari(string s):animal(s)
    {
        if(s=="rata")
        {
            ++nr_rate;
            id=nr_rate;
            cout<<"Tocmai am creeat rata"<<id<<"!"<<endl;
        }
        else if(s=="gaina")
        {
            ++nr_gaini;
            id=nr_gaini;
            cout<<"Tocmai am creeat gaina"<<id<<"!"<<endl;
        }
    }

    void afisare()
    {
        cout<<"animalul este "<<animal<<id<<endl;

    }
    Ferma

Edited by Narue: Fixed code tags

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by daianahoney
0

Where is the code for the overloaded operator? Please make some attempt at that code and post back.

0

So the problem is at the assignement in main(). Sorry for the previous post. I know the design is a mess so tips are welcomed.

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

class Ferma
{
public:
    static int nr_rate;
    static int nr_gaini;
    static int nr_porci;
    static int nr_vaci;
    virtual void afisare()
    {
        cout<<"Ferma:\n";
    }
private:
    int id;
    string animal;
};

int Ferma::nr_rate=0;
int Ferma::nr_gaini=0;
int Ferma::nr_vaci=0;
int Ferma::nr_porci=0;
class Pasari:public Ferma
{
public:

    Pasari(string s):animal(s)
    {
        if(s=="rata")
        {
            ++nr_rate;
            id=nr_rate;
            cout<<"Tocmai am creeat rata"<<id<<"!"<<endl;
        }
        else if(s=="gaina")
        {
            ++nr_gaini;
            id=nr_gaini;
            cout<<"Tocmai am creeat gaina"<<id<<"!"<<endl;
        }
    }

    void afisare()
    {
        cout<<"animalul este "<<animal<<id<<endl;

    }
    Ferma& operator=(Pasari& p)
    {
        if(this !=&p)
        {
            this->animal=p.animal;
            this->id=p.id;
        }
        return (*this);
    }
private:
    int id;
    string animal;

};
class Mamifere:public Ferma
{
public:
    Mamifere(string s):animal(s)
    {
        if(s=="porc")
        {
            ++nr_porci;
            id=nr_porci;
            cout<<"Tocmai am creeat  porc"<<id<<"!"<<endl;
        }
        else if(s=="vaca")
        {
            ++nr_vaci;
            id=nr_vaci;
            cout<<"Tocmai am creeat  vacuta"<<id<<"!"<<endl;
        }
    }
    void afisare()
    {
        cout<<animal<<id<<endl;

    }
    Ferma& operator=(Mamifere& p)
    {
        if(this !=&p)
        {
            this->animal=p.animal;
            this->id=p.id;
        }
        return (*this);
    }

private:
    int id;
    string animal;

};
int main()
{

    Ferma f[20];
    int i=0;

    int e=0;
    do
    {
        cout<<"Alegeti optiunea:\n";
        cout<<"1.rata\n"
            <<"2.gaina\n"
            <<"3.porc\n"
            <<"4.vaca\n"
            <<"0.iesire\n";
        cin>>e;
        switch(e)
        {
        case 1:
            f[i]=new Pasari("rata");
            i++;
            break;
        case 2:
            f[i]=new Pasari("gaina");
            i++;
            break;
        case 3:
            f[i]=new Mamifere("porc");
            i++;
            break;
        case 4:
            f[i]=new Mamifere("vaca");
            i++;
            break;
        case 0:
            break;
        }

    }
    while((!e) || (i<20));
    cout<<"Ferma contine "<<Ferma::nr_rate+Ferma::nr_gaini+Ferma::nr_porci+Ferma::nr_vaci<<":";
    for(int c=0; c<=i; c++)
        f[c].afisare();


    return 0;

}

Edited by daianahoney: n/a

0

Solved. Thanks, had to use a vector of pointers to base class so i could use polymorphism.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.