943,645 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2611
  • C++ RSS
Aug 20th, 2004
0

Help with Classes

Expand Post »
Hi!
Thanks for the help regarding vectors It really helped me.

Now, I have a problem regarding classes. I do not know the problem in this program

#include <iostream.h>
#include <ctype.h> //islower(), toupper()
#include <stdlib.h> //exit( )
class Temp
{
public:
Temp( );
Temp(double initDeg, char initScale);
double Degrees();
char Scale();
Temp Fahrenheit( );
Temp Celsius( ); //needed to work on
Temp Kelvin( ); //needed to work on
void Read( );
void Print( );
private:
double myDegree;
char myScale;
};


Temp Temp :: Fahrenheit( )
{//converting to Fahrenheit
switch(myScale)
{
case ‘F’: return Temp(myDegrees, ‘F’);
case ‘C’: return Temp(myDegrees*1.8+32.0, ‘F’);
case ‘K’: return Temp(myDegrees-273.15, ‘F’);
}
}

void main()
{
cout<<"This program shows the Fahrenheit, Celcius,"
<<" and Kelvin equivalents of a temperature.\n\n";

char response;
Temp theTemp; //construction

do
{ cout<<"Enter a temperature(e.g., 98.6 F): ";
cin>>theTemp; //input
cout<<"-->"
<<theTemp.Fahrenheit()
<<" = "
<<theTemp.Celcius()
<<" = "
<<theTemp.Kelvin()
<<endl;
cout<<"\nDo you want to have more temperature conversion?";
cin>>response;
}while(response=='Y' || response=='y');

}

Please help me! Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
biel_andrada is offline Offline
3 posts
since Jul 2004
Aug 20th, 2004
0

Re: Help with Classes

Can you give us a hint as to where to look, what you are seeing, how you know it doesn't work? Does it compile? Run? Give unexpected results?

We might be able to help if we know what we're looking for.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 20th, 2004
0

Re: Help with Classes

Well, here's an easy one--you've spelled a method two different ways (the first is correct):

Quote originally posted by biel_andrada ...
...
Temp Celsius( ); //needed to work on
...
<<theTemp.Celcius()
...
Also, it won't compile until you actually define a body for that method like you did for Temp::Fahrenheit.

Hm, also noticed you have both myDegree and myDegrees in your code; I bet you don't want that either.

Unless you've snipped out lots of code, it looks like you haven't actually implemented half of the class members... including the constructor that actually accepts an argument and (one would imagine) initializes myDegree. Without that, it'll always be zero.

And... this looks suspicious:

Quote originally posted by biel_andrada ...
...
cin>>theTemp; //input
...
There's no way for the compiler to know that you want a double-precision value to be read... you need a double somewhere to hold the value.

That should get you started.
--sg
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Constructor and Convertion operator are the same,how to avoid memory leak?
Next Thread in C++ Forum Timeline: project conversion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC