Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6710767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:03:36+00:00 2026-05-26T08:03:36+00:00

I have a homework that asks me to do the following: Design a template

  • 0

I have a homework that asks me to do the following:

Design a template class named FlexArray which offers flexible array
indexes. The user of the class can set the lower index and upper
index when the object is declared.

Examples of user code:

FlexArray a (1,5); // lower index is 1 and the upper index is
5 FlexArray b(-5, 10); // lower index is -5 and the upper
index is 10

Provide the following functions for your class:

  1. default constructor
  2. parameterized constructor in which the user specified lower index and upper index
  3. destructor
  4. copy constructor
  5. assignment operator
  6. overloaded [ ] operator with similar semantics to [ ] already used with built-in arrays.

Errors on PRE conditions may be handled with assert statements or
try/catch blocks. There is no resizing of the array offered.
Subscripts must be in range.

The book is really no help for creating templates. I was hoping for someone to provide me with some direction on this problem and to see if my code is on the right track. I’ve tried to start this problem by the very limited scope of the book and different resources online and got:

#ifndef H_templates
#define H_templates

#include <iostream>
using namespace std;

template <typename T>           
class FlexArray
{  public:                  
    FlexArray();            // POST: empty FlexArray
    FlexArray(LI,UI);               // POST: Parameterized Constructor
    ~FlexArray();           // POST: destructor
    CopyArr(Array* sourceArray, Array* destinationArray, size);             // POST: Copies array

    //Overloading the assignment operators to add arrays(?) Unsure if
    // this is what is meant by the original question
    FlexArray operator+
      (const FlexArray& otherFlexArray) const;
      //Overload the operator +
    FlexArray operator-
      (const FlexArray& otherFlexArray) const;
      //Overload the operator -
    FlexArray operator[]
      (const FlexArray& otherFlexArray) const;
      //Overload the operator []

private:
    T FlexArray[size];      // Flex array   
    int size;               // number of items Array
    int LI;                 //Lower Index
    int UI;                 //Upper Index
};

template <typename T>  
FlexArray<T>::FlexArray ()  
// POST: empty FlexArray
{    size = 0;  }

template <typename T>  
FlexArray<T>::~FlexArray()  
// POST: destructor
{    }
template <typename T>  
FlexArray<T>::CopyArr( Array* sourceArray, Array* destinationArray, size)   
//Pre: Takes 3 arguments, the original array, the array to copy too, and, the size of array
// POST: Copies the array
{   
    for(int i=0; i<size; i++){
        sourceArray[i] = destinationArray[i]
    }
}

#endif
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:03:37+00:00Added an answer on May 26, 2026 at 8:03 am

    You’re off to a good start. A couple of things to point out.

    The assignment asks for a default constructor, BUT also states that resizing the array is not supported. These two requirements logically conflict — your assumption (to use size=0) seems logical, but then this default constructed object will always be empty. Not a huge problem, just a logical disconnect in the requirements.

    A paramaterized constructor taking the upper and lower bounds. You’ve started on this as:

    FlexArray(LI,UI);               // POST: Parameterized Constructor
    

    However, LI and UI will need types. Since you have to support negative indices, this should be a signed type, like int.

    A copy-constructor, is a constructor that takes an object of the same type. You have not declared one of these. It should have the form:

    FlexArray(const FlexArray&);
    

    The assignment operator is the = operator that allows you to do this:

    FlexArray a, b;
    b = a;
    

    You haven’t declared one of these. It should take the form:

    FlexArray& operator=(const FlexArray&);
    

    The implementation will be similar to the copy-constructor (in fact, the copy-constructor could simply be implemented in terms of the assignment operator).

    An overloaded [] operator. You have declared one of these, but it’s not really in the correct form — doesn’t take the appropriate argument types. The usage will look like this:

    FlexArray arr(-5, 10);
    
    // This is a call to operator[]
    arr[3] = value;
    

    Given that, try to think about what argument type it should take.

    Now, on to the functional requirements. Given an upper and lower bound, you have to create an array that can be indexed using those boundaries. Think about what you need to know in order to do that. I will suggest that you need to know the difference of the upper and lower bound (this will be the SIZE of your array). You should check in the constructor that the upper bound is greater than the lower bound, or you cannot effectively create this array.

    Now, to actually build your array of objects, you will need to dynamically allocate some memory for them. You have a try at this with:

    T FlexArray[size];      // Flex array
    

    But this has some problems. First off, I don’t think you can name it FlexArray as this will collide with the name of your class. Secondly, this requires that size is a compile time constant, which runs afoul of our requirements. So, you will need to dynamically allocate the internal array of T objects (using new if you haven’t learned about smart pointers yet). Remember to deallocate this array of objects in your destructor.

    Now functionally, how will the [] work? Requirement is to do bounds checking, so given an index you have to know if it is too low (outside the lower bound) or too high (outside the upper bound) and raise an appropriate error. Now you have a dynamically allocated (0-based) array of T objects — given an index that is user specified you need to find the appropriate object to return. Think about how to make that happen.

    Also, you’ve declared + and - operators, though the requirement does not specify those should be there. I would suggest taking them out. The + operator implies that the array will be resized (which is contradicted by the requirements), and the - is ambiguous, what does it mean to subtract two arrays? Both of these functions could be valid, but for this assignment are unnecessary.

    No more hints 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a homework problem that asks: Desgin a derived class GraduateStudent from base
In my homework, I have to design a class Message; among other attributes, it
A recent homework assignment I have received asks us to take expressions which could
I have a homework assignment that asks to create an order form. The order
I'm very new to XML and XSLT. I have a homework assignment that asks
I have homework that I need to make a custom string class using C
I have a homework to do that says the following: Write a program in
I have one class that declares an enumeration type as: public enum HOME_LOAN_TERMS {FIFTEEN_YEAR,
I have homework that should use fork() and wait() system calls: 1) Write two
I have a compiler homework question that wants me to draw a DFA for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.