# concept of encapsulation

### Question

•  give up with this one, I guess I don’t understand this concept of encapsulation, well I do but I don’t know how to make this program work

I declare in the fraction_class the following: as a PRIVATE

private:

int pnum,pdem;         //private numerator and private denominator use for asignment#5

int pnumb1;

double presult;           //double to hold the fraction decimal value

static char const slash = '/';     //static character field that hold the slash

//Private function to calculate the decimal equivalent of a fraction

//fraction Assig#5

void calculateDecimalValue()

{presult = ((double) num1 / (double)dem1);}

I understand that I need to declare the functions as a public in order to be able to access the private members of the class. I declare the functions in the class:

//function for to allow the user values for numerator and denominator Assig #5

void EnterFractionValue(int pnum,int pdem);

//Function that display the fraction Assig#5

void displayFraction(int pnum, int pdem, double presult);

All the examples I see in books they hard code the variables in the main function, I know my problem is in the main :

void main(void)

{

fraction_class gprivate;

gprivate.EnterFractionValue(int pnum,int pdem);

gprivate.displayFraction(int pnum, int pdem, double presult,int pnumb1);

} // end main

I guess I don’t know how to pass the variables from the functions to main…

I will appreciate any help!!

Here is the all code

------------------------------------------------------------------------------

// EnDisEncapP5.cpp : main project file.

/******************************************************************************

This main function contains three functions that are declare

in the fraction_class:

EnterFractionValue()

CalculateDecimalValue()

displayFractio()

EnterFractionValue: Prompt the user to enter the value of the numerator

and denominator

CalculateDecimalValue:Function that computes the floating point decimal equivalent

of the fraction, This fuction is called within the Enter Fraction

Value function, it defined private in the fraction_class

DisplayFraction: If the User enter 1: display the fraction as 1/4

2 : display the fraction as a decimal

0.25

Assignement # 5:

Students:

*********************************************************************************/

#include "stdafx.h"

#include <iostream>

#include "fraction_class.h"

#include <iomanip>

using namespace std;

/*--------------------------------------------------------------------------------------

Name: main

Purpose: In keeping with the object oriented concept of encapsulation, the fraction

data are private. Clients programs access the Fraction's data through public functions

---------------------------------------------------------------------------------------*/

void fraction_class::EnterFractionValue(int pnum, int pdem)

{

cout << " *****************************************************************************"  <<endl;

cout << " * Please Enter the numerator and the denominator for the fraction,           *" <<endl;

cout << " * If you entered 0 as denominator the program will display                   *" <<endl;

cout << " * and error and will allow you to continue entering numbers until you enter  *" <<endl;

cout << " * a number that is greater than 0 **ENJOY**                                  *" <<endl;

cout << " ****************************************************************************" <<endl;

cout << " " <<endl;

cout << "Enter Numerator : ";

cin >> pnum,

cout <<  "Enter denominator : ";

cin >> pdem; cout << '\n';

if (pnum == 0 ||pdem == 0)

{

cout << "I can not divide by 0. Please enter a number > 0: " <<endl;

cin>> pdem;

}

fraction_class::calculateDecimalValue(); ??? call the private class within EnterFractionValue()

// end of fnEnterFractionValue()

void fraction_class::displayFraction(int pnum, int pdem, int pnumb1, double presult)

{

cout << " *****************************************************************************"<<endl;

cout << " * Please Enter number 1 if you want to see the fraction in a fraction format " <<endl;

cout << " * Enter number 2 if you want to see the fraction as a decimal                " <<endl;

cout << " *****************************************************************************" <<endl;

cout << " " <<endl;

cout << "Enter Number, Remember 1 OR 2: ";

cin >> pnumb1; cout << '\n';

switch(pnumb1)

{

case 1:

cout <<  "You Just Entered: " ; cout <<  pnum,  cout << " / " , cout << pdem; cout << '\n';

break;

case  2:

presult = ((double) pnum / (double)pdem);

cout <<  "You Just Entered: ";  cout <<  presult; cout << '\n';

break;

}

}

void main(void)

{

fraction_class gprivate;

gprivate.EnterFractionValue(int pnum,int pdem);

gprivate.displayFraction(int pnum, int pdem, double presult,int pnumb1);

} // end main

I am getting those errors

----- Build started: Project: EnDisEncapP5, Configuration: Debug Win32 ------

Compiling...

EnDisEncapP5.cpp

C:\Documents and Settings\rechever\My Documents\Visual Studio 2005\Projects\Fraction_Class\Fraction_Class\Fraction_Class\fraction_class.h(13) : see declaration of 'fraction_class'

.\EnDisEncapP5.cpp(74) : error C2144: syntax error : 'int' should be preceded by ')'

.\EnDisEncapP5.cpp(74) : error C2660: 'fraction_class::EnterFractionValue' : function does not take 0 arguments

.\EnDisEncapP5.cpp(74) : error C2059: syntax error : ')'

.\EnDisEncapP5.cpp(75) : error C2144: syntax error : 'int' should be preceded by ')'

.\EnDisEncapP5.cpp(75) : error C2660: 'fraction_class::displayFraction' : function does not take 0 arguments

.\EnDisEncapP5.cpp(75) : error C2059: syntax error : ')'

Build log was saved at "file://c:\Documents and Settings\rechever\My Documents\Visual Studio 2005\Projects\EnDisEncapP5\EnDisEncapP5\Debug\BuildLog.htm"

EnDisEncapP5 - 7 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =

Monday, November 13, 2006 4:53 AM