-1

This is a part of my assignment that I was given in my college CSCI course using C++

Create a class Array, representing an arran of size n, with the private properties:

class Array {private: double * a {}; int n;

  • Constructor by default
  • Constructor by specifying the size of the array
  • Constructor by specifying the size of the array, and the default value for all the elements in the array
  • Constructor by copy
  • Destructor
  • Accessor: Retrieve the size of the array

I was faced with the task of defining an array using a class and the class must contain what each bullet point says. However, I am stuck on bullet point number 3. I am attempting to fill the array with default values of 0 for a[0], 1 for a1, etc. depending on the size of the array that a user inputs and sets as value n. In doing so all that my code is doing is assigning each and every element in the array with a value of 0.

#include <iostream>

using namespace std;

class MyArray {
    private:
        double * a {};
        int n;

    public:
        MyArray();
        explicit MyArray(int size);
        MyArray(int size, double def_val);
        MyArray(const MyArray & a2);
        ~MyArray();
        int get_Size() const;
        void SetElement(int i, double x);
        double GetElement(int i);
        void display();
};

MyArray::MyArray(int size) {
    n = size;
    a = new double[size];

}

MyArray::MyArray() {
    a = nullptr;
    n = 0;
}

double MyArray::GetElement(int i) {
    return a[i];
}

void MyArray::SetElement(int i, double x) {
    a[i] = x;
}

MyArray::MyArray(const MyArray & a2) {
    n = a2.n;
    for (int i = 0; i < n; i++) {
        a[i] = a2.a[i];
    }
} //constructor by copy

MyArray::~MyArray() {
    delete[] a;

}

MyArray::MyArray(int size, double def_val) { //THIS IS WHERE IT SHOULD ASSIGN           
    n = size;
    a = new double[size];

    for (int i = 0; i < size; ++i) {

        a[i] = i;
    }

}

int MyArray::get_Size() const {
    return n;
}

void MyArray::display() {
    cout << "The values of the array MyArray are: " << endl;
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
}

int main() {
    MyArray a(5);
    a.display();
    return 0;
}

The output is this

The values of the array MyArray are:

0 0 0 0 0

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 2
    You have multiple constructors. The one you are using is the first one, which only allocates the array and does not set any values. – Garr Godfrey Sep 15 '21 at 18:31
  • Usually, the *default* value is zero... If I read correctly, that bullet point in your assignment is asking to set the *same* initial value to all of the elements. – Bob__ Sep 15 '21 at 18:31
  • What is your actual question? Your code calls a constructor that populates `MyArray`'s size to 5. The result is an internal double array of size 5. The value of each element in the array is 0. – h0r53 Sep 15 '21 at 18:34
  • 1
    Please also note that your code is *not* calling that particular constructor. – Bob__ Sep 15 '21 at 18:37
  • Alright, so that makes a lot more sense, I was not aware that by default value it meant to populate the array with its default value that is 0, I was confused by the question and it makes a lot more sense now. I appreciate the feedback and help as I am new here. Thank you! – CaliChzHedz Sep 15 '21 at 18:43
  • note there's a mistake in the copy ctor: you require `a = new double[size];` before copying a2 elements in the instance you are creating by copy – Gian Paolo Sep 15 '21 at 18:57
  • If you use `a = new double[size];` that is an uninitialized array. If you use `a = new double[size]{};` that is an array initialized to `0.0` for each element. Why would you want an *uninitialized* array? Let's say you are about to initialize (technically, not initialize but assign) it with some interesting values, so you'd want to avoid the overhead of "wasting" time initializing it to zero, only to reassign a moment later. – Eljay Sep 15 '21 at 19:26
  • @CaliChzHedz -- Your copy constructor is broken, as you failed to allocate memory for the current object array. The second thing is that you lack a user-defined assignment operator for `MyArray` -- if `main` were simply this: `{ MyArray a(5); MyArray b(6); b = a; }` you have a double-free error and a memory leak. – PaulMcKenzie Sep 15 '21 at 19:29
  • Unrelated: [The Rule of Three](https://en.cppreference.com/w/cpp/language/rule_of_three) states that If you have a copy constructor, destructor or assignment operator, you almost certainly want the other two. In this case you have cops constructor and destructor, so you should complete the set with an assignment operator. [Here's a link to a laughably simple way to write the assignment operator](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) – user4581301 Sep 15 '21 at 19:34
  • @PaulMcKenzie Where is the memory leak exactly? I am having trouble understanding what is wrong with it, Also how would I fix this? – CaliChzHedz Sep 16 '21 at 21:52
  • @CaliChzHedz -- The `a` object allocated memory for the array. The `b` object allocated memory for its array. Now you set the `b` objects allocated pointer value to `a` when `b = a;` is executed. How are you going to deallocate the memory you originally allocated for `b`, when you overwrote that pointer value? That's why a user-defined assignment operator is necessary. Doing this with pointers, `=`, does not deallocate memory -- it overwrites the pointer value, and the original pointer value is needed to do the deallocation. – PaulMcKenzie Sep 16 '21 at 22:38
  • @CaliChzHedz As to how to fix it, that is what the link to the "rule of 3" is for. You are missing the user-defined assignment operator: `MyArray& operator=(const MyArray&);`. If you were given this class by a teacher or someone or some site purporting to teach C++, they missed this vitally important piece of the puzzle. I would say that whoever gave this to you is not a C++ programmer, as no C++ programmer (or teacher) would write a class stub like that and totally forget about the user-defined assignment operator. – PaulMcKenzie Sep 16 '21 at 22:43
  • See the comment made by @user4581301 as to how to write a super-simple assignment operator. – PaulMcKenzie Sep 16 '21 at 22:56
  • Sadly bad C++ is one of the world's most frequently taught programing languages. – user4581301 Sep 16 '21 at 23:01

3 Answers3

3

You are calling the wrong constructor you need to call this one: MyArray(int size, double def_val) not this: MyArray(int size);

like this:

    int main(){
MyArray a(5,5);
a.display();

     }

you will get the output as 0,1,2,3,4 But I don't think this is what the assignment wants you to do As you said it should "Constructor by specifying the size of the array, and the default value for all the elements in the array"

so I think you should modify your constructor to be like this:

   MyArray::MyArray(int size, double def_val) {           
    n = size;
    a = new double[size];
    for (int i = 0; i < size; ++i) {
        a[i] = def_val;
    }
   }
Dharman
  • 30,962
  • 25
  • 85
  • 135
0
MyArray::MyArray(const MyArray & a2) {
    n = a2.n;
    for (int i = 0; i < n; i++) {
        a[i] = a2.a[i];
    }
} //constructor by copy

will crash when n < a2.n, you need to reallocate the array then copy elements

  • `MyArray(const MyArray & a2) { n = a2.n; a = new double [n]; for (int i=0; i< n; i++){ a[i]=a2.a[i]; } }` Like this? – CaliChzHedz Sep 16 '21 at 18:45
-2

Use the function std::iota in the numeric header in the constructor that takes an int:

MyArray::MyArray(int size) {
    n = size;
    a = new double[size];
    std::iota(a, a+size, 0.0);
}
Captain Hatteras
  • 490
  • 3
  • 11