Question
My question is at the bottom. I am creating a module for a Mark to encapsulate between 0 and 100. I have a few operator
My question is at the bottom.
I am creating a module for a Mark to encapsulate between 0 and 100.
I have a few operator and conversion overload functions so far:
class Mark {
int mark;
public:
Mark(); //constructor ~Mark(); //deconstructor Mark(int value); // mark is created using int value that sets the value of mark void setEmpty(); // mark = 0 bool isValid()const; //checks if mark is valid //type conversion operator int() const; // mark casted to an int. result would be value of the mark or zero if mark is invalid operator double() const; //mark casted to a double for gpa..equivalent of int value operator char() const; // mark casted to char type...result would be grade latter value of mark //binary member operator Mark& operator += (int w); // int is added to the value of mark Mark& operator = (int i); // mark is set to an integer
};
main program
#include#include "Mark.h" using namespace std; using namespace sdds; int main() { Mark m, n(25), k(200), p(-10); cout << "int ............" << endl; cout << int(m) << endl; cout << int(n) << endl; cout << int(k) << endl; cout << int(p) << endl; cout << "+= ............." << endl; cout << int(m += 20) << endl; cout << int(n += 20) << endl; cout << int(k += 20) << endl; cout << int(n += 60) << endl; cout << "= .............." << endl; cout << int(m = 80) << endl; cout << int(n = 120) << endl; cout << int(k = 70) << endl; cout << "double ........." << endl; m = 50; n = 80; k = 120; cout << double(m) << endl; cout << double(n) << endl; cout << double(k) << endl; cout << "char ..........." << endl; cout << char(m) << endl; cout << char(n) << endl; cout << char(k) << endl; cout << "int += Mark ..." << endl; int val = 60; cout << (val += n) << endl; cout << (val += k) << endl; return 0; }
output
int ............ 0 25 0 0 += ............. 20 45 0 0 = .............. 80 0 70 double ......... 1 4 0 char ........... D A X int += Mark ... 140 140
HERE IS MY QUESTION
So I am trying to add mark to an integer and return that integer and any invalid marks would not add any value to the integer. In the main program, this is the code, its the very last 2 lines.
Mark n(80), k(120); cout << (val += n) << endl; cout << (val += k) << endl;
and the output would be
140 140
I am not able to define this without their being errors saying that my += operator from above is ambiguous. So I am thinking that this needs to be a helper function?
Any help on how to go about this? Any suggestions? Any hints?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started