Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help (http://www.go4expert.com/forums/help-t1970/)

SparkyCH 23Nov2006 11:15

Help
 
What i have here is a currency class which is a sub class. I have hit a wall and do not know how to do the following empty functions and constructors. Please help. thanks

Code:

Currency::Currency():_dollars(0),_cents(0){}

Currency::Currency(const Currency& curr)
{
       
        _dollars = curr._dollars;
        _cents = curr._cents;

}

Currency::Currency(int dollars, int cents):_dollars(dollars),_cents(cents){}

Currency::Currency(float curr)
{
       
       
       
}

Currency::Currency(const string& curr)
{
       
       
       
}

int Currency::getDollars() const
{
       
        return _dollars;       
       
}

int Currency::getCents() const
{
       
        return _cents;       
       
}

void Currency::setDollars(int dollars)
{
       
        this->_dollars = dollars;       
       
}

void Currency::setCents(int cents)
{
       
        this->_cents = cents;       
       
}
// This function converts the internal currency representation to a floating point number
float Currency::toFloat() const
{
       
       
       
        return 0;
}

string Currency::toString() const
{
       
        Currency curr;
        string returning_string;
        string assignStringDollars, assignStringCents ;
        ostringstream dollarStream, centStream;

        dollarStream << curr.getDollars();
        centStream << curr.getCents();

        assignStringDollars = dollarStream.str();
        assignStringCents = centStream.str();

        returning_string = assignStringDollars + "." + assignStringCents;

        return returning_string;       
       

}
//This function converts a floating point number to the internal representation of the currency class
void Currency::fromFloat(float curr)
{
       
       
       
}

int Currency::fromString(const string& curr)
{
        Tokeniser tok(curr, ".");
        char *garbage;

        _dollars = strtol(tok.nextToken().c_str(), &garbage, 10);
        if (garbage != NULL && strlen(garbage) > 0)
        {
                return -1;
        }

        _cents = strtol(tok.nextToken().c_str(), &garbage, 10);
        if (garbage != NULL && strlen(garbage) > 0)
        {
                return -1;
        }

        return 0;
}

Currency& Currency::operator =(const Currency& obj)
{
        if (*this != obj)
        {
                _dollars = obj.getDollars();
                _cents = obj.getCents();
        }
       
        return *this;
}

bool Currency::operator <(const Currency& obj) const
{
        if (_dollars < obj.getDollars())
        {
                return true;
        }
        if (_dollars > obj.getDollars())
        {
                return false;
        }
        if (_cents < obj.getCents())
        {
                return true;
        }

    return false;
}

bool Currency::operator ==(const Currency& obj) const
{
        if (_dollars == obj.getDollars() && _cents == obj.getCents())
        {
                return true;
        }

        return false;
}

bool Currency::operator !=(const Currency& obj) const
{
        return !(*this == obj);
}

ostream& operator <<(ostream& out, const Currency& obj)
{
        out << obj.getDollars() << "." << obj.getCents();
        return out;       
       
}


SparkyCH 24Nov2006 17:39

Re: Help
 
I got the first two but no clue about the toflaot and fromfloat please help
Code:

Currency::Currency(float curr)
{
       
        _dollars = (int)curr; 
        _cents = (int)((curr - _dollars) * 100);       
       
}

Currency::Currency(const string& curr)
{
       
        double f = atof(curr.c_str());
        _dollars = (int)f;
        _cents = (int)((f - _dollars) * 100);
}



All times are GMT +5.5. The time now is 18:36.