I'm having some serious trouble understanding object-oriented programming. I currently have this assignment for Programming Project 6 for Chapter 8. This is the problem that I'm trying to solve.
6. Define a class named MyInteger that stores an integer and has functions to get and set the integer’s value. Then, overload the [] operator so that the index returns the digit in position i, where i = 0 is the least-significant digit. If no such digit exists then –1 should be returned.
For example, if x is of type MyInteger and is set to 418, then x[0] should return 8, x[1] should return 1, x[2] should return 4, and x[3] should return negative 1.
So you can see where I am on understanding this, I can make out what I need to from this is
1. Make a class named MyInteger
2. Make a private member variable (an integer)
3. Use accessor (getter) and mutator (setter) functions to get and set the value of the integer.
4. Use an overloaded operator []
Where I'm lost
1. How am I supposed to use the [] operator to return just the digit of position (index) i?
I DO understand that I am supposed to do this
Example 5367
[0] = 7
[1] = 6
[2] = 3
[3] = 5
[4] = negative 1 (-1)
But how am I to implement it with the [] overloaded operator?
I also have to worry about these two extra constraints as well.
1. implement a prefix increment operator overload that increases the internal value of the object by 10
2. implement a postfix increment operator overload that increases the internal value of the object by 1
I'm willing to work with someone to try and understand this concept before test time rolls around for me. Not just looking for an answer here, but an explanation. (If it's not too much to ask)
I have not learned about pointers yet either, so I prefer not to use them.
Here is what I have so far. (For some reason, I'm getting an undefined reference error)
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
// ---------------------------------------------------------------------------
// Name:
// Course: CS 255
// Assignment: MyInteger - Class
// Description: Fill this in later!
// ---------------------------------------------------------------------------
#include <iostream>
using namespace std;
// Class Definition
class MyInteger
{
public:
MyInteger ();
int getValue () const;
void setValue (int inputValue);
int operator [] (int index); // Member Function
private:
int value;
};
int main ()
{
MyInteger other;
MyInteger answer;
answer.setValue(13);
cout << answer.getValue();
return 0;
}
// Class Implementation
int MyInteger::getValue() const
{
return value;
}
void MyInteger::setValue(int inputValue)
{
value = inputValue;
}
int MyInteger::operator [] (int index)
{
if (index == 0)
{
return (value & 10);
}
else if (index == 1)
{
return 4;
}
else
{
return -1;
}
}
|