Solved

How do publics and privates work in C++

Posted on 2004-09-18
4
223 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now