8.6 HOMEWORK


8.6 HOMEWORK

  1. How many times will the destructor of class X be invoked as the variables go out of scope when the flow of execution hits the right brace of main in the following C++ program? Note that main consists of four groupings of statements: the ‘A’ grouping, the ‘B’ grouping, the ‘C’ grouping, and the ‘D’ grouping. In each grouping, the first statement constructs a new object, with the other statements creating references to the previously created objects or references. So you need to ask yourself that as the variables xobj1, xobj2, and xobj3 from the ‘A’ grouping go out of scope, how many times will X's destructor be invoked. And so on for the other three groupings.

     
    //Reference.cc #include <iostream> using namespace std; class X { int n; public: X( int nn ) : n(nn) {} ~X() { cout << "destructor invoked for X obj with n= " << n << endl; } }; int main() { X xobji( 100 ); //(A1) X& xobj2 = xobji; //(A2) X& xobj3 = xobj2; //(A3) const X& xobj4 = X(200); //(B1) const X& xobj5 = xobj4; //(B2) const X& xobj6 = xobj5; //(B3) X* p = new X(300); //(C1) X*& q = p; //(C2) X*& r = q; //(C3) delete r; //(C4) const X* s = new X(400); //(D1) const X*& t = s; //(D2) const X*& u = t; //(D3) delete u; //(D4) return 0; }

  2. Note carefully the difference between the main of the program in the previous problem and the main below. The statements that invoke the delete operator appear to be at odd locations—look at the statements in lines (C2) and (D2). The new main also includes in lines (E1) and (E2) additional statements involving a const pointer. Now answer the following questions:

    1. Will this program compile?

    2. Assuming this program compiles, are there any obvious memory leaks in the program? Is the destruction of the object constructed in line (E1) an issue from the standpoint of a memory leak?

    3. Assuming that this program compiles and runs to completion, how many times will the destructor of the class X be invoked?

       
      //Reference2.cc #include <iostream> using namespace std; class X { int n; public: X( int nn ) : n(nn) {} ~X() { cout << "destructor invoked for X obj with n= " <;< n << endl; } }; int main() { X xobj1( 100 ); //(A1) X& xobj2 = xobj1; //(A2) X& xobj3 = xobj2; //(A3) const X& xobj5 = X(200); //(B1) const X& xobj6 = xobj5; //(B2) const X& xobj7 = xobj6; //(B3) X* p = new X(300); //(C1) delete p; //(C2) X*& q = p; //(C3) X*& r = q; //(C4) const X* s = new X(400); //(D1) delete s; //(D2) const X*& t = s; //(D3) const X*& u = t; //(D4) X* const w = new X(500); //(E1) *w = X(600); //(E2) return 0; }

  3. Numerically intensive applications often require extensive memory allocation and deallocation. As an exercise that is somewhat numerically intensive, write a C++ program that extracts the numerical information contained in a JPEG image file. Note that this exercise is not about decoding a JPEG image, but about allocating (and, when so needed, deallocating) the memory needed for an array representation of the data generated by a publically available decoding function such as djpeg. With appropriate flags, a function such as djpegcan convert an encoded JPEG image into a PPM file in which each image pixel is represented by three consecutive bytes for the three RGB components of color at that pixel. Each byte represents the numerical value of a color component as an unsigned integer between 0 and 255. The djpegfunction also deposits at the top of the PPM file a header that contains information about the height of the image, its width, and so on. More specifically, the format of a PPM file is as follows, where the specific numbers shown refer to a particular image

           comment lines that start with the character #       the label P6 for color image and P5 for a               black and white image       579 768 for specifying the image width and               the height in pixels       255 for the number of quantization levels               for each color       The rest of the data in the file consists of                3 * width * height bytes. Each pixel in                the image is represented by three                consecutive bytes, the first for Red,                the second for Green, and the third for                Blue. 

    Your program must read this header and determine whether the image is color or just black and white. Your program must then extract information about the height and the width of the image and the number of quantization levels for each color. Your program must also include a filter function that allows you do simple pixel manipulation (such as subtracting adjacent pixels to enhance edges). Finally, your program should invoke another publically available function, such as cpegfor converting the processed PPM file back into an output JPEG image.

    Your program should be in the form of a class called JpegToDatawith the following data members:

         int width     int height     int quantLevels     string colorLabel     int* pixelArray     int* resultArray

    where widthis the widthof the image, heightthe height of the image, quantLevelsthe number of quantization levels for each of the colors, pixelArraythe array of integers corresponding to the pixels of the JPEG image, and resultArraythe array obtained by applying the filter to the contents of pixelArray. You need to provide the JpegToDataclass with at least the following methods:

         void imageFilter()     void toJpeg() 

    The imageFiltermethod when applied to an object of type JpegToDatanumerically processes the pixelArraywith some sort of a filter function and puts the result in resultArray. The toJpegmethod creates a JPEG image out of the contents of resultArrayby using the cjpeg function.

    Suggestions:

    For executing the "djpeg"and "cjpeg"commands in C++, you'd need to invoke the systemcommand as in

         system( command_string_as_a_C_style_string ); 

    In addition, your C++ program will most likely use the get() or get(ch), and peek() functions defined for the input stream objects (see Section 6.8 of Chapter 6) in order to extract the information from the PPM file, as in the following snippets of code:

         ifstream( "temp.ppm");     // for getting rid of the comment block     if ( '#' == in.peek() )         while( '\n' != in.get() )             ;     in >> colorLabel;     in >> width;     ...     ...     int i = 0;     unsigned char pix;     while ( in.get( pix ) )         pixelArray[ i++ ] = pix;     ..... 

    and the put ( ch ) method defined for the file output stream for writing the pixels back into an output PPM file:

         ofstream out( "temp_out.ppm");     //.....     int* ptr = resultArray;     for ( int i = 0; i < 3 * wideth * height; i++ )     out.put( (unsigned char) *ptr++ );     // ... 
  4. Do the same as in the previous problem but for Java. (The reader should note that Java comes with a class java.awt.image.BufferedImage class that gives you access to the numerical data for the pixels in an image.)




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net