I need some help with my program and the concept of OOP

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;
        
    }
    
    
}

Last edited on
closed account (E0p9LyTq)
Where is the definition for your constructor? You have definitions for the other class methods.
Here is a hint. The following program prints the digits in a number, starting with the least significant and working up to the most significant. You should be able to modify the code in printDigits() to do what you need.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream.h>
using std::cout;

void printDigits(int num)
{
    int digit;
    do {
	digit = num % 10;
	num = num / 10;
	cout << digit << '\n';
    } while (num);
}


void main()
{
  int num = 44957832;
  printDigits(num);
}

@FurryGuy well this is what I had originally, but it still gave me an error so I changed it to the code you see above.

@dhayden. I'll try to apply your approach and see if I understand it correctly with the overloaded operator.

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
108
#include <iostream>

using namespace std;


// Class Definition

class MyInteger

{
    
    public:
    
        MyInteger (); // You don't have to have this default constructor right here!
    
        MyInteger (int init);
    
        int getValue () const;
    
        void setValue (int inputValue);
    
        int operator [] (int index); // Member Function
    
    
    private:
    
        int value;
    
    
};



int main ()

{
    
    MyInteger other;
    
    MyInteger answer (13);
    
    
    cout << answer.getValue();
    
    return 0;
    
}

// Class Implementation



MyInteger::MyInteger (int init)

{
    
    value = init;
    
}


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;
        
    }
    
    
}
Last edited on
closed account (E0p9LyTq)
You are not providing a default constructor, MyInteger::MyInteger(), definition. You just added another constructor and its definition.

As the comment "says" on the default constructor declaration you don't need it. Remove it from the class declaration:

1
2
3
4
5
6
7
8
9
10
11
class MyInteger
{
public:
   MyInteger (int init);
   int getValue () const;
   void setValue (int inputValue);
   int operator [] (int index);

private:
   int value;
};
Last edited on
I actually fixed that before I looked back at your reply lol. It compiles fine now!

Now I need to understand how to implement % and / with the overloaded operator.

I understand that % and / extract the digit from right to left, provided you know the size of the number. It's just confusing me when it asks to implement it with the overloaded operator. (I've looked at videos, and the Absolute C++ book)

This is what I have now.

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
#include <iostream>

using namespace std;


// Class Definition

class MyInteger

{
    
public:
    
    
    MyInteger (int init);
    
    int getValue () const;
    
    void setValue (int inputValue);
    
    int operator [] (int index); // Member Function
    
    
private:
    
    int value;
    
    
};



int main ()

{
    
    MyInteger answer (418);
    
    cout << answer.getValue();
    
    return 0;
    
}

// Class Implementation



MyInteger::MyInteger (int init)

{
    
    value = init;
    
}


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 (value / 10);
        
    }
    

    else
        
    {
        
        return -1;
        
    }
    
    
}




Last edited on
Topic archived. No new replies allowed.