Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do publics and privates work in C++

Posted on 2004-09-18
4
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

636 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