Solved

How do publics and privates work in C++

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

22 Experts available now in Live!

Get 1:1 Help Now