Question

you will write a templated array class. Called MyArray with member variables ptr and array_size. This...

you will write a templated array class. Called MyArray with member variables ptr and array_size. This array must use dynamic memory--see program shell, names should remain unchanged.

The following is a list of required member functions of the array class along with a rubric:

(1pts) program compiles

(2pts) default constructor

(2pts) parametered constructor which feeds MyArray a size.

(10pts) copy constructor

    8pts for performing a *deep* copy

    2pts for correct syntax

(10pts) overloaded = operator

     7pts for functioning properly

     3pts for being capable of performing a chained assignment

(10pts) overloaded [] operator--a mostly normal [] operator, but one that performs bounds checking****.

     5pts for performing bounds checking (graded by hand)

     5pts for functioning properly

(5pts) Write a child class for the above templated class called SmallArray--Your child class doesn't have to do anything, just write a default and parametered constructor. Write the parametered constructor in such a way that it passes its arguments to the parent.

     1pts default constructor

     2pts parametered constructor

     2pts proper inheritance syntax

#ifndef MY_ARRAY_H
#define MY_ARRAY_H

template <class T>
class MyArray{
private:
T* ptr;
int array_size;
  
public:
  

T& operator[](int index) const;//do not modify
MyArray<T> operator=(MyArray<T>);//do not modify

int size(){
return array_size;
}
  
//These exist for my unit tests, do not use them in any of your member functions
//but feel free to use them to test your code.
T getElement(int index) const{
return ptr[index];
}

void setElement(T value, int index){
ptr[index] = value;
}
  
};


//test function, do not modify
template <typename T>
MyArray<T> copy_test_2(MyArray<T> temp_val){
MyArray<T> temp(temp_val);
return temp;
}


#endif

No changes in main.ccp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

screenshot

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

Program

MyArray.h

#ifndef MY_ARRAY_H
#define MY_ARRAY_H
//Template
template <class T>
class MyArray {
//Instance variables
private:
   T* ptr;
   int array_size;
//Member functions
public:
   //Default constructor
   MyArray<T>() {
       array_size = 0;
       ptr = new T[array_size];
   }
   //Parameterized constructor
   MyArray<T>(int sz) {
       array_size = sz;
       ptr = new T[array_size];
   }
   //Copy constructor
   MyArray<T>(const MyArray<T>& myarray) {
       array_size = myarray.array_size;
       ptr = new int[array_size];
       for (int i = 0; i < array_size; i++) {
           ptr[i] = myarray.ptr[i];
       }
   }
   //Overload index operator
   T& operator[](int index) const;//do not modify
   //Overload equals operator
   MyArray<T> operator=(MyArray<T>);//do not modify
   //Get size of the array
   int size() {
       return array_size;
   }

   //These exist for my unit tests, do not use them in any of your member functions
   //but feel free to use them to test your code.
   T getElement(int index) const {
       return ptr[index];
   }

   void setElement(T value, int index) {
       ptr[index] = value;
   }

};


//test function, do not modify
template <typename T>
MyArray<T> copy_test_2(MyArray<T> temp_val) {
   MyArray<T> temp(temp_val);
   return temp;
}


#endif

MyArray.cpp

#include "MyArray.h"
#include<iostream>
using namespace std;
//Overload index operator
template <class T>
T& MyArray<T>::operator[](int index) const {
   if (index < 0 || index >= array_size) {
       cout << "\nError:Index " << index << " out of range" << endl;
       exit(1);
   }
   return ptr[index];
}
//Overload equals operator
template <class T>
MyArray<T> MyArray<T>::operator=(MyArray<T> myarray) {
   if (&myarray != this) {
       if (array_size != myarray.array_size) {
           delete[] ptr;
           array_size = myarray.array_size;
           ptr = new int[array_size];
       }
       for (int i = 0; i < array_size; i++) {
           ptr[i] =myarray.ptr[i];
       }
   }
   return *this;
}

SmallArray.h

#include "MyArray.h"
template <class T>
class SmallArray :public MyArray<T> {
   //default constructor
   SmallArray<T>() {

   }
   //Parameterized constructor
   SmallArray<T>(int sz) : MyArray(sz) {

   }
};

main.cpp

#include <iostream>
#include "MyArray.h"
using namespace std;
int main()
{
   //Create an object
   MyArray<int> myarray(3);
   //Display size
   cout << "Size of myarray: " << myarray.size() << endl;
   //Create a copy
   MyArray<int> myarray2=copy_test_2(myarray);
   //Display size
   cout << "Size of the copied array: "<<myarray2.size() << endl;
}

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

output

Size of myarray: 3
Size of the copied array: 3

Add a comment
Know the answer?
Add Answer to:
you will write a templated array class. Called MyArray with member variables ptr and array_size. This...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called Book that has two private member variables called page (integer) and topic...

    Write a class called Book that has two private member variables called page (integer) and topic (string). It also has a static private member variable called count (integer). This class has only one constructor with default argument for page and topic. Write this constructor. Also overload the addition operator for this class such that it will add an integer value to the page variable of the book. For example when in main we say: book2 = book1 + 4; then...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • Write a templated function sumList that will take in an array of any type and the...

    Write a templated function sumList that will take in an array of any type and the length of the array. It should add all the elements in the array and return the total. Hint: you can declare a variable sum of type T and initialize it like this: T sum {}; The {} are the best way to make sure that numbers get set to 0, a string gets created as empty etc... #include <iostream> using namespace std; //Do not...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • IN C++ Create a class to act as a generic array (i.e. the user will be...

    IN C++ Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • Write a C++ program Write a class named Employee that has the following member variables: name...

    Write a C++ program Write a class named Employee that has the following member variables: name A string that holds the employee name idNumber An int to hold employee id number department A string to hold the name of the department position A string to hold the job position The class will have three constructors: 1. A constructor that accepts all the internal data such as: Employee susan("Susan Meyers", 47899, "Accounting", "Vice President"); 2. A constructor that accepts some of...

  • QUESTION 1 Using the following declarations and a member of the Array class, int [ ]...

    QUESTION 1 Using the following declarations and a member of the Array class, int [ ] bArray = new int [10]; int location; Using a method in the Array class, write a statement that would change the order of the elements in the bArray array. The contents of the first cell should hold what was in the last cell. The second cell should hold what was in the next to last cell. __________________________ HINT: You must write the full statement...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT