| | |
Help with Classes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2004
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: May 2004
Posts: 72
Reputation:
Solved Threads: 5
Well, here's an easy one--you've spelled a method two different ways (the first is correct):
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:
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
•
•
•
•
Originally Posted by biel_andrada
...
Temp Celsius( ); //needed to work on
...
<<theTemp.Celcius()
...
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:
•
•
•
•
Originally Posted by biel_andrada
...
cin>>theTemp; //input
...
That should get you started.
--sg
![]() |
Similar Threads
- need idea for project using classes and inheritance (C++)
- How do i do chat program using MFC (Microsoft Foundation Classes) and Visual Basic? (Visual Basic 4 / 5 / 6)
- Loading classes from a .jar file (Java)
- Understanding classes (C++)
- Working with objects of different Classes (Java)
- Classes (C++)
- Summer Classes (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: Constructor and Convertion operator are the same,how to avoid memory leak?
- Next Thread: project conversion
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





