473,651 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>()con st 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(f 2)){
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::Multi pliedBy (Fraction otherFraction)
{
Fraction reducedFraction ;

Fraction product(numerat or * otherFraction.n umerator,
denominator * otherFraction.d enominator);

Reduce(product. numerator, product.denomin ator, reducedFraction );
return reducedFraction ;
}


Fraction Fraction :: DividedBy (Fraction otherFraction)
{

Fraction reducedFraction ;

Fraction quotient(numera tor * otherFraction.d enominator,
denominator * otherFraction.n umerator);

Reduce(quotient .numerator, quotient.denomi nator, reducedFraction );

return reducedFraction ;
}

Fraction Fraction :: AddedTo(Fractio n otherFraction)
{

Fraction reducedFraction ;

Fraction sum(numerator * otherFraction.d enominator +
otherFraction.n umerator * denominator, denominator *
otherFraction.d enominator);

Reduce(sum.nume rator, sum.denominator , reducedFraction );


return reducedFraction ;
}
Fraction Fraction :: Subtract(Fracti on otherFraction)
{
Fraction reducedFraction ;
Fraction difference(nume rator * otherFraction.d enominator -
otherFraction.n umerator * denominator, denominator *
otherFraction.d enominator);

Reduce(differen ce.numerator,di fference.denomi nator, reducedFraction );

return reducedFraction ;

}

void Fraction :: Reduce (int& numerator,int& denominator,Fra ction&
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%num erator;
denominator = numerator;
numerator = remainder;

}

gcf = denominator;

}


void Fraction :: set(int inNumerator, int inDenominator)
{
numerator = inNumerator;
denominator = inDenominator;
}
bool Fraction :: isEqualTo(Fract ion otherFraction)
{
if(numerator==o therFraction.nu merator &&
denominator == otherFraction.d enominator)
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(Fr action otherFraction);
Fraction DividedBy(Fract ion 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,Fra ction& reducedFraction );
};
#endif

Jul 19 '05 #1
1 4227
"Luis" <ki***@comcast. net> wrote in <FAXXa.74496$YN 5.55963@sccrnsc 01>:
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>()co nst 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(Fr action otherFraction);
Fraction DividedBy(Fract ion 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,Fra ction&
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
3479
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 non-zero index is returned. Below is my attempt, which generates compiler errors: namespace Reference {
20
2488
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 keyword a lot less usable. Can anybody tell me what that good reason is? TIA,
2
3627
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
8
10085
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) When I asked why, I was told that it facilitated calling it as:
20
2434
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 doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
4
3297
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 time, and inline functions are all defined in headers. " Can someone explain to me why some of the const objects must be defined in the header file?
23
2312
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
1924
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 course the buffers are filling up with data. So some variable values are changing. So what is rule for a const member function? Is it that only member variables cannot change? But local variables inside the function can?
5
1984
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 function, and then figure that the function will not be modifying the list at all, so a const qualifier seems appropriate in the parameter. So essentially I have:
0
8349
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8576
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.