473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

const functions?

can you look at my code, and tell
my wy the section of the client program which says :

const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.MultipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
does not work for me? I know that it is because they have const, so private
members cannot be changed, but some of my function require this. DO i have
to make new functions such as Fraction Fraction <function>()const and it's
counterpart without the const? or do i simply have to change all of my
functions to value returning?
below is the client program with the implmentation file below it, then the
header file.
#include <iostream>
#include "fraction.h"
using namespace std;

#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
Fraction f1(9,8);
Fraction f2(2,3);
Fraction result;

cout << "The result starts off at ";
result.print();
cout << endl;

cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.MultipliedBy(f2);
result.print();
cout << endl;

cout << "The quotient of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.DividedBy(f2);
result.print();
cout << endl;

cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.AddedTo(f2);
result.print();
cout << endl;

cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Subtract(f2);
result.print();
cout << endl;

if (f1.isEqualTo(f2)){
cout << "The two fractions are equal." << endl;
} else {
cout << "The two fractions are not equal." << endl;
}

const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.MultipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
}



Fraction :: Fraction()
{
numerator = 0;
denominator = 1;
}


Fraction :: Fraction (int inNumerator, int inDenominator)
{
numerator = inNumerator;
denominator = inDenominator;
}


void Fraction :: print()
{
cout << numerator << "/" << denominator <<endl;
}


Fraction Fraction::MultipliedBy (Fraction otherFraction)
{
Fraction reducedFraction;

Fraction product(numerator * otherFraction.numerator,
denominator * otherFraction.denominator);

Reduce(product.numerator, product.denominator, reducedFraction);
return reducedFraction;
}


Fraction Fraction :: DividedBy (Fraction otherFraction)
{

Fraction reducedFraction;

Fraction quotient(numerator * otherFraction.denominator,
denominator * otherFraction.numerator);

Reduce(quotient.numerator, quotient.denominator, reducedFraction);

return reducedFraction;
}

Fraction Fraction :: AddedTo(Fraction otherFraction)
{

Fraction reducedFraction;

Fraction sum(numerator * otherFraction.denominator +
otherFraction.numerator * denominator, denominator *
otherFraction.denominator);

Reduce(sum.numerator, sum.denominator, reducedFraction);


return reducedFraction;
}
Fraction Fraction :: Subtract(Fraction otherFraction)
{
Fraction reducedFraction;
Fraction difference(numerator * otherFraction.denominator -
otherFraction.numerator * denominator, denominator *
otherFraction.denominator);

Reduce(difference.numerator,difference.denominator , reducedFraction);

return reducedFraction;

}

void Fraction :: Reduce (int& numerator,int& denominator,Fraction&
reducedFraction)
{
int gcf;

GCF(numerator, denominator,gcf);
reducedFraction.set(numerator/gcf,denominator/gcf);
}




void Fraction :: GCF(int numerator,int denominator,int& gcf)
{

int remainder = 1;
while(remainder!=0)
{

remainder = denominator%numerator;
denominator = numerator;
numerator = remainder;

}

gcf = denominator;

}


void Fraction :: set(int inNumerator, int inDenominator)
{
numerator = inNumerator;
denominator = inDenominator;
}
bool Fraction :: isEqualTo(Fraction otherFraction)
{
if(numerator==otherFraction.numerator &&
denominator == otherFraction.denominator)
return true;
else
return false;
}


HEADER FILE

#ifndef Fraction_H
#define Fraction_H
class Fraction
{
public:
Fraction();
Fraction(int inNumerator, int inDenominator);
void print() ;
Fraction MultipliedBy(Fraction otherFraction);
Fraction DividedBy(Fraction otherFraction);
Fraction AddedTo ( Fraction otherFraction);
Fraction Subtract ( Fraction otherFraction);
bool isEqualTo (Fraction otherFraction) ;

private:
int numerator;
int denominator;
void GCF(int numerator, int denominator,int& gcf);
void set(int numerator, int denominator);
void Reduce (int& numerator,int& denominator,Fraction& reducedFraction);
};
#endif

Jul 19 '05 #1
1 4196
"Luis" <ki***@comcast.net> wrote in <FAXXa.74496$YN5.55963@sccrnsc01>:
can you look at my code, and tell
my wy the section of the client program which says :

const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.MultipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
does not work for me? I know that it is because they have const, so
private members cannot be changed, but some of my function require this.
DO i have to make new functions such as Fraction Fraction
<function>()const and it's counterpart without the const? or do i simply
have to change all of my functions to value returning?
You are mixing to many things.
First, print member function should be const.
That's the cause of your errors.
You have to declare this function like this:

class Fraction {

public:
void print() const;
...
};

below is the client program with the implmentation file below it, then
the header file.
HEADER FILE

#ifndef Fraction_H
#define Fraction_H
class Fraction
{
public:
Fraction();
Fraction(int inNumerator, int inDenominator);
void print() ;
void print() const;
you may also replace it by friend operator <<(
std::ostream& os,const Fraction& f);

- did you noticed the const.
Fraction MultipliedBy(Fraction otherFraction);
Fraction DividedBy(Fraction otherFraction);
Fraction AddedTo ( Fraction otherFraction);
Fraction Subtract ( Fraction otherFraction);
bool isEqualTo (Fraction otherFraction) ;
.. const
bool isEqualTo(const Fraction& other) const;
this all could be replaced by operators , but it's up to you to decide.
Anyway it's better if the argument passed is const Fraction& , almost
the same but additional copy construction is then not neccessery.
Equality comparison is of course const.
And all the functions above should be const because they take
two const Fractions as arguments and return another Fraction.
Would you define a function that modifies this Fraction then
const would not be permited.
Usually you write :

Fraction& operator +=(const Fraction& other);
// add other fraction to this one, it's not const
and

Fraction operator +(const Fraction& other) const {
Fraction result(*this); result +=other; return result;
}


private:
int numerator;
int denominator;
void GCF(int numerator, int denominator,int& gcf);
and why not
static int GCF(int numerator,int denom);
void set(int numerator, int denominator);
void Reduce (int& numerator,int& denominator,Fraction&
reducedFraction);
this looks ugly too, write a member function which reduces this fraction,
it's enough, as you will see.

void Reduce();
};
#endif


I hope it's not too much. Good luck.

grzegorz
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
2
by: Angus | last post by:
I have a member function, int GetLogLevel() which I thought I should change to int GetLogLevel() const - I made the change and it works fine. But in the function I am creating buffers and of...
5
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.