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

Array.h #ifndef ARRAY_H_ #define ARRAY_H_ // Your TEMPLATED Array class declarat

ID: 3915204 • Letter: A

Question

Array.h

#ifndef ARRAY_H_
#define ARRAY_H_

// Your TEMPLATED Array class declaration goes here:

// Your TEMPLATED Array class implementations for methods (1) and (2) (parametrized ctor and assignment operator) go here:

#endif //ARRAY_H_

Lab9.cpp

#include <iostream>

#include "Array.h"

using namespace std;


int main()
{
// Create an Array object that works with <int> Type by invoking the parametrized ctor of the templated class
// At this time the compiler:
// a) first generates the required class declaration and method implementation out of the provided template in Array.h
//    creates a class Array where T is int
// b) after it has generated this code, it actually invokes the requested method (the parametrized ctor)

Array<int> intArray_0(10, -10); //10 elements, all with value -10

// Create another Array object that works with <int> Type. The compiler has already generated the class Array<int> so it just invokes the ctor method

Array<int> intArray_1(20, 1000); //20 elements, all with value 1000

// Call the assignment opreator of the Array class templated for <int> Type.
// The compiler understands that the left-hand side argument (intArray_0) is of Array<int> type, and the right-hand side argument (intArray_1) is also of Array<int> type.
// Therefore it knows to call the assignment operator of the Array<int> class which it generated before.
intArray_0 = intArray_1;


// Now, create an Array object that works with <double> Type by invoking the parametrized ctor of the templated class
// The compiler will now generate another SEPARATE class Array<double> and its required methods and act in the same way as described above
  
Array<double> doubleArray_0(10, -0.1); //10 elements, all with value -0.1

Array<double> doubleArray_1(20, 0.001); //20 elements, all with value 0.001

doubleArray_0 = doubleArray_1;

return 0;
}

Makefile

TARGET = lab9
LIBS = -lm #Math Library, just a placeholder
HEADERS = Array.h
SRCS = lab9.cpp
OBJECTS := $(patsubst %.cpp,%.o,$(SRCS))
CXX = g++
CXX_FLAGS = -Wall -std=c++11 #C++11 just for reference, not necessary

.PHONY: default all clean

all: depend $(TARGET)

#Rules to recompile template headers when they change
depend: .depend
.depend: $(HEADERS)
rm -f ./.depend
$(CXX) $(CXX_FLAGS) -MM $^ > ./.depend;
include .depend

%.o: %.cpp $(HEADERS)
$(CXX) $(CXX_FLAGS) -c $< -o $@

$(TARGET): $(OBJECTS)
$(CXX) $(CXX_FLAGS) $(OBJECTS) $(LIBS) -o $@

clean:
-rm -f *.o
-rm -f ./.depend
-rm -f $(TARGET)

Explanation / Answer

Given below is the completed code for the question.
Please do rate the answer if it was helpful. Thank you

Array.h
--------

#ifndef ARRAY_H_
#define ARRAY_H_

// Your TEMPLATED Array class declaration goes here:
template <typename T>
class Array{
public:
Array(size_t size, const T &val);
Array<T>& operator =(const Array<T> &rhs);
private:
T *m_buffer;
size_t m_size;
};


// Your TEMPLATED Array class implementations for methods (1) and (2) (parametrized ctor and assignment operator) go here:
template <typename T>
Array<T>::Array(size_t size, const T &val){
m_buffer = new T[size];
m_size = size;
for(int i = 0; i < m_size; i++)
m_buffer[i] = val;
}

template <typename T>
Array<T>& Array<T>::operator =(const Array<T> &rhs){
if(this != &rhs){
delete []m_buffer;
m_size = rhs.m_size;
m_buffer = new T[m_size];
for(int i = 0; i < m_size; i++)
m_buffer[i] = rhs.m_buffer[i];
}
return *this;
}
#endif //ARRAY_H_

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