HW2
Please complete the coding exercises below, and submit a .zip file of your source code on D2L.
Q1. Class construction (7 points)
During the 'Pointers' lecture, we created and used a 'float **xtable' 2D array, like so:
int xRes=512, yRes=256;
float **xtable; // pointer to pointer[s]
xtable = new float*[yRes]; // a ("column") array to hold 'float *' type pointers
// each element of the above array points to a 'row' of xRes elements
for(int i=0;i < yRes;i++) {
xtable[i] = new float[xRes];
}
for(int i=0;i < yRes;i++){
for(int j=0;j < xRes;j++){
xtable[i][j]=45; // store 45 for pixel data, "for now"
}
}
Create an 'array2D' class that encapsulates the above. Your class should contain the following 'protected' members ["protected members are accessible in the class that defines them, AND in classes that inherit from it"]:
• xtable
• xRes
• yRes
It should also contain the following public interface:
• constructor: array2D(xResolution,yResolution)
• getSize(int &xResolution, int &yResolution) ['&' because we want those vars to be SET inside the function, like a 'return' in a way; confusingly, int xResolution&, int yResolution& means the same thing!]
• setValue(x,y,val) [at (x,y), store val (in xtable, y would be the rowID, x, the columnID)]
• val=getValue(x,y) [at (x,y), retrieve val]
Remember to create a destructor function as well :)
Exercise your array2D class as follows, inside main():
array2D *a = new array2D(320,240);
int xRes, yRes;
a->getSize(xRes,yRes);
//NOTE the j,i ordering below (twice), since we specify coords as (x,y) [not (y,x)]
for(int i=0;i < yRes;i++){
for(int j=0;j < xRes;j++){
a->setValue(j,i,100); // constant value of 100 at all locations
}
}
for(int i=0;i < yRes;i++){
for(int j=0;j < xRes;j++){
cout << a->getValue(j,i) << " ";
}
cout << endl;
}
delete a;
All the code needs to be in a single Q1.cpp file.
Q2. Class inheritance (8 points)
Extend (inherit from) the array2D class from Q1 above, to create a 'PGMImage' class.
class PGMImage: public array2D {
// private:
// ...
// protected:
// ...
// public:
// ...
};//PGMImage
PGMImage 'inherits' [gets handed down] all the public and protected members and methods of array2D.
PGMImage should contain this additional non-public (private or protected) member:
• string filename (or char filename[2048])
It should contain the following public methods:
• PGMImage(xResolution,yResolution,imageFilename)
• getResolution(xResolution&,yResolution&)
• setPixel(x,y,val) [at (x,y), store val]
• val=getPixel(x,y) [at (x,y), retrieve val]
• writeFile()
In main(), write code similar to that for Q1, with 'test.pgm' as the output filename. Your code would output a test.pgm file, which should be loadable via the netpbm viewer, Irfanview, etc.
Place BOTH class declarations (array2D, PGMImage) in Q2.h, #include "Q2.h" in Q2.cpp, and in Q2.cpp, provide both class definitions plus main()