Operator Overloading

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&);

public:
Exams(); // default constructor

//Exams operator+=(const Exams&) const;
//Exams operator/=(const Exams&) const;

void assign();
void display();

private:
int examScore;
};

Exams::Exams ()
{
examScore = 0;
};

Exams operator+=(const Exams& first, const Exams& second)
{
Exams tempTot;

tempTot.examScore = first.examScore + second.examScore;
return tempTot;
}


/*
Exams Exams::operator+=(const Exams& passExam) const
{
Exams tempExam;

tempExam.examScore = this->examScore + passExam.examScore;
return tempExam;
}
*/


/*
Exams Exams::operator/=(const Exams& passExam) const
{
Exams tempExam;

tempExam.examScore = examScore + passExam.examScore;
return tempExam;
}
*/


int main()
{
Exams aClass[3], tot;

for (int x = 0; x < 3; x++)
{
aClass[x].assign();
tot += aClass[x];
}
//tot.examScore /= 3;

tot.display();
aClass[0].display();
aClass[1].display();
aClass[2].display();


system("pause"); //added pause to review work
//cin.get();
return 0;
}

void Exams::assign()
{
cout << "Enter exam score: "; cin >> examScore;
}

void Exams::display()
{
cout << "Tot exam score average: " << examScore << endl;
}
1
2
3
4
5
Exams operator+=(Exams& first, const Exams& second)
{
first.examScore = first.examScore + second.examScore;
return first;
}


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.

Sorry for any typos but it's late over here.
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.

1
2
3
4
5
Exams operator+=(Exams& first, const Exams& second)
{
	first.examScore = first.examScore + second.examScore;
	return first;
}
Your friend isn't

Exams operator+=(Exams& first, const Exams& second)

it's

Exams operator+=(const Exams& first, const Exams& second)

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).

Andy

Thanks! It was that simple oversights got me. But the your help got me through it. the code works. Thanks eidge and andtwestken!

any other advice on my final code will be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <iomanip>
using namespace std;


class Exams
{
	friend Exams operator+=(Exams&, const Exams&);
	friend Exams operator/=(Exams&, const int);

	public:
		Exams(); // default constructor

		void assign();
		void display();

	private:
		int examScore;
};

Exams::Exams ()
{
	examScore = 0;
};

Exams operator+=(Exams& first, const Exams& second)
{
	first.examScore += second.examScore;
	return first;
}

Exams operator/=(Exams& first, const int a)
{
	first.examScore /= a;
	return first;
}


int main()
{
	Exams aClass[3], tot;

	for (int x = 0; x < 3; x++)
	{
		aClass[x].assign();
		tot += aClass[x];
	}
	
	tot /= 3;
	
	tot.display();


system("pause"); //added pause to review work
//cin.get();
return 0;
}

void Exams::assign()
{
	cout << "Enter exam score: "; cin >> examScore;
	cout << endl;
}

void Exams::display()
{
	cout << "Tot exam score average: " << examScore << endl;
}
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...)
Last edited on
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.
Is the book you use a standard text, or something written by your college/school?

Do you get a worked examples after you complete your exercises?

Are you also studying program design as well as how to code in C++ ??
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <iomanip>
using namespace std;


class Exams
{
/***********************************************************************************************
commented out code - this was the first effort to overload
	friend Exams operator+=(Exams&, const Exams&); //overloading += operator 
	friend Exams operator/=(Exams&, const int); //overloading /= operator
***********************************************************************************************/

	public:
		Exams operator+=(Exams); //overloading += operator //overloading += operator
		Exams operator/=(int); //overloading /= operator //overloading /= operator

		Exams() {examScore = 0;}; // default constructor prototype
		void assign(); // member method prototype to capture exam grades
		void display(); // member method prototype to display grade

	private:
		int examScore; // declaring member variable 
};

/*
Exams::Exams ()  //default constructor
{
	examScore = 0;
};
*/

Exams Exams::operator+=(Exams param)// += overloading method 
{
	Exams temp;
	temp.examScore += param.examScore;
	return temp;
}

Exams Exams::operator/=(int a) // /= overloading method
{
	Exams temp;
	temp.examScore /= a;
	return temp;
}

/***********************************************************************************************
commented out code - this was the first effort to overload

Exams operator+=(Exams& first, const Exams& second) // += overloading method
{
	first.examScore += second.examScore;
	return first;
}

Exams operator/=(Exams& first, const int a) // /= overloading method
{
	first.examScore /= a;
	return first;
}
***********************************************************************************************/

int main() /************************************************************************************
                 the main function was provide by the instructor*******************************/
{
	Exams aClass[3], tot;

	for (int x = 0; x < 3; x++)
	{
		aClass[x].assign();
		tot += aClass[x];
	}
	
	//tot /= 3;
	
	tot.display();

system("pause"); //added pause to review work
//cin.get();
return 0;
}

void Exams::assign() // class member method to capture score
{
	cout << "Enter exam score: "; cin >> examScore;
	cout << endl;
}

void Exams::display() // class member method to display score
{
	cout << "Tot exam score average: " << examScore << endl;
}
When you're implementing an operator as a member, you need to act on the object instance, not a temp. So you get:

1
2
3
4
5
Exams& Exams::operator+=(const Exams& param) 
{
	examScore += param.examScore;
	return *this;
}


Note the return by ref and param passing by const ref.

Andy

P.S. You mean the Malik book?
Topic archived. No new replies allowed.