View Single Post
  #7 (permalink)  
Old January 2nd, 2006, 05:07 AM
Jonax Jonax is offline
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

Thanks Vector - the following seems to work:
Code:
/* Overloading and inheritance
** tested using overloads of unary operators
** 051231 Jonax
*/

#include <iostream.h>    // cout,cin,endl
#include <conio.h>        // getche()
#include <iomanip.h>    // setw()
#include <string.h>        // strlen()

class cApple
{
protected:
    int iWormCount;
    float fTotalLengthOfWorms;
public:
    cApple()
    {
        iWormCount = 0;
        fTotalLengthOfWorms = 0;
    }

    cApple(int iWC, float fTLoW)
    {
        iWormCount = iWC;
        fTotalLengthOfWorms = fTLoW;
    }

    cApple operator ++ () 
    {
        return cApple(++iWormCount, ++fTotalLengthOfWorms);
    }

    void put()
    {
        cout << "Antal orme: " << iWormCount << endl;
        cout << "Længde: " << fTotalLengthOfWorms << endl;
    }
};

class cBigApple:public cApple
{
private:
    float fWeightInKilos;
public:
    cBigApple() : cApple()
    {
        fWeightInKilos = 0;
    }

    cBigApple(int iWC, float fTLoW, float fWiK) : cApple(iWC, fTLoW)
    {
        fWeightInKilos = fWiK;
    }

    friend cBigApple operator -- (cBigApple tmp) 
    {
        return cBigApple(--tmp.iWormCount, --tmp.fTotalLengthOfWorms, 0.0);
    }
};

class cBigRedApple:public cBigApple
{
private:
public:
    cBigRedApple() : cBigApple()
    {
    }

    cBigRedApple(int iWC, float fTLoW, float fWiK) : cBigApple(iWC, fTLoW, fWiK)
    {
    }

    cBigRedApple operator -- (int)
    {
        return cBigRedApple(iWormCount--, fTotalLengthOfWorms--, 0.0);
    }
};

int main(){
    cApple oMyApple;
    ++oMyApple;
    cout << "oMyApple:" << endl;
    oMyApple.put();

    cBigApple oMyBigApple;
    ++oMyBigApple;
    ++oMyBigApple;
    ++oMyBigApple;
    cout << "\noMyBigApple:" << endl;
    oMyBigApple.put();

    cBigRedApple oMyBigRedApple;
    ++oMyBigRedApple;
    ++oMyBigRedApple;
    ++oMyBigRedApple;
    ++oMyBigRedApple;
    --oMyBigRedApple;
    oMyBigRedApple--;
    cout << "\noMyBigRedApple:" << endl;
    oMyBigRedApple.put();

    getch();
    return 0;
}
Reply With Quote