Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Doing something wrong

Reply
Thread Tools

Doing something wrong

 
 
Chiller
Guest
Posts: n/a
 
      04-12-2004
Ok, I've implemented a few changes to the code and the bool functions now
seem to be functioning correctly; however, I think I'm doing the convertions
incorrectly because the values printed out from my TEST_DISTANCE driver
haven't converted correctly. I'm possibly using enum incorrectly, so I'd
appreciate
some assistance with its use. Also, when outputing the Distance objects,
numbers are displayed but the value isn't displayed correctly. ie I'm
getting erroneous characters rather than a simple m etc.

Can I please ask someone to compile my code and have a look where I'm going
wrong. I've tried, but I've hit a brick wall so to say. I'm still very much
learning so can I please ask for simple examples and explanations. I'm not
too concerned with complicated refined code.

Thanks for any help.

..cpp file
**************************
#include "Distance.h"
#include <iostream>

using namespace std;

/*-------------------------------------------------------*\

| implementation of member functions |

\*-------------------------------------------------------*/

// constructors

Distance :: Distance (int n, char m) : nu(n), me(m) {}
Distance :: Distance (void) : nu(0), me('m') {}

enum Measure {mm=1, cm=10, m=1000, km=1000000};



// Convert mm value into metres
int Distance::conv()
{
return (nu = nu * 1000, me ='m');
}

// access functions
int Distance :: number (void)
{
return nu;
}

char Distance :: measure (void)
{
return me;
}

// Overload of the "=" operator
Distance & Distance:perator= (Distance const & right_operand)

{
nu = right_operand.nu;
me = right_operand.me;
return *this;
}

// Overload of the "==" operator
bool Distance:perator == ( Distance const & rhs )
{
conv();
return ( nu == rhs.nu);
}

//Overload of the != operator
bool Distance:perator != ( Distance const & rhs )
{
conv();
return ( nu != rhs.nu);
}

//Overload of the < operator
bool Distance:perator < (Distance const & rhs)
{
conv();
return ( nu < rhs.nu);
}

//Overload of the <= operator
bool Distance:perator <= (Distance const & rhs)
{
conv();
return (nu <= rhs.nu);
}

//Overload of the > operator
bool Distance:perator > (Distance const & rhs)
{
conv();
return (nu > rhs.nu);
}

//Overload of the >= operator
bool Distance:perator >= (Distance const & rhs)
{
conv();
return (nu >= rhs.nu);
}

//Overload of the + (addition) operator
Distance Distance:perator+ (Distance right_operand)
{
conv();
return Distance (nu + right_operand.nu, me);
}

//Overload of the - (subtraction) operator
Distance Distance:perator- (Distance right_operand)
{
conv();
return Distance (nu - right_operand.nu, me);
}

// provide an overload of "<<" for easy display
ostream& operator<< (ostream& out, Distance& d)
{
out << "(" << d.number() <<", " << d.measure() << ")";
return out;
}

/*-------------------------------------------------------*\

| test driver for the Distance class |

\*-------------------------------------------------------*/

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main (void)

{
// create test input

Distance a = Distance (6, cm);
Distance b (4, km);
Distance c (2, m);
Distance d;
Distance e (5, m);
Distance z1 = a = b;

cout << a << endl << b << endl << c << endl << d << endl << e << endl <<
endl;

cout << a <<endl << endl;
cout << "The results of comparing various Distance values :" << endl;
cout << "Distance a == Distance e : ";
cout << (a == e ? "true" : "false") << endl;
cout << "Distance a != Distance e : ";
cout << (a != e ? "true" : "false") << endl;
cout << "Distance a < Distance c : ";
cout << (a < c ? "true" : "false") << endl;
cout << "Distance a <= Distance c : ";
cout << (a <= c ? "true" : "false") << endl;
cout << "Distance a > Distance c : ";
cout << (a > c ? "true" : "false") << endl;
cout << "Distance a >= Distance b : ";
cout << (a >= c ? "true" : "false") << endl;
cout << "Distance b > Distance e : ";
cout << (b > e ? "true" : "false") << endl;

Distance z2 = a + b;
cout << "\nDistance a plus Distance b = " << z2 <<endl << endl;

Distance z3 = b - c;
cout << "\nDistance b minus Distance c = " << z3 <<endl << endl;

cin.ignore();

return 0; // normal termination

}

#endif



..h file
***************************

#ifndef DISTANCE_H
#define DISTANCE_H
#include <iostream>

using namespace std;

class Distance
{
public :
Distance (int, char) ; // constructor - takes int and char values
Distance (void) ; // default - zero

//access member functions
int number (void);
char measure (void);

//overloads
Distance & Distance:perator= (Distance const & right_operand);
bool operator == ( Distance const &rhs );
bool operator != ( Distance const &rhs );
bool operator < ( Distance const &rhs );
bool operator <= ( Distance const &rhs );
bool operator > ( Distance const &rhs );
bool operator >= ( Distance const &rhs );

Distance Distance:perator+ (Distance right_operand);
Distance Distance:perator- (Distance right_operand);

int Distance::conv();

private :
int nu ; // the value
char me ; // the unit of measure (m)

} ;

// provide an overload of "<<" for easy display
ostream& operator<< (ostream&, const Distance&);

#endif





 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      04-12-2004
In article <(E-Mail Removed) m>,
"Chiller" <...@...> wrote:

> Ok, I've implemented a few changes to the code and the bool functions now
> seem to be functioning correctly; however, I think I'm doing the convertions
> incorrectly because the values printed out from my TEST_DISTANCE driver
> haven't converted correctly. I'm possibly using enum incorrectly, so I'd
> appreciate
> some assistance with its use. Also, when outputing the Distance objects,
> numbers are displayed but the value isn't displayed correctly. ie I'm
> getting erroneous characters rather than a simple m etc.
>
> Can I please ask someone to compile my code and have a look where I'm going
> wrong. I've tried, but I've hit a brick wall so to say. I'm still very much
> learning so can I please ask for simple examples and explanations. I'm not
> too concerned with complicated refined code.
>
> Thanks for any help.


> /*-------------------------------------------------------*\
>
> | test driver for the Distance class |
>
> \*-------------------------------------------------------*/
>
> #ifdef TEST_DISTANCE // .... Distance class .... test driver
>
> int main (void)
>
> {
> // create test input
>
> Distance a = Distance (6, cm);
> Distance b (4, km);
> Distance c (2, m);
> Distance d;
> Distance e (5, m);


In the above code, your c_tor is being used incorrectly. The second
argument is supposed to be a char but you are sending it an enum type.
I'm suprised your compiler isn't at least warning you about 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
XSL: I'm doing something wrong, and I can't see it! Simon Brooke XML 5 02-27-2007 08:34 AM
Crypto.Cipher.ARC4, bust or me doing something wrong? Michael Sparks Python 6 09-21-2005 07:54 PM
Re: Crypto.Cipher.ARC4, bust or me doing something wrong? Jp Calderone Python 1 09-20-2005 05:20 PM
Either I'm doing something wrong or jaxb is crappola Mark XML 0 05-18-2004 11:54 PM
Re: Testing magnification. Am I doing something wrong? Mark M Digital Photography 14 08-20-2003 12:43 PM



Advertisments