Consider the following template class:
template <typename T>
class ArrayOfTen
{
public:
ArrayOfTen();
~ArrayOfTen();
void insert(T);
void printAll();
private:
T * array;
int size;
}
a. When the array is first created, it should allocate memory for
10 items. Size should
be zero. Write the constructor:
b. When ArrayOfTen::insert(T) is called, the new item should be
added to the array,
and size should increase by one. If the array already has 10 items,
then it should
throw an exception. Write the ArrayOfTen::insert(T) method.
c. When ArrayOfTen::printAll() is called, all items in the array
should be printed.
Write ArrayOfTen::printAll().
d. When the destructor is called, memory should be deallocated.
Write the
destructor.
#include<iostream>
using namespace std;
template <typename T>
class ArrayOfTen
{
public:
ArrayOfTen();
~ArrayOfTen();
void insert(T);
void printAll();
private:
T * array;
int size;
};
//a
template <typename T>
ArrayOfTen<T>::ArrayOfTen()
{
array = new T[10];//creating memory for 10 items
size=0;//setting size to 0
}
//b
template <typename T>
void ArrayOfTen<T>::insert(T t)
{
if(size+1<10)
{
array[size]=t;//inserting
item
size++;///increasing size
}
else//if list already has 10 items, then thrown
exception
throw "list is full";
}
//c
template <typename T>
void ArrayOfTen<T>::printAll()
{
//printing all items
for(int i=0;i<=size;i++)
cout<<array[i]<<" ";
cout<<endl;
}
//d
template <typename T>
ArrayOfTen<T>::~ArrayOfTen()
{
delete array;//freeing memory
}
int main()
{
return 0;
}
Consider the following template class: template <typename T> class ArrayOfTen { public: ArrayOfTen(); ~ArrayOfTen(); void insert(T);...
#ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO:: default constructor perform new to reserve memory of size INITIAL_CAPACITY. set num_items = 0; */ heap() { // write your code here }; /** Create a heap from a given array */ heap(dtype *other, size_t len) { // write your code here }; /** copy constructor copy another heap to this heap. */ heap(const heap<dtype> &other) { //write your code here }; /** Accessor...
#include <iostream> using namespace std; template <typename Item> class MyArray{ private: Item *myarray; int size; int used; void doubleSize(); public: MyArray(); ~MyArray(); int length(); void insertHead(Item i); void insertTail(Item i); void deleteHead(); void deleteTail(); void sortAscending(); void sortDescending(); Item operator [](int i){ return myarray[i]; } }; template <typename Item> MyArray<Item>::MyArray(){ size = 5; used = 0; myarray = new Item[size];...
//C++ program #include<iostream> using namespace std; template<typename T> class list{ private: T*arr; int size; int capacity; void resize(){ capacity*=2; T * newArr = new T[capacity]; for(int i=0;i<size;i++){ newArr[i] = arr[i]; } delete(arr); arr = newArr; } public: ...
NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a template class to represent a matrix. Because it’s a template, your matrix class will be able to work with different types of underlying data. Start by creating a new file matrix.hpp. Inside this file, start to write your matrix template class. Here’s a start for the class definition: template <class T> class Matrix { private: int _rows; int _cols; T** data; public: Matrix(int rows,...
Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...
Review the Stack implementation with Vector, and
implement/answer the following methods.
Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...
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...
Please use template <typename T>. This is a C++
Data Structures Question.
Problem 5. (20 pts total) Write the erase functions for the Mtree class that deletes every node in the Mtree. Note there should be two erase functions. The irst is in the e second is a recursive function in the private section that is called by the function in the public ar Root.) ree-
In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....
template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...