Solved

How do publics and privates work in C++

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 106
Dialogbox API leak? 18 98
What is sub-make ? 2 71
max float value 3 51
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

861 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