overloading operators

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 15
Reputation: monocog is an unknown quantity at this point 
Solved Threads: 0
monocog monocog is offline Offline
Newbie Poster

overloading operators

 
0
  #1
Feb 10th, 2009
Before I begin, let me start by saying that this is NOT homework. I am also not enrolled in a C++ class. I am simply trying to learn C++ as I have time. I am using random assigments/tutorials/whatever I can find on the net and shopping amazon for books. I'm not trying to become an awesome programmer, just trying to get a grasp on basic concepts (I'm a network admin and work with a bunch of programmers and just want to be able to understand things better)

I wrote the program at the bottom to understand how classes work and would like to stick with this to understand overloading operators since I understand how this works.

Can someone show me what this program would look like with the following (or if I have typed something stupid in the following, ask me and I'll try to explain what I mean unless I am totally confusing myself):

constructor methods that initialize my variables
program modified with an overloaded (<<) output method that uses friend
program modified with an overloaded (+) method as a class member
program modified with an overloaded (+) method as a function outside of class

Here's the code, and sorry if this is something really easy, I'm just trying to wrap my head around it:


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Statistics
  5. {
  6. private:
  7. int count;
  8. float num, total, average;
  9.  
  10. public:
  11. void calcAverage()
  12. {
  13. total = 0.0;
  14. int limit = 10;
  15.  
  16. for (count = 0; count < limit; count++)
  17. {
  18. cout << "Enter a number: ";
  19. cin >> num;
  20. total = total + num;
  21. }
  22. average = total / count;
  23. cout << endl << endl << "AVERAGE: " << average << endl;
  24. }
  25. };
  26.  
  27. int main()
  28. {
  29. Statistics sp;
  30. sp.calcAverage();
  31. return 0;
  32. }
Last edited by monocog; Feb 10th, 2009 at 4:27 pm. Reason: redundant statement removed
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 15
Reputation: monocog is an unknown quantity at this point 
Solved Threads: 0
monocog monocog is offline Offline
Newbie Poster

Re: overloading operators

 
0
  #2
Feb 10th, 2009
That would be the original program modified three separate times. Not all of that in one program. I'd like to see how each works.

Thanks a ton!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 267
Lerner Lerner is offline Offline
Posting Virtuoso

Re: overloading operators

 
0
  #3
Feb 10th, 2009
You can overload operators to do whatever you want them to, almost. For example, you could overload the % to increment an int by 33 if you wanted. That being said, most overload implementations stick to reasonably traditional actions. So overloading the + operator almost always means addition if it is a numerical type and concatenation if it is a string type. However, that also means that using the operators even makes sense within the context of the usage of the class. For example, what do you want to output with the << operator and how would you use the addition operator in your class? It is easier to envision overloading those operators for a class modeling rational numbers.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 15
Reputation: monocog is an unknown quantity at this point 
Solved Threads: 0
monocog monocog is offline Offline
Newbie Poster

Re: overloading operators

 
0
  #4
Feb 10th, 2009
Could the addition of the total be overloaded somehow?

  1. total = total + num

For the output, could the average be overloaded?

  1. average = total / count;
  2. cout << endl << endl << "AVERAGE: " << average << endl;

It doesn't matter to me what is overloaded, I just want to see it work on this program if that is possible.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: overloading operators

 
0
  #5
Feb 10th, 2009
1. It's impossible to overload basic types operators, so don't try to overload an addition op for float total and average...
2. You may overload operators for your class Statistics (in other words, to define operators with class Statistic arguments), but what's an example of a reasonable operator of a class which is not a value of anything? Your class is a black hole, it has all private data members and the only (rather strange) action (user input required) member function. This class data members have unpredictable values before an user interaction via calcAverage member function. Evidently, you can't study C++ operator overloading with such class example.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 15
Reputation: monocog is an unknown quantity at this point 
Solved Threads: 0
monocog monocog is offline Offline
Newbie Poster

Re: overloading operators

 
0
  #6
Feb 10th, 2009
What if the program asked if you wanted to add more numbers after the initial calculation and calculate the new average then? Would my initial question be answerable in that case?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 267
Lerner Lerner is offline Offline
Posting Virtuoso

Re: overloading operators

 
0
  #7
Feb 10th, 2009
OK, if you insist, here's a relatively useless version of overloading the << operator to output the value of the data variable called total when the << operator is called on an object of type Statistics.

Place this snippet within the public section of your program:
  1. friend ostream & operator<<(ostream & os, const Statistics rhs)
  2. {
  3. os << rhs.total;
  4. return os;
  5. }

and this line just before return 0; in main()

cout << sp << endl;

Here's a snippet to overload the + operator implemented as a class method. It is used to add a float object to the member variable total modifying the value of total. Add it to the public section of the Statistics class:

void operator+(const float & rhs){ total += rhs;}

and add these lines just before return 0; to use it, assuming you have already overloaded the << operator as above:

float f = 1.23;
sp + f;
cout << sp << endl;
Last edited by Lerner; Feb 10th, 2009 at 10:31 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 267
Lerner Lerner is offline Offline
Posting Virtuoso

Re: overloading operators

 
0
  #8
Feb 10th, 2009
I hit Post Quick Reply sooner than I intended with last post. Here's the rest of what I intended to post now that I've tested it all.

Here's how to overload the + operator outside the class to do the same as it did in my previous post with an overloaded + operator as a class member. Add this line to the class in the public section:

void addToTotal(const float f) {total += f;}

Add this after the class declaration and before main();

void operator+(Statistics & stat, float input){stat.addToTotal(input);}

And finally add this before return 0;

float f2 = 0.1;
sp + f2;
cout << sp << endl;

Don't write both of these versions of the overloaded + operator into the same program as it causes a potential for ambuity when you use the + operator in main().

In addition, in my previous post, it would be tradition to pass a const reference to type Statistics rather than a const Statistics object to the << operator. I inadvertantly left out the & sign in my earlier post. It still works as I posted it earlier, but passing a reference, const or not, prevents the need to pass a copy of the object.

This is a really ugly demonstration of operator overloading and in my opinion, represents bad object design. I think you would benefit from writing a class where the use of these overloaded operators makes more sense.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC