1. Homepage
  2. Programming
  3. [2022] USC - CS455: Introduction to Programming - HW2 C++ Pointers, Class Inheritance and Image Synthesis

[2022] USC - CS455: Introduction to Programming - HW2 C++ Pointers, Class Inheritance and Image Synthesis

Engage in a Conversation
CS455Introduction to ProgrammingUSCC++InheritancePointers

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: CourseNana.COM


    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(): CourseNana.COM


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. CourseNana.COM

Q2. Class inheritance (8 points)

Extend (inherit from) the array2D class from Q1 above, to create a 'PGMImage' class. CourseNana.COM


class PGMImage: public array2D {
   // private:
   // ...
   // protected:
   // ...
   // public:
   // ... 
};//PGMImage 

PGMImage 'inherits' [gets handed down] all the public and protected members and methods of array2D. CourseNana.COM

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() CourseNana.COM


CourseNana.COM


CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
CS455代写,Introduction to Programming代写,USC代写,C++代写,Inheritance代写,Pointers代写,CS455代编,Introduction to Programming代编,USC代编,C++代编,Inheritance代编,Pointers代编,CS455代考,Introduction to Programming代考,USC代考,C++代考,Inheritance代考,Pointers代考,CS455help,Introduction to Programminghelp,USChelp,C++help,Inheritancehelp,Pointershelp,CS455作业代写,Introduction to Programming作业代写,USC作业代写,C++作业代写,Inheritance作业代写,Pointers作业代写,CS455编程代写,Introduction to Programming编程代写,USC编程代写,C++编程代写,Inheritance编程代写,Pointers编程代写,CS455programming help,Introduction to Programmingprogramming help,USCprogramming help,C++programming help,Inheritanceprogramming help,Pointersprogramming help,CS455assignment help,Introduction to Programmingassignment help,USCassignment help,C++assignment help,Inheritanceassignment help,Pointersassignment help,CS455solution,Introduction to Programmingsolution,USCsolution,C++solution,Inheritancesolution,Pointerssolution,