What will be the output of the following C++ code?
#include <iostream>
#include <vector>
#include <iterator>
#include <stddef.h>
using namespace std;
template<class myType>
class SimpleContainer
{
public:
SimpleContainer(size_t xDim, size_t yDim, myType const& defaultValue)
: objectData(xDim * yDim, defaultValue)
, xSize(xDim)
, ySize(yDim)
{
}
myType& operator()(size_t x, size_t y)
{
return objectData[y * xSize + x];
}
myType const& operator()(size_t x, size_t y) const
{
return objectData[y * xSize + x];
}
int getSize()
{
return objectData.size();
}
void inputEntireVector(vector<myType> inputVector)
{
objectData.swap(inputVector);
}
void printContainer(ostream& stream)
{
copy(objectData.begin(), objectData.end(),
ostream_iterator<myType>(stream, ""/*No Space*/));
}
private:
vector<myType> objectData;
size_t xSize;
size_t ySize;
};
template<class myType>
inline ostream& operator<<(ostream& stream, SimpleContainer<myType>& object)
{
object.printContainer(stream);
return stream;
}
void sampleContainerInterfacing();
int main()
{
sampleContainerInterfacing();
return 0;
}
void sampleContainerInterfacing()
{
static int const ConsoleWidth = 80;
static int const ConsoleHeight = 25;
size_t width = ConsoleWidth;
size_t height = ConsoleHeight;
SimpleContainer<int> mySimpleContainer(width, height, 0);
cout << mySimpleContainer.getSize() << endl;
mySimpleContainer(0, 0) = 5;
}
#include <iostream>
#include <vector>
#include <iterator>
#include <stddef.h>
using namespace std;
template<class myType>
class SimpleContainer
{
public:
SimpleContainer(size_t xDim, size_t yDim, myType const& defaultValue)
: objectData(xDim * yDim, defaultValue)
, xSize(xDim)
, ySize(yDim)
{
}
myType& operator()(size_t x, size_t y)
{
return objectData[y * xSize + x];
}
myType const& operator()(size_t x, size_t y) const
{
return objectData[y * xSize + x];
}
int getSize()
{
return objectData.size();
}
void inputEntireVector(vector<myType> inputVector)
{
objectData.swap(inputVector);
}
void printContainer(ostream& stream)
{
copy(objectData.begin(), objectData.end(),
ostream_iterator<myType>(stream, ""/*No Space*/));
}
private:
vector<myType> objectData;
size_t xSize;
size_t ySize;
};
template<class myType>
inline ostream& operator<<(ostream& stream, SimpleContainer<myType>& object)
{
object.printContainer(stream);
return stream;
}
void sampleContainerInterfacing();
int main()
{
sampleContainerInterfacing();
return 0;
}
void sampleContainerInterfacing()
{
static int const ConsoleWidth = 80;
static int const ConsoleHeight = 25;
size_t width = ConsoleWidth;
size_t height = ConsoleHeight;
SimpleContainer<int> mySimpleContainer(width, height, 0);
cout << mySimpleContainer.getSize() << endl;
mySimpleContainer(0, 0) = 5;
}A. 2000
B. No Space
C. Error
D. Depends on the compiler
Answer: Option D
What does the 'sizeof' operator return in C++?
A. Size of a data type in bits
B. Size of a data type in bytes
C. Size of a variable in bytes
D. Size of a variable in bits
What is the purpose of the 'static' keyword in C++?
A. To declare a variable with dynamic storage duration
B. To declare a constant
C. To declare a variable with external linkage
D. To declare a variable with static storage duration
What is the difference between '++i' and 'i++' in C++?
A. None of the above
B. They both have the same effect
C. '++i' increments the value of 'i' before returning it, while 'i++' increments the value of 'i' after returning it
D. '++i' increments the value of 'i' after returning it, while 'i++' increments the value of 'i' before returning it

Join The Discussion