Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help with simple file I/O :)

Reply
Thread Tools

Help with simple file I/O :)

 
 
=?ISO-8859-15?Q?Bo_M=F8ller?=
Guest
Posts: n/a
 
      11-29-2006
James Willmott wrote:
> Kai-Uwe Bux wrote:
>>>Salt_Peter wrote:
>>>
>>>
>>>coordinate::coordinate(int a, int b) : x(a), y(b)

>> {
>> }

>
> What's the benefit of doing that versus...
>
> coordinate::coordinate(int a, int b){
> x=a;
> y=b;
> }
>
> They seem to have the same end result at first glance?
>

No.
You can always initialize a member, but if the member is const, then
you can not assign a value to it once it has been initialized!

try this code:
#include <ostream>

class Problem
{
public:
Problem(int n);
private:
const int m_number;
};

Problem:roblem(int n)
{
m_number = n; // Illegal because m_number is const!
}

int main()
{
Problem p1(1);
}

The point is the contructor will always initialize the members before
entering the body of the constructor. By giving an initializer list
you specify what values will be used to initialize the member. If no
initialiser list is given default-initialisation occurs.

Bo Møller
Hobbyist

--
Bo Møller

Hobby-Programmer


 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      11-29-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple VB.Net / webservice requirement (but not simple for me....) Dave E ASP .Net 7 01-11-2006 02:07 PM
Simple simple program error...please help tasheeta@gmail.com C++ 14 11-02-2005 12:52 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57