problems with .h

this is a school assignment, I can get my program to run ok but we are suppose to place the (parent) class into a .h file when I do this it say the variable are in accessible here is a piece of the code, the r,temp is what come up as inaccessible, any suggestion thanks in advance for you thoughts

istream& operator >>(istream& s, Rational& r)
{


cout <<setw(20)<<" "<<"Please enter a fraction in this form N/D: ";
s >> r.temp;
string temp = r.temp;
int a = temp.find("/");
for (int i = 0 ; i < temp.size();i++)
{
Where is the class ?

Take the whole class declaration into (cut & paste) to a .h file and move class's member functions' definitions into .cpp files witch have the same name as .h.

in the .h write those line in the first place

1
2
3
4
5
6
#ifndef SOMECLASS_H
#define SOMECLASS_H

//Class declarations

#endif 


In the .cpp or main.cpp include those lines :

 
#include "SomeClass.h" 
here is the class contained in the .h file
and so i would take the int variables (num and denom ) out of the class private data and create a cpp file called MY Rational or do i just put them back in the cpp file that holds all of the functions?? I am lost I am probably over thinking this be working on it for awhile. here is the .h file and my cpp file.

(this is the .h file )

#ifndef MY Rational_H
#define MY Rational_H
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <cctype>
#include <sstream>
#include <conio.h>
#include <fstream>
class Rational//declaring class
{
private:
int num,denom;//setting varaibles
char temp[80];
void simplify();//creating a member function (to aquire the gcd)
void normalize();// creating member to aquire correct negative placement

public:
Rational(int a=1, int b=1); //building a constructor
//void singlefrac();//gets the users input
//void displayfrac(); // display the fraction as a numerator/denomanator
void displayFloatFrac(); // display floating point fraction
Rational operator + (const Rational & frac);
Rational operator - (const Rational & frac);
Rational operator * (const Rational & frac);
Rational operator / (const Rational & frac);
friend istream& operator >> (istream& s, Rational& r);
friend ostream& operator << (ostream& s, Rational& r);
bool operator < (Rational & frac);
bool operator > (Rational & frac);
bool operator == (Rational & frac);

class ZeroDen
{
};
class Alpha
{
};


};///// end class
#endif


**************** here is the cpp file well most of it had to cut some out so it would fit *************

//complier derictves

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <cctype>
#include <sstream>
#include <conio.h>
#include <fstream>
#include "MY Rational.h"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////
istream& operator >>(istream& s, Rational& r)
{


cout <<setw(20)<<" "<<"Please enter a fraction in this form N/D: ";
s >> r.temp;
string temp = r.temp;
int a = temp.find("/");
for (int i = 0 ; i < temp.size();i++)
{
if (isdigit(temp.at(i)))
{
if(a==0||a==-1||temp.size()==a+1)
{
throw Rational::ZeroDen();
}
if (i<a)
{
istringstream strStream(temp.substr(i));
strStream>>r.num;
strStream.clear();
}
else
{
istringstream strStream(temp.substr(i));
strStream>>r.denom;
strStream.clear();
if (r.denom==0)
{
throw Rational::ZeroDen();
}
}
}
else if (i!=a)
{
throw Rational::Alpha();
}

}
return s;
}
///////////////////////////////////////////////////////////////////////////////////
ostream& operator <<(ostream& s, Rational& r)
{
s << r.num <<"/"<<r.denom <<" ";
return s;

}

///////////////////////////////////////////////////////////////////////////////////
Rational::Rational(int a,int b) //assign varible value 1
{

}
///////////////////////////////////////////////////////////////////////////////////
bool Rational ::operator<(Rational & frac)
{


if (static_cast<float>(num)/denom < static_cast<float>(frac.num)/frac.denom) return true;
else return false;


}
////////////////////////////////////////////////////////////////////////////////
bool Rational ::operator>(Rational & frac)
{

if (static_cast<float>(num)/denom > static_cast<float>(frac.num)/frac.denom) return true;
else return false;


}
////////////////////////////////////////////////////////////////////////////////
bool Rational ::operator==(Rational & frac)
{

if (static_cast<float>(num)/denom == static_cast<float>(frac.num)/frac.denom) return true;
else return false;


}
////////////////////////////////////////////////////////////////////////////////
void Rational :: simplify()
{
Rational temp;
temp.num = num;//setting values to num
temp.denom = denom;//setting values to denom
int gd;// temp variable greatest comon denominator
if(denom!=0)// making sure we have a viable # to reduce
{
num = abs(num);//changing value to postive if they were neg
denom = abs(denom);// by taking absolute value.
int temp = num % denom;//getting remainder
while (temp > 0)//chaging valve to divide again if remainder is not 0
{
num = denom;//set up numbers
denom = temp;//to get remainder
temp = num % denom;//getting remainder
}
gd = denom;//setting the gcd
}
temp.num /= gd;//divide numerator by the greatest comon denominator
temp.denom /= gd;//divide denominator by the greatest comon denominator

num = temp.num;//Set num to Rational object temp.num
denom = temp.denom;//Set den to Rational object temp.denom
}

////////////////////////////////////////////////////////////////////////////////////////////
void Rational::normalize()//switching neg sign to num only

{
if (denom<0)// I could not get negate to function so n=-n
{
num = -(num);
denom = -(denom);
}
}
////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////
Rational Rational::operator +(const Rational & frac)//adding fraction
{
Rational Temp;
Temp.num=((num*frac.denom)+(denom*frac.num));
Temp.denom=(denom*frac.denom);
Temp.simplify();//calling simplify function for gcd
Temp.normalize();
return Temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
Rational Rational::operator - (const Rational & frac)//subtracting function
{
Rational Temp;
Temp.num=(num*frac.denom-denom*frac.num);
Temp.denom=(denom*frac.denom);
Temp.simplify();//calling simplify function for gcd
Temp.normalize();//cout <<setw(20)<<" ";//alligment
return Temp;
}


////////////////////////////////////////////////////////////////////////////////////////////
Rational Rational ::operator * (const Rational & frac)//mulitply function
{
Rational Temp;
Temp.num=(num*frac.num);
Temp.denom=(denom*frac.denom);
Temp.simplify();//calling simplify function for gcd
Temp.normalize();//cout<<setw(20)<<" ";//alligment
return Temp;
}

////////////////////////////////////////////////////////////////////////////////////////////
Rational Rational::operator/(const Rational & frac)//Dividing function
{
Rational Temp;
Temp.num=(num*frac.denom);
Temp.denom=(denom*frac.num);
Temp.simplify();//calling simplify function for gcd
Temp.normalize();//cout<<setw(20)<<" ";//alligment
return Temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
// void Rational:: displayfrac()//display out
// {
// cout<<num<<"/"<<denom<<" ";

// }
////////////////////////////////////////////////////////////////////////////////////////////
void Rational::displayFloatFrac()//display float value
{
//ofstream outfile("MY Rational.h");
cout<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint);
cout <<setprecision(3)<<(static_cast<double>(num)/denom)<<endl;



}
////////////////////////////////////////////////////////////////////////////////////////////
int main (int argc, char*argv[])//set up command line input arg
{
if(argc>1)//checking for number of arg passed in we need more than 1
{
cout <<setw(35)<<"Hello "<<argv[1]<<endl;//gretting user with there name

}
else//if user did not enter in command line arg cout error
{
Use code tag next time...

What's the problem ?
New to this I don't know what code tag is?
I was trying to get my private varaible to be accessable in the main cpp program and was unclear on how to do this once my .h file was created you had said to create a cpp file with the same name as the .h I have never done this like I said I am new to this and feel a little overwhelmed with all of the syntax. so same problem as before I have not created a cpp file with the same name as my .h I will try that.
http://cplusplus.com/ico/text_code.png
That's the code tag.Wrap it around the code (select the code) click the button...see the right.

You don't know what is about public and private I think :

When you put something in private , you can't access using the function outside the class...but MUST USE member function of the class.

After checking your code,I see you didn't write accessor and mutator functions.

Accessor is a function that gives you access to member variable that is private :

1
2
3
4
int getNum()
{
       return num;
}


and mutator gives you the right to change private member variables :

1
2
3
4
void setNum(int theNum)
{
      num = theNum;
}
If you're confused with all this just put every member of the class to public section until you know how to use private member.Then you can access normally like cout << SomeClass::num in the main() . Or if not ... accessor and mutator are necessary.

But doing so isn't what OOP is about...variable should be private and member function should be public except helping function that helps public function only.
Last edited on
finally found it the .h file must contain using namespace std; or it will not work right.
Topic archived. No new replies allowed.