Stupid Class/Object Initialization Question

I have what I assume is a very stupid/simple question regarding arrays. Yes, this is a homework assignment for a second semester programming class (1st semester was in Java, this one is C++).

We're supposed to create an array buffer class which will have 7 methods: copyFromArray(), print(), sum(), mean(), max(), min() and range(). The buffer class should also have three properties: a constant named 'BUFFER_SIZE', an array of ints called 'buffer' and a variable named 'length' for the amount of data in the buffer. Carrying out all of those methods is easy enough, no problem with those.

In our professor's documentation for the assignment, only the copyFromArray() method accepts arguments which are the array created in main() and it's length, none of the other methods accept any arguments. So, what I don't understand is how are any of those other methods supposed to get the data from the array to actually do any of the operations? I did a simple print() function that just loops through the elements of the array and prints them out to the console but if that loop isn't in the copyFromArray() function, it just prints random/garbage numbers, obviously because none of the elements in the array are actually initialized.

I suspect I'm missing something in the constructor for the 'buffer' class but I'm not sure what it is and I've scoured our book, the web and this forum for a clue but haven't found one yet. I've also emailed our professor but haven't heard back from him yet so I'm reaching out to all of you. Like most people here, I'm not looking for someone to write the code for me, I'm just looking for a hint so I can get over this hump.

This is main.cpp (from the professor's document, he didn't provide anything else)

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
#include <iostream>
#include "DataBuffer.h"

using std::cout;
using std::endl;
using std::string;

void testDataBuffer(int arr[], int length);

int main()
{
    const int ARR0_LEN = 10;
    int arr0[ARR0_LEN] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    testDataBuffer(arr0, ARR0_LEN);

    return 0;
}

void testDataBuffer(int arr[], int length)
{
    DataBuffer data;
    data.copyFromArray(arr, length); // this works
    data.print(arr, length); // this works
    data.printBuffer(); // this outputs garbage
}


This is DataBuffer.h

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
#ifndef DATABUFFER_H
#define DATABUFFER_H

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;
using std::to_string;

class DataBuffer
{
    public:
        DataBuffer();
        void copyFromArray(int arr[], int length);
        void print(int arr[], int length);
        void printBuffer(); // my testing function
        int sum();
        double mean();
        int max();
        int min();
        int range();

    private:
        static const int BUFFER_SIZE = 256;
        int buffer[];
        int length;
        int intSum;
};

#endif // DATABUFFER_H 


And this is the implementation file DataBuffer.cpp

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
#include "DataBuffer.h"

DataBuffer::DataBuffer()
{

}

void DataBuffer::copyFromArray(int arr[], int length) // This works as expected
{
    this->length = length;
    int buffer[length];
    for (int i = 0; i < length; i++)
    {
        buffer[i] = arr[i];
        cout << buffer[i] << endl;
    }
}

void DataBuffer::print(int arr[], int length) // This works as expected
{
    this->length = length;
    cout << "Length is: " << length << endl;
    cout << "{" << arr[0];
    for (int i = 1; i < length ; i++)
    {
        cout << ", " << arr[i];
    }
    cout << "}" << endl;
}

void DataBuffer::printBuffer() // This prints random ints, array elements not initialized
{
    for (int i = 0; i < 10; i++)
    {
        cout << buffer[i] << endl;
    }
}

int DataBuffer::sum()
{
    intSum = 0;
    for (int i = 0; i < length; i++)
    {
        intSum += arr[i];
    }
    return intSum;
}

double DataBuffer::mean()
{
    double dblMean = 0;
    return dblMean;
}

int DataBuffer::max()
{
    int intMax = 0;
    return intMax;
}

int DataBuffer::min()
{
    int intMin = 0;
    return intMin;
}

int DataBuffer::range()
{
    int intRange = 0;
    return intRange;
}


Again, I understand why the printBuffer() function prints garbage, I just don't understand how to make it get the correct buffer[] values since it doesn't accept any arguments. Is it even possible? Is it possible our professor's notes are purposely misleading so as to make us go "Hey, we need to pass arguments to this stuff in here"?

Thank you!
Last edited on
Do you understand what member functions are and what the this pointer is?

http://www.cplusplus.com/doc/tutorial/classes/ <-- should help a lot
Hey, thanks for the quick reply LB!

Well, if the implication is that the answer lies within, then apparently no, I don't completely understand what member functions are and what the this pointer is. :)

I mean, I understand that a member function is a function within the given class and, assuming it's public, can be accessed from outside that class. And I do understand what this does in the sense that it's used to refer to the invoking object so I'm back to my original thought of not invoking or constructing the buffer correctly.

So I just read through that tutorial but the light bulb has not yet illuminated; I'll probably have to think about it for a little while. I do see that my default constructor is doing nothing (but I knew that before) and I'm thinking that's irrelevant at the moment. I'll keep reading up on those two topics.
Last edited on
8
9
10
11
12
13
14
15
16
17
void DataBuffer::copyFromArray(int arr[], int length) // This works as expected
{
    this->length = length;
    int buffer[length];
    for (int i = 0; i < length; i++)
    {
        buffer[i] = arr[i];
        cout << buffer[i] << endl;
    }
}
Line 11 - that variable is unrelated to the member variable.
Thanks again LB. I think I understand what I was doing wrong but I'm not sure I can correctly verbalize it.

Removing that line did get me going in the right direction but then the program would segfault at the end. Also, the printBuffer() function reported the variable length as having a value of 0. My suspicion for the cause of the segfault was because the size of the buffer[] array. An "out of bounds" error, if you will. So, going back to my header file, changing the initialization of the buffer[] array to int buffer[BUFFER_SIZE] resolved that. The variable length is also now correct as far as all of the other member functions are concerned.

So let's see if I have this straight...

* The DataBuffer object gets created
* The constant BUFFER_SIZE is initialized at 256 and the integer array named buffer gets initialized with a size of BUFFER_SIZE
* The member variable length gets set when the copyFromArray() function is executed and the individual elements of the buffer[] array are set to the corresponding elements of the arr[] array up until arr[(length - 1)] (from the for loop, obviously).
* From that point forward, the values of length and buffer[] apply to the entire scope of the object until we're done working with that particular object and either exit or create a new one (or use a destructor, of course)

Is that a good summary of what's going on? I suppose I should edit the subject and change this from "Array" to "Class/object Initialization" question.
Topic archived. No new replies allowed.