Solved

How do publics and privates work in C++

Posted on 2004-09-18
4
231 Views
Last Modified: 2010-04-01
I am taking a class in C++, and we are required to write  a program that provides a temp conversion. I wrote the program based on previous knowledge of the languge and my previous coding histoy. The first version runs fine(I will copy it below so that you can see what I did). The instructer said "great but" ....-   that he want me to include publics and privates. I've tried to rewrite according to a sample example, but I am apparently missing the concept because it is not working. Could someone please look at the two codes and explain to me what I need to do to make this work. Thanks much....
*******************************************************************************************************************
//First Program
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

void CalcTemp(const float,const float,double,double&);

const float num1 =.5555555;
const float num2 =32;



int main()
{
      string firstName;
      string first;
      double numF;
      double numC;

      cout << endl<< endl<< "Please enter your name : " ;
      cin >> firstName;
      cout << endl;
      cout <<  "Hello " << firstName << ", I am a simple program designed by
C++
Student" ;
      cout << " Annie Young."<< endl;
      cout << "My purpose is to preform a simple calculation that will
convert"<<
endl;
      cout <<" F temp to C temp."<< endl<<endl;
      cout << "To Exit this program enter the number 9999."<< endl;

      do
            {
            cout << "Please enter a temp in F: ";
            cin >> numF;
            CalcTemp (num1,num2,numF, numC);
            cout << "The C temp is "<< numC ;
            cout << endl;
            }
      while
            (numF !=  9999  );

      return 0;
}
void CalcTemp(const float num1,
                    const float num2,
                    double numF,
                    double& numC)
{
      numC =(num1*(numF-num2));
}
***************************************************************************************************************

//2 program(doesnt work)
#include <iostream>

using namespace std;
class temp
{
private:

const float num1 =.5555555;
const float num2 =32;
double numF;
double numC;

public:

void inputTempF();
void calcTemp();
{
      num1 =.5555555;
      num2 =32;
}
void displayTempC();


};


void main()
{
temp theTemp;

theTemp.inputTempF();
theTemp.calcTemp();
theTemp.displayTempC();

}
void Temp::inputTempF()
{
    cout << "Please enter a temp in F: ";
    cin >> tempF;
    cout << endl;
}
void Temp::calcTemp()
{
            numC =(num1*(numF-num2));
}

void Temp::displayTempC()
{
cout << "The C temp is "<< numC ;
            cout << endl;
}
0
Comment
Question by:bananaamy
  • 3
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12092829
Hello,
Basically you have many typos and some C++ syntax errors.
Here is the correct code, I will comment later:
#include <iostream>

using namespace std;
class temp
{
private:

    static float num1;
    static float num2;
    double numF;
    double numC;

public:

    void inputTempF();
    void calcTemp();
    void displayTempC();
};

float temp::num1 = .555555;
float temp::num2 = 32;

main()
{
temp theTemp;

theTemp.inputTempF();
theTemp.calcTemp();
theTemp.displayTempC();
system("Pause");
}

void temp::inputTempF()
{
    cout << "Please enter a temp in F: ";
    cin >> numF;
    cout << endl;
}

void temp::calcTemp()
{
          numC =(num1*(numF-num2));
}

void temp::displayTempC()
{
cout << "The C temp is "<< numC ;
          cout << endl;
}
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12092860
Some comments:

Instead of const variables, you can use "static members". Those are initialized outside the class definition, as you can see.

Your calcTemp() function is implemented  both inside and outside your class definiton. I leaved the outside one.

You have to take care about upper/lowercase of class name. This is incorrect
void Temp::inputTempF()
Because correct classname is 'temp', all with lowercase.

There is an error in your inputTempF() function:
    cin >> tempF;
Correct variable is 'numF', not 'tempF'

It is always recommended to make some kind of 'pause' at the end of your program. Look at the end of main() function.

Good luck,
Jaime.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12092864
Oh, the most important. Public and private members are OK in your implementation.
0
 

Author Comment

by:bananaamy
ID: 12092883
Thank-you -I truly appreciate your comments.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question