I am have problems with operator overloading and my instructor hasn't provided much help.
I can get data into the array variable for the aClass object, but I can't get my += operator to sum the array scores into the tot object. Been stuck on that that I haven't been able to figure out averaging the tot object.
Any help would be appreciated.
Assignment
"You will need to create an Exams class with all the appropriate properties and methods. Then, you will need to create an array of Exams objects called aClass, and a single object called tot. The assign() method should prompt the user to input grades into the array and the tot object should keep track of the total. Then divide the tot object by 3 to compute the average. Then display the average."
#include <iostream>
#include <iomanip>
using namespace std;
class Exams
{
friend Exams operator+=(const Exams&, const Exams&);
Since you are using operator+= changes are made directly to the first argument of the function. No need to create a temporary object.
I believe you should still return the result because of the lvalue impact, correct me if I'm wrong.
Also you don't need to declare this function as a friend, you may use +=operator method directly as both arguments have the same object type. So your prototype should look like this:
Exams operator+=(const Exams&);
Also, you can't use the const qualifier as you'll need make changes to "this" object.
I made that change but Visual Studio says that examScore is inaccessible. I thought that frien function had acces to all members of the class. If I change it from a friend I need change the syntax. If I can't figure this out, it is unlikely I can figure out the other syntax.
You need to alter the function signature of the friend!
But I would do as eidge suggested and use a member function in this case. And I would prob. also use += inside the function instead of + (a bit neater and more consistent).
You code looks ok, given the problem description. Though I have to admit I find it a little bit on the vague side (esp. from the oo point of view.)
There are a few other comments I could make, but I think what you've done should be ok at this stage (member vs friend function aside). I obviously have no idea where you are in your studies!
Being nosey, I would be interested to know what your professor thinks of your code?
Andy
P.S. One trivial comment: it's preferable to initialize all member variables using the initializer list. This isn't important for intrinsic types, from a performance point of view, but for consistency I always use it (except for debug variables...)
The code meets the assignment requirement. I was having a little problem with this weeks assignment because it just isn't clicking.
This is my second class and I have not coded before. It is an internet mediated class, the instructor isn't all that helpful, and the book causes confusion the way it is written. At least that is my experience.
I don't get any feedback on the code I create. If it compiles without error and performs the task give, all is okay.
It would be nice to get this kind of feedback on my coding.
We are using C++ Programming:Program Design Including Data Structures 5th Edition.
No. I just got a message stating I got 100% on the code I submitted.
No. There are some limited design discussions in the book.
Here is a revised version of my code not using friend functions. I am following the syntax from this site but I can't get the operation overloading to work.