![]() |
|
|
| Subject:
C++ code question
Category: Computers > Programming Asked by: purplepit-ga List Price: $25.00 |
Posted:
01 Jun 2003 09:28 PDT
Expires: 01 Jul 2003 09:28 PDT Question ID: 211548 |
I need to develop a class to represent the data concept of sterling
currency(ie UK Pounds)
The class will use a data member 'amount' as a single floating point
number, Thus an amount of value 1.5
would represent the sterling value of £1.50p.
The class should have operations to initialise the data, to add more
money,to subtract money, and to return the whole pounds and pence,
a short program to illustrate is below.....
void main()
{
Stering s1:
s1.initialise(3, 75); // £3.75p
s1.add (1,50); // £5.25p
s1.subtract (0,26); // £4.99p
cout << "Current total is £" << s1.pounds()
<< '-' <<s1.pence() << 'p';
}
would produce output of £4.99p
1)
I need to write a class specification in a good style, to include
the 'data member'
and the prototypes of member functions.
2)
I also need c++ code to implement each of the following;
* member function initialise()
* member function pounds()
* member function pence()
* member function add()
* member function subtract()
with comments to explain if possible.
Thank you.... |
|
| Subject:
Re: C++ code question
Answered By: answerguru-ga on 01 Jun 2003 12:31 PDT Rated: ![]() |
Hi again purplepit,
Nice to see you here again....below is the source code that conforms
to the specifications you've provided. Note that the the class
specification is within the "class Sterling{...}" clause in the code,
followed by the implementation of all member functions and then the
main method.
// BEGIN SOURCE CODE
//importing required libraries
#include <stdio.h>
#include <iostream>
// class definition
class Sterling
{
float amount; // representation of sterling value
public:
Sterling(); // constructor
void initialize(int pounds, int pence); // initialize amount
void add(int pounds, int pence); // add to amount
void subtract(int pounds, int pence); // subtract from amount
int pounds(); // return pound value
int pence(); // return pence value
};
Sterling::Sterling()
{
// set amount to zero if not initialized
amount = 0;
}
void Sterling::initialize(int pounds, int pence)
{
// type conversions and calculations for initialization
amount = (float)pounds + ((float)(pence%100)/100);
}
void Sterling::add(int pounds, int pence)
{
// type conversions and calculations for adding to amount
amount = amount + (float)pounds + ((float)(pence%100)/100);
}
void Sterling::subtract(int pounds, int pence)
{
// type conversions and calculations for subtraction
amount = amount - pounds - ((float)(pence%100)/100);
}
int Sterling::pounds()
{
// casts amount to int and returns
return (int)amount;
}
int Sterling::pence()
{
// calculates pence as follows:
// subtract pounds from amount
// multiply by 100 to obtain pence int values
// add one to account for roundoff error
int pence = (int)((amount - pounds()) * 100) + 1;
return pence;
}
int main()
{
Sterling s1;
s1.initialize(3, 75); // £3.75p
s1.add (1,50); // £5.25p
s1.subtract (0,26); // £4.99p
printf("Current total is £ %i - %i p", s1.pounds(), s1.pence());
}
// END SOURCE CODE
Please let me know if you have any problems understanding the
information above and I will respond promptly :)
Cheers!
answerguru-ga |
purplepit-ga
rated this answer:
and gave an additional tip of:
$10.00
As Brilliant and as helpful as ever!!!!! |
|
| Subject:
Re: C++ code question
From: carnegie-ga on 02 Jun 2003 02:24 PDT |
Dear Purplepit, You appear to be under the impression that the symbols "£" and "p" are both used in specifying an amount in UK currency. This is not so: as with most other currencies, you use one or the other. Amounts under a pound can be specified just as pence, e.g. 37p. Any amount can be specified in pounds, but in this case "p" is not used, e.g. £0.37, £5.99, £5.00. I trust this helps. Carnegie |
| Subject:
Re: C++ code question
From: purplepit-ga on 02 Jun 2003 03:29 PDT |
Thank you very much..It does help.. Purplepit-ga |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |