A few points on the code. That I have not commented on any particular
issue does not mean that the code is good at that point. It simply
means I did not comment on it.
Kush wrote:
>>
> ----------------------------------------------------------------------------------------------------------------------------------------
> #include <iostream.h>
> #include <math.h>
> #include <fstream.h>
Wrong headers. <iostream> <cmath> and <fstream> are the correct
headers. You are a new C++ programmer or one from 10 years ago? Why are
*new* C++ programmers being taught to include old headers?
> //************************************************** ******Class
> definitions
> class coordinate
> {
> private:
> int x, y;
> public:
void initialize(int a, int b);
Bad as already pointed out. Do not have generally have public
initialize() methods. You might have a private one which is called by a
constructor.
> int provide_x_coordinate(void);
> int provide_y_coordinate(void);
Bad style. Do not use (void) for functions with no parameters but
instead use ()
> class property
> {
> private:
> side side1, side2, side3, side4;
Almost certainly not good. Better to use a fixed length array. That
also might not be the best design but it's pretty much likely to be
better than four distinct members such that you cannot iterate through
them.
> double perimeter(void);
> public:
> void initialize(void);
> void write_percentage_per_side_to_file(ofstream &os);
Bad for three reasons. Firstly this method is not going to modify the
class so it should be a const method. Secondly, it is bad to take
ofstream & instead of the generic ostream & unless it is going to use
features specific to ofstream. (And you'll need std:: to qualify
ostream). The third bad thing is that the function calculates something
and outputs it to a stream. Why can't you calculate something and
return it?
> };
>
> void property::write_percentage_per_side_to_file(ofstre am &os)
> {
> os << "The percentage length of side1 relative to the perimeter is "
> << (side1.length() / perimeter() ) * 100 << endl
> << "The percentage length of side2 relative to the perimeter is " <<
> (side2.length() / perimeter() ) * 100 << endl
> << "The percentage length of side3 relative to the perimeter is " <<
> (side3.length() / perimeter() ) * 100 << endl
> << "The percentage length of side4 relative to the perimeter is " <<
> (side4.length() / perimeter() ) * 100 << endl;
> os.close();
> }
Bad to close the stream at the end. If you had used an array you could
do something like:
for ( int i=0; i<numSides; ++i )
{
os << "The percentage length of side" << i << " relative to the
perimeter is "
<< side[i].length() / perimeter() ) * 100 << '\n';
}
os.flush(); // if you really want the flush
The "function that calculates something" that I mentioned earlier would
give you that percentage. Instead of putting that calculation in your
loop here, you could call it each time. It could also be a public
method so it could be called externally if someone using the class
needs this percentage.
> void main(void)
main returns int. And don't use (void) of course as parameter list.
> {
> property lot;
> lot.initialize();
> ofstream os = ("C:\\output.txt", ios:
ut);
Remove the = sign.
If you used int main( int argc, char * argv[] ) then you could check if
there is a runtime argument and use it as the output file. Much better
than hard-coding it.