Exercise 3: The next set of files have to do with templates and iterators. Download and try to compile the code in Lab4/

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Exercise 3: The next set of files have to do with templates and iterators. Download and try to compile the code in Lab4/

Post by answerhappygod »

Exercise 3 The Next Set Of Files Have To Do With Templates And Iterators Download And Try To Compile The Code In Lab4 1
Exercise 3 The Next Set Of Files Have To Do With Templates And Iterators Download And Try To Compile The Code In Lab4 1 (481.69 KiB) Viewed 49 times
Code Below: PLEASE USE COMMENT// WHEN NEW CODES
ADDED THANK YOU
-------------------------------------------------------------------------------------------------
//IteratorTest.cpp
#include <iostream>
using namespace std;
#include "SquareContainer2.h"
int main()
{
// You create a container like so:
SquareContainer<int> mc;
// and use it in various ways:
SquareContainer<int>::iterator ci = mc.begin();
unsigned i = 1;
while (ci != mc.end()) {
cout << "Setting element " << i << endl;
*(ci++) = i++;
}
for (SquareContainer<int>::iterator ci = mc.begin();
ci != mc.end();
++ci) {
cout << *ci << endl;
}
}
-------------------------------------------------------------------------------------------------
//SquareContainer2.cpp
#ifndef SQUARECONTAINER_H
#define SQUARECONTAINER_H
#include <cstdlib>
#include <iostream>
using namespace std;
#include "SquareContainer2.h"
// Prefix form of "next" operator
template <typename T>
typename SquareContainer<T>::iterator
SquareContainer<T>::iterator::operator++()
{
// Don't do anything if we're already past the end
if (y < theContainer->ySize) {
if (++x >= theContainer->xSize) {
x = 0;
y++;
}
}
return *this;
}
// Postfix form of "next"
template <typename T>
typename SquareContainer<T>::iterator
SquareContainer<T>::iterator::operator++(int)
{
iterator returnValue = *this;
// Don't do anything if we're already past the end
if (y < theContainer->ySize) {
if (++x >= theContainer->xSize) {
x = 0;
y++;
}
}
return returnValue;
}
// Returns the current item
template <typename T>
const T& SquareContainer<T>::iterator::operator*()
const
{
// If we're past the end, it's really an error. You could throw
an
// exception, if you like. Make sure you document the
iterator's
// behavior in these circumstances. Here, we just
cr@p out.
if (y >= theContainer->ySize) {
cerr << "Attempt to dereference bad iterator; x=" <<
x
<< ", y=" << y << endl;
exit(EXIT_FAILURE);
}
return theContainer->contents[x][y];
}
template <typename T>
bool SquareContainer<T>::iterator::operator==(const
iterator& rhs) const
{
return (theContainer == rhs.theContainer) && (x==rhs.x)
&& (y==rhs.y);
}
template <typename T>
bool SquareContainer<T>::iterator::operator!=(const
iterator& rhs) const
{
return (theContainer != rhs.theContainer) || (x != rhs.x) || (y
!= rhs.y);
}
#endif
-------------------------------------------------------------------------------------------------
//SquareContainer2.h
#pragma once
template <typename T>
class SquareContainer {
public:
class iterator; // forward declaration
friend class iterator; // friend declaration
/*!
@class iterator
This is a nested class. Note that it is in the public section
of
SquareContainer
*/
class iterator {
public:
/*!
Initializes the iterator by associating it with a
SquareContainer object, starting it at the beginning.
*/
iterator(SquareContainer<T>* c, unsigned xloc = 0,
unsigned yloc = 0) :
theContainer(c), x(xloc), y(yloc) {}
// You may need a copy constructor; we don't have it here
// iterator(const iterator& it);
// Prefix form of "next" operator
iterator operator++();
// Postfix form of "next"
iterator operator++(int);
// Returns the current item
T& operator*() const;
// Comparison operators
bool operator==(const iterator& rhs) const;
bool operator!=(const iterator& rhs) const;
private:
/*! Keep track of where we are in theContainer */
unsigned x, y;
/*! The SquareContainer object this iterator is associated with
*/
SquareContainer<T>* theContainer;
};
//Returns iterator that refers to first item, using iterator
constructor
iterator begin(void) { return iterator(this); }
const iterator end(void) { return iterator(this, 0, ySize);
}
private:
static const unsigned xSize = 5;
static const unsigned ySize = 5;
T contents[xSize][ySize];
};
// Remember, we can't compile template classes separately. How
can we fix this?
Exercise 3: The next set of files have to do with templates and iterators. Download and try to compile the code in Lab4/Exercise3 (SquareContainer2.h, SquareContainer2.cpp, and IteratorTest.cpp) and consider the following activities (30 minutes coding): 1. Question: Look at the code in IteratorTest.cpp. What do you think it does? The program doesn't currently compile. What do you need to change to make it compile? Fix it and run the program. Does it do what you think it would? Put your answers in AnswersToLab4.txt. O Take Away: Reminder of how to template class compilation, understanding of code. 2. Question: Why do the iterator increment operators return an iterator? How do the two increment operators differ? Is the iterator they return allocated dynamically, auto, or something else? Put your answer in AnswersToLab4.txt. O Take Away: What does it mean to return a reference to self (in this case, and, in general, remember, overloaded operators typically return self). 3. Interactive Coding and Question: Change SquareContainer::iterator::operator*() so that it is a const method. What is this supposed to mean? When you try to compile and run the program, what happens? Does this appear to be consistent with the declaration of that operator as const? Why or why not? In your opinion, should that operator be declared const? Put your answer in AnswersToLab4.txt. Take Away: Understanding of "const", when to use, and why to (or not to use. O
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply