Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer the following questions based on class A\'s definition: c. Implement the

ID: 3797400 • Letter: A

Question

Answer the following questions based on class A's definition: c. Implement the ++ (only the prefix increment operator). This operator should increment every element in the array by one. d. Implement the class destructor. c. What will be the output of the main function: int main() {A ob1(4); A ob2(ob1); ++ob1; ob1.display(); ob2.display(); return theta;} f. Implement the copy constructor. #include using namespace std; class A {private: int* numbers; int size; public: A(int); void display();}; Az zA(int s) {size = s; numbers - new int[size]; for (int i = 0; i

Explanation / Answer

At first I am writing the complete code after all the things implemented.

Source Code:

#include<iostream>

using namespace std;

class A
{
private:
int* numbers;
int size;

public:

A(int);
void display();

void operator ++();

A(const A &);

~A();

};

A::A(int s)
{
size = s;
numbers = new int[size];
for(int i=0; i< size; ++i)
numbers[i] = i*size;
}

void A::display()
{
for(int i=0; i<size; ++i)
cout << numbers[i] << endl;
}

void A::operator++()
{
for(int i=0; i< size; ++i)
++numbers[i];
}
A::A(const A &obj)
{
size = obj.size;
numbers = new int[size];

for(int i=0; i<size; ++i)
{
numbers[i] = obj.numbers[i];
}
}

A::~A()
{
delete[] numbers;

numbers = NULL;

size = 0;
}

int main()
{
A ob1(4);
A ob2(ob1);
++ob1;
ob1.display();
ob2.display();
return 0;
}

Explanation:

c).

Here, you need to overload the ++ operator. It is unary operator. And whenever we overload unary operator, we don't need to pass any argument as it has 1 argument 'this' pointer which points to the object which has called the method. So, size variable is also present in object and we need to increment every element of numbers[ ] array by 1. So, by using a for() loop, I have done the same. The snippet is included in above code. I am just writing for reference again:

void A::operator++()
{
for(int i=0; i< size; ++i)
++numbers[i];
}

---------------------------------------------------------------

d). Here, the class contains two member variables. So, we need to delete everything. Moreover, the numbers has dynamically allocated the memory that we need to clear first. So, using delete[ ] numbers makes sure that all the memory space (it is an array after all so it will be in contiguous memory locations so it can remove everything at one time) is removed that was allocated using *numbers i.e. numbers[i] to numbers[size-1]. After that I de-referenced numbers to NULL and size to 0. This is done for removing everything completely before the object gets destroyed so that the memory can be used again. The code snippet for the same is:


A::~A()
{
delete[] numbers;

numbers = NULL;

size = 0;
}

---------------------------------------------------------------

e). Output related:

Here, first ob1 is created with size 4. So, the numbers[ ] is created by for loop using values i*size where i=0 to 3(size-1) and size=4

i.e. 0*4 = 0

1*4 = 4

2*4 = 8

3*4 = 12

ob2 is created with copy constructor that copies the ob1 values. So, ob1 had size = 4 which will be copied in ob2.size. And the numbers[ ] will have the same values as above 0, 4, 8, 12 as the ob1 values get copied there.

Now, we have called ++ob1; and inside the prefix increment operator, every element is incremented by 1. So, ob1 values of numbers[ ] becomes 1,5,9, and 13.

Now simply ob1.display() prints 1,5,9,13 and ob2.display() prints 0,4,8,12 each on the new line. So, the final output is:

1

5

9

13

0

4

8

12

---------------------------------------------------------------

f). The copy constructor code here is :

A::A(const A &obj)
{
size = obj.size;
numbers = new int[size]; // if not created using new and directly writing following loop gives run-time error segmentation fault.

for(int i=0; i<size; ++i)
{
numbers[i] = obj.numbers[i];
}
}

The syntax is fix for every copy constructor. You need to pass the object of the same class as constant & so that its address is passed there. ( as per function prototype written above)

Here, inside the copy constructor, the given objects every variable values are supposed to be copied in the object which invoked it.

So, here the size is assigned obj.size value where obj is the object of same class and already created.

To copy the numbers, we need to create the array numbers[ ] dynamically because it was earlier created in other object's parameterized constructor. Otherwise, if we have not allocated the dynamic memory, how can we store the values in it? So, we create the array dynamically and simply assign the obj.numbers array's every value to numbers[ ] using for() loop. Here, size means this.size and numbers means this.numbers. So, there is no need to give reference to destination object as it is maintained itself using 'this'. So, this how every elements of numbers[ ] is also copied and so the whole object is copied into newer one.

Do comment if there is any query. Thank you. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote