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:

#include <iostream>
using namespace std;

class Statistics
{
	private:
	int count;
	float num, total, average;

	public:	
	void calcAverage()
	{
		total = 0.0;
		int limit = 10;

		for (count = 0; count < limit; count++)
		{
			cout << "Enter a number: ";
			cin >> num;
			total = total + num;
		}
		average = total / count;
		cout << endl << endl << "AVERAGE: " << average << endl;
	}
};

int main()
{
	Statistics sp;
	sp.calcAverage();
	return 0;
}

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!

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.

Could the addition of the total be overloaded somehow?

total = total + num

For the output, could the average be overloaded?

average = total / count;
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.

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.

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?

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:

friend ostream & operator<<(ostream & os, const Statistics rhs)
{
    os << rhs.total;
    return os;
}

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;

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.

Hi,

In my opinion operator overloading is not applicable to your code at all since you have just object of your class "STATISTICS". Operator overloading can come to your aid only if you have multiple objects of the class and you want to modify their respective data members.

Hope this made sense. Or if you wanted to know something else, plz let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.