473,395 Members | 1,629 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.

overloading ! operator for factorial

Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!
I can get the factorial of x.
The problem is, that the operator I want to overload takes no parameter.
Most examples I'ver seen, have a parameter. I wonder if this causes the
problem.
My code is

float A::operator !()

{

float product = 0.0;
if (n != 0)

{

for ( ; n > 0 ; n--) // We omit the initialization expression

product *= n; // since we have initialized n with the constuctor
return product;

}

else

return 1.0;

}
Jul 22 '05 #1
9 7604


Silver wrote:
Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!


How is that going to work? operator! applies to the argument to the
right of it.

Jul 22 '05 #2
Silver wrote:

Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!


That's not possible. The best you can get would be
!x

But it still is not a good idea. The reason is: In C++ all the standard
operators have a predefined meaning, in the case of ! this is 'not'.
So when reading through a source code, everybody seeing the line:

i = !x;

thinks immediatly: i gets assigned 'not x' and not i gets assigned the
factorial of x.

There is a simple rule when it comes to operator overloading: Do as int does.
This means: Try to implement operators in a way as you would implement them
for int (if you could). In principle you can create a class, which has
an operator+ which does the equivalent of multiplication. But this would
be confusing, cause everybody reading a + b thinks of addition and not
of multiplication. That's what is ment with: Do as int does.

Anyway:

#include <iostream>
using namespace std;

class A
{
public:
A( int n ) : m_n( n ) {}
double operator!() const {
double Result = 1.0;
for( int i = 1; i <= m_n; ++i )
Result *= i;
return Result;
}
private:
int m_n;
};

int main()
{
cout << !A(5) << endl;

A Test(8);
cout << !Test << endl;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
I now begin to understand the use of operator overloading.
Thanks.

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...
Silver wrote:

Hi everyone,

I want to overload the ! operator, so that when I write this in main
x!
That's not possible. The best you can get would be
!x

But it still is not a good idea. The reason is: In C++ all the standard
operators have a predefined meaning, in the case of ! this is 'not'.
So when reading through a source code, everybody seeing the line:

i = !x;

thinks immediatly: i gets assigned 'not x' and not i gets assigned the
factorial of x.

There is a simple rule when it comes to operator overloading: Do as int

does. This means: Try to implement operators in a way as you would implement them for int (if you could). In principle you can create a class, which has
an operator+ which does the equivalent of multiplication. But this would
be confusing, cause everybody reading a + b thinks of addition and not
of multiplication. That's what is ment with: Do as int does.

Anyway:

#include <iostream>
using namespace std;

class A
{
public:
A( int n ) : m_n( n ) {}
double operator!() const {
double Result = 1.0;
for( int i = 1; i <= m_n; ++i )
Result *= i;
return Result;
}
private:
int m_n;
};

int main()
{
cout << !A(5) << endl;

A Test(8);
cout << !Test << endl;
}

--
Karl Heinz Buchegger
kb******@gascad.at

Jul 22 '05 #4
While it was 4/12/03 1:20 pm throughout the UK, Karl Heinz Buchegger
sprinkled little black dots on a white screen, and they fell thus:

<snip>
There is a simple rule when it comes to operator overloading: Do as
int does. This means: Try to implement operators in a way as you
would implement them for int (if you could). In principle you can
create a class, which has an operator+ which does the equivalent of
multiplication. But this would be confusing, cause everybody reading
a + b thinks of addition and not of multiplication. That's what is
ment with: Do as int does.

<snip>

That makes sense. Shame someone didn't invent an operator that means
concatenation, leaving std::string stuck with +. (My hat goes off (not
that I ever have any reason to wear one) to D, which has introduced ~ as
a binary op for this purpose.)

A related question is what operator should be overloaded to represent
the dot product of vectors, particularly if you want to implement cross
product as well. I've been using % for this, for want of a better
option....

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #5
Stewart Gordon wrote:
While it was 4/12/03 1:20 pm throughout the UK, Karl Heinz Buchegger
sprinkled little black dots on a white screen, and they fell thus:

<snip>
There is a simple rule when it comes to operator overloading: Do as
int does. This means: Try to implement operators in a way as you
would implement them for int (if you could). In principle you can
create a class, which has an operator+ which does the equivalent of
multiplication. But this would be confusing, cause everybody reading
a + b thinks of addition and not of multiplication. That's what is
ment with: Do as int does.


<snip>

That makes sense. Shame someone didn't invent an operator that means
concatenation, leaving std::string stuck with +. (My hat goes off (not
that I ever have any reason to wear one) to D, which has introduced ~ as
a binary op for this purpose.)

A related question is what operator should be overloaded to represent
the dot product of vectors, particularly if you want to implement cross
product as well. I've been using % for this, for want of a better
option....

Stewart.


I prefer the FORTRAN concept: create explicit functions when no
operator exists. So given three vectors, the dot product becomes:
result = dot_product(vector_a, vector_b);
Rather than
result = vector_a % vector_b;
or
result = vector_a . vector_b;
or
result = vector_a * vector_b;
or
result = vector_a *. vector_b;

This issue seems to be one of typing rather than readability
or clarity.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #6
Back with another question:
Why doesn't this work?

long A::operator &(const A& alpha) const

{

return this->n + alpha.n;

}

long A::operator &(const int& value) const

{

return this->n + value;

}

NOTES: A is the name of my class. n is a private variable of the class.

...............

.............

cout << "\na[" << i << "].n & b.n = " << a[i]&b;

NOTES: I have an array of A objects (that is a[]) and b is another A object

I get this error message when compiling with MS visual c++ .NET

error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'A' (or there is no acceptable conversion)

Jul 22 '05 #7

"Silver" <ar******@med.auth.gr> wrote in message news:bq**********@nic.grnet.gr...
cout << "\na[" << i << "].n & b.n = " << a[i]&b;

NOTES: I have an array of A objects (that is a[]) and b is another A object

I get this error message when compiling with MS visual c++ .NET

error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'A' (or there is no acceptable conversion)

<< binds tighter than binary-&.
What you wrote is parsed as
... ( ... << a[i]) &b );

You want to write
... << (a[i]&b);
Jul 22 '05 #8
Silver wrote:
Back with another question:
Why doesn't this work?

long A::operator &(const A& alpha) const

{

return this->n + alpha.n;

}

long A::operator &(const int& value) const

{

return this->n + value;

}


Just curious, why do you reference member variables
using the "this" pointer when you can access them
directly?

long A::operator &(const int& value) const
{
return n + value;
}
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #9
Thomas Matthews wrote:
Silver wrote:
Back with another question:
Why doesn't this work?

long A::operator &(const A& alpha) const

{

return this->n + alpha.n;

}

long A::operator &(const int& value) const

{

return this->n + value;

}


Just curious, why do you reference member variables
using the "this" pointer when you can access them
directly?

long A::operator &(const int& value) const
{
return n + value;
}


May be an artifact of his IDE. A buddy of mine does that because if he uses "this->", he gets a drop down list of members.

Jul 22 '05 #10

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

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
3
by: Bob | last post by:
I can't seem to find the factorial operator. It's not in the math namespace, either. Is factorial not built in to the Framework? Bob
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.