Question

In: Computer Science

Answer the following questions: What is the purpose of #ifndef NO_CHECK in operator[]? Why does void...

Answer the following questions:

  1. What is the purpose of #ifndef NO_CHECK in operator[]?
  2. Why does void push_back(Object&& x) use std::move(x) while void push_back(const Object& x) simply uses objects[theSize++] = x;?
  3. What does return &objects[0]; do?

code:

#ifndef VECTOR_H
#define VECTOR_H

#include <algorithm>
#include <iostream>
#include <stdexcept>
#include "dsexceptions.h"

template<typename Object>
class Vector {
public:
  explicit Vector(int initSize = 0)
      : theSize{initSize},
        theCapacity{initSize + SPARE_CAPACITY} { objects = new Object[theCapacity]; }

  Vector(const Vector &rhs)
      : theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, objects{nullptr} {
    objects = new Object[theCapacity];
    for (int k = 0; k < theSize; ++k)
      objects[k] = rhs.objects[k];
  }

  Vector &operator=(const Vector &rhs) {
    Vector copy = rhs;
    std::swap(*this, copy);
    return *this;
  }

  ~Vector() { delete[] objects; }

  Vector(Vector &&rhs)
      : theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, objects{rhs.objects} {
    rhs.objects = nullptr;
    rhs.theSize = 0;
    rhs.theCapacity = 0;
  }

  Vector &operator=(Vector &&rhs) {
    std::swap(theSize, rhs.theSize);
    std::swap(theCapacity, rhs.theCapacity);
    std::swap(objects, rhs.objects);

    return *this;
  }

  bool empty() const { return size() == 0; }

  int size() const { return theSize; }

  int capacity() const { return theCapacity; }

  Object &operator[](int index) {
#ifndef NO_CHECK
    if (index < 0 || index >= size())
      throw ArrayIndexOutOfBoundsException{};
#endif
    return objects[index];
  }

  const Object &operator[](int index) const {
#ifndef NO_CHECK
    if (index < 0 || index >= size())
      throw ArrayIndexOutOfBoundsException{};
#endif
    return objects[index];
  }

  void resize(int newSize) {
    // Change the size of the vector (i.e., teh value returned by the method size).
    // If newSize > capacity, then the new capacity should be newSize * 2.

    // Place your code here.  
  }

  void reserve(int newCapacity) {
    // Increase the capacity of the vector.  Do not change the size.

    // Place your code here.  
  }

  // Stacky stuff
  void push_back(const Object &x) {
    if (theSize == theCapacity)
      reserve(2 * theCapacity + 1);
    objects[theSize++] = x;
  }

  // Stacky stuff
  void push_back(Object &&x) {
    if (theSize == theCapacity)
      reserve(2 * theCapacity + 1);
    objects[theSize++] = std::move(x);
  }

  void pop_back() {
    if (empty())
      throw UnderflowException{};
    --theSize;
  }

  const Object &back() const {
    if (empty())
      throw UnderflowException{};
    return objects[theSize - 1];
  }

  // Iterator stuff: not bounds checked
  typedef Object *iterator;
  typedef const Object *const_iterator;

  iterator begin() { return &objects[0]; }

  const_iterator begin() const { return &objects[0]; }

  iterator end() { return &objects[size()]; }

  const_iterator end() const { return &objects[size()]; }

  iterator insert(iterator iter, const Object &x) {
    // Insert x **before** iter.
    // Make sure your algorithm is O(N).

    // Place your code here.  
    return iter;
  }

  iterator erase(iterator from, iterator to) {
    // Remove all the elements between from and to (including from.  *not* including to).
    // Make sure your algorithm is O(N).

    // Place your code here.  
    return to;
  }

  static const int SPARE_CAPACITY = 2;

private:
  int theSize;
  int theCapacity;
  Object *objects;
};

#endif

Solutions

Expert Solution

A. Purpose of # ifndef NO_CHECK :

Usually, ifndef is used ,it will confirm if there is no previous definitions of the given function /macro. Here, compiler first checks if there is any definition of NO_CHECK before that point of time. One other option is user might give it at the time of compilation. If is not specified in either of the cases, the code following the ifndef will be executed.

B. void push_back(Object&& x) use std::move(x) while void push_back(const Object& x) simply uses objects[theSize++] = x; :

Let us first understand that both the above functions work the same way .They both will add to the end of vector's array.

But the function push_back(Object&& x) will use the rvalue concept. Let me explain.

objects[theSize++] = x;

In the above statement, a copy of x is assigned to objects[theSize++]. whereas std::move will 'move' the value of x. In simple terms, std::move is like CUT and the other one is like COPY in Ms-Word. We can explain this in two ways.

1.The expression x is not a rvalue. x is a lvalue of type "rvalue reference" .

2.We also know "if it has a name, it is lvalue" rule. so as x has a name , it has to be changed to rvalue by using std::move. Thus we can move it instead of copying it to the destination.

C. return &objects[0] will return the reference (address) of the object[0] . That is the address of the first element of the objects array. Note that objects is a pointer of type Object. So, objects[0] would also be a pointer. when we write &objects[0], we are returning the reference to the beginning pointer.


Related Solutions

Chapter 9 (Pointers) – Review Questions Short Answer 1.     What does the indirection operator do? 2.    ...
Chapter 9 (Pointers) – Review Questions Short Answer 1.     What does the indirection operator do? 2.     Look at the following code. int x = 7; int *ptr = &x; What will be displayed if you send the expression *ptr to cout? What happens if you send the expression ptr to cout? 3.     So far you have learned three different uses for the * operator. What are they? 4.     What math operations are allowed on pointers? 5.     Assuming ptr is a...
Answer the questions given in the two options below. a) Why is the financial purpose of...
Answer the questions given in the two options below. a) Why is the financial purpose of the firm determined as "firm value maximization" instead of "making profit"? Discuss. b) Why is a financial decision taken from the concept of "cash flow" rather than profit? Discuss.
Discuss the following questions: What is the purpose of technical analysis, and why are those who...
Discuss the following questions: What is the purpose of technical analysis, and why are those who use technical analysis referred to as chartists? What is a moving average? What is the significance when a stock’s price crosses a moving average of the stock’s price? What is the difference between “support” and “resistance” in technical analysis?
Please answer the following questions: 1.What does it mean “Fiat Money” and why did the modern...
Please answer the following questions: 1.What does it mean “Fiat Money” and why did the modern economies move to this payments system? 2.Calculate the total increase in deposit of a bank in the following case: Initial deposit: $ 2,000 RRR: 20 percent 3. Which component of the Federal Reserve System decides on the reserve requirement and what will be the effect of its reduction? Please answer them properly and Write in PRINT!
What is the purpose of meiosis? Where does it occur, what is produced, why does it...
What is the purpose of meiosis? Where does it occur, what is produced, why does it occur or what are the significances and what are the consequences if it does not occur?
Answer the following questions. a. What does it mean to say that b is a divisor...
Answer the following questions. a. What does it mean to say that b is a divisor of a? b. What is the meaning of the expression a divides b? c. What is the Congruent Modulo n ? Support your answer with one example of Congruent Modulo n. d. What is a prime number? Support your answer with one example of two integers are relatively prime. e. What is the difference between active and passive attacks?
What does the void keyword mean?
What does the void keyword mean?
Answer the following questions:       1. What is an audit trail and why is it important in...
Answer the following questions:       1. What is an audit trail and why is it important in accounting?       2. There are several types of corporations. If you were to start your own business and incorporate; discuss what type of corporation you would want to be.             Explain why.
Answer the following short answer questions (each can be answered in half a page): What does...
Answer the following short answer questions (each can be answered in half a page): What does the term "risk pooling" mean in the context of health insurance? How does risk pooling reduce the problem of health care costs varying widely between people? How does Adverse Selection interfere with risk pooling?
What is the purpose of ”what if” questions?
What is the purpose of ”what if” questions?- The what if questions can be used to put focus on the final product- The what if questions are used to challenge the consumer- The what if questions should help us to break out from the limitations- To make sure that we don’t forget anything vital
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT