6 Replies - 1209 Views - Last Post: 20 May 2011 - 04:33 PM Rate Topic: -----

#1 daianahoney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 11

class inheritance, operator= overloading error

Posted 14 May 2011 - 09:40 AM

Hello, i wanted to implement a farm [the base class] and 2 derived classes like Mamals and Birds (the language in code is Romanian). Everything is fine until in main i used an array of Farm objects to hold the Birds and Mamals. The compiler error ask for a proper operator= for this task and here i`m stuck. I have an =operator but its no good. Help greatly apreciated. Thanks in advance!
#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) //the operator
    {
        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"); //error at this kind of assignement
            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;

}



Is This A Good Question/Topic? 0
  • +

Replies To: class inheritance, operator= overloading error

#2 vividexstance  Icon User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,873
  • Joined: 31-December 10

Re: class inheritance, operator= overloading error

Posted 14 May 2011 - 09:45 AM

use a vector of Ferma*(pointers to a Ferma), that way you can use polymorphism.
Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1745
  • View blog
  • Posts: 5,896
  • Joined: 03-August 09

Re: class inheritance, operator= overloading error

Posted 14 May 2011 - 09:45 AM

Ferma f[20];

so f[i] would have type Ferma
but then you do this...
f[i]=new Pasari("rata");

new Pasari("rata"); has type Pasari*
a Pasari* can be implcitly cast to Ferma* but not to a Ferma
Was This Post Helpful? 1
  • +
  • -

#4 daianahoney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 11

Re: class inheritance, operator= overloading error

Posted 14 May 2011 - 09:53 AM

Thanks a lot, works like a charm. And I properly understand whats going on. Great help from you guys.
Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,873
  • Joined: 31-December 10

Re: class inheritance, operator= overloading error

Posted 14 May 2011 - 01:58 PM

If you're using a somewhat modern compiler then you should start using newer headers for the C++ Standard library(you did use the right one for "iostream":
#include <string>
#include <cctype>


Here is a good C++ reference: cplusplus.com/reference/. Note: the standard C headers are available without a '.h' at the end, and they have a 'c' appended to the beginning of the header name, like "cctype" above.
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: class inheritance, operator= overloading error

Posted 15 May 2011 - 12:09 AM

View Postdaianahoney, on 14 May 2011 - 05:40 PM, said:

Hello, i wanted to implement a farm [the base class] and 2 derived classes like Mamals and Birds
This doesn't sound quite right to me. Inheritance usually represents an "is a" relationship, in other words, what you seem to be saying is
  • A Bird "is a" Farm
  • A Mammal "is a" Farm


I would typically expect farm/animals to have a "has a" relationship instead of inheritance. This relationship is usually represented by 'aggregation' or 'composition'; If you're trying to build a class to represent your farm (as a collection of farm animals) then its worth reading up on these terms and what they might mean for you and the problem you're trying to solve.

This post has been edited by Bench: 15 May 2011 - 12:13 AM

Was This Post Helpful? 2
  • +
  • -

#7 daianahoney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 11

Re: class inheritance, operator= overloading error

Posted 20 May 2011 - 04:33 PM

View PostBench, on 15 May 2011 - 12:09 AM, said:

View Postdaianahoney, on 14 May 2011 - 05:40 PM, said:

Hello, i wanted to implement a farm [the base class] and 2 derived classes like Mamals and Birds
This doesn't sound quite right to me. Inheritance usually represents an "is a" relationship, in other words, what you seem to be saying is
  • A Bird "is a" Farm
  • A Mammal "is a" Farm


I would typically expect farm/animals to have a "has a" relationship instead of inheritance. This relationship is usually represented by 'aggregation' or 'composition'; If you're trying to build a class to represent your farm (as a collection of farm animals) then its worth reading up on these terms and what they might mean for you and the problem you're trying to solve.

Got your point there, I`ll clear up this concepts and differences in implementation asap. Right now I`m enjoying "Standard Template Library (STL), 1 of n Stephan T. Lavavej". Very good videos, the guy is amazing.
Thanks for the tips everyone!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1