In this problem set, we define a simple integer vector class, called IntVector
Ask Expert

Be Prepared For The Toughest Questions

Practice Problems

In this problem set, we define a simple integer vector class, called IntVector

Problem Set 2: Indexers, Method Overriding, and Lambdas

Problem 1

In this problem set, we define a simple integer vector class, called IntVector, that provides us with a container type for integer arrays. The class IntVector shares some similarities with the standard vector class std::vector, but we only define those features here that would allow us to practice indexers, method overriding, and lambda expression (being used latter to implement basic sorting algorithms).

The class IntVector defines the following interface:

#pragma once

// include for size_t (unsigned integral type)

#include <cstddef>

class IntVector

{

private:

int * fElements;

size_t fNumberOfElements;

public:

// Constructor: copy argument array

IntVector( const int aArrayOfIntegers[], size_t aNumberOfElements );

// Destructor: release memory

// Destructor is virtual to allow inheritance

virtual ~IntVector();

// size getter size_t size() const;

// element getter

const int get( size_t aIndex ) const;

// swap two elements within the vector

void swap( size_t aSourceIndex, size_t aTargetIndex );

// indexer

const int operator[]( size_t aIndex ) const;

};

The class IntVector may be extended by inheritance. For this reason, the destructor

~IntVector() is marked virtual. This will allow for a proper dynamic method being of the destructor to prevent memory leaks.

The class IntVector just defines a wrapper for an array of integers. The wrapper adds, however, range checks so that index errors can be caught. The class does not expose the underlying array to clients or subclasses. Access to elements is read only. Nevertheless, we can change to order of elements via method swap(). 

The constructor for IntVector takes an integer array and its size as parameters. The constructor copies this array as shown below:

IntVector::IntVector( const int aArrayOfIntegers[], size_t aNumberOfElements )

{

fNumberOfElements = aNumberOfElements; fElements = new int[fNumberOfElements];


for ( size_t i = 0; i < fNumberOfElements; i++ )

{

fElements[i] = aArrayOfIntegers[i];

}

}

The destructor has to free the allocated memory and the member function size() has to return the number of elements in the array.

The member function swap() takes two indices and, if they are within range, swaps the corresponding array elements in an IntVector object. We need swap() for sorting.

The indexer operator[] and the method get() both return the value that corresponds to aIndex, if this is possible. Please note that both return a read-only value copy by design. This has not impact on performance, but requires us to use member function swap() when we wish to exchange array elements.

You should implement method get() using operator[]. This approach requires you to refer to “this object” explicitly. In C++, we write *this to mean “this object.” You need to enclose *this is parentheses, that is, (*this), to avoid any issues with operator priority.

You can use the test driver in Main.cpp (available on Canvas) to test your implementation. Please uncomment #define P1 for this purpose. Running the program should produce the following output:


The test driver uses exception handling in order to verify range checks. The two error messages are expected here. No other error message should appear in the output though.

Dataset

Hint
ComputerC++ is basically a general-purpose programming language. It was created as an extension of the C programming language, or the C with Classes. Also, the language has expanded significantly over the time, and modern C++ also has object-oriented, generic, and several functional features in addition to facilities for the low-level memory manipulation....

Know the process

Students succeed in their courses by connecting and communicating with
an expert until they receive help on their questions

1
img

Submit Question

Post project within your desired price and deadline.

2
img

Tutor Is Assigned

A quality expert with the ability to solve your project will be assigned.

3
img

Receive Help

Check order history for updates. An email as a notification will be sent.

img
Unable to find what you’re looking for?

Consult our trusted tutors.

Developed by Versioning Solutions.