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++)
{
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.
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
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();
}
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);
}
}
////////////////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////////////////
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
{
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.
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.