Lab #11, Friday April 6

The purpose of this lab is to give you practice with inheritance.

Complete the following non-coding steps:

  1. Set up a Visual Studio project with the OpenGL graphics library as described in Lab 9.
  2. Copy these files into your new project source code folder, in place of the Main.cpp file from Lab 9. : Main.cpp, Oval.h, Oval.cpp, Rectangle.h, Rectangle.cpp
  3. Add the files to your project's source code
  4. You should be able to compile and run the project as long as the DLL and LIB files are in the right place.  The program creates three rectangles and three ovals and draws them on the screen. If you click on an oval or rectangle than its name will be displayed. Look through the comments in the code and at the class files to see how the program works.
Programming Portion:
  1. There is a lot of redundancy between the Oval and Rectangle classes. Create a parent class of both, named Shape. The Shape class should have all the items in common between Oval and Rectangle: All the variables, the getter functions, the pointInShape function, a default constructor, and a constructor that sets all the variables.
  2. Clean up the Oval and Rectangle classes to use the Shape class. The program should run as before. At this point there should be a draw() function in Oval and Rectangle, but not in Shape.
  3. Add a void draw() function to Shape. Since we don't know what kind of shape to draw, this function should draw a red X within its bounding rectangle. Refer to the code in Lab 9 for how to draw lines.
  4. Test that your Shape's draw() function is working by creating a Shape in the display() function in Main.cpp. For example:
      Shape s("Shape",150,150,50,50);
      s.draw();
    should draw a red X in the screen from 150,150 to 200,200.
  5. The line of code ovals[i].draw() is drawing an oval, so obviously it is calling Oval's draw() function, NOT Shape's draw function. In C++, we say that the draw() function is redefined in the child class.

Save your code, because we will improve upon it in Lab 12 by adding in virtual functions. Show your working code / answers to the instructor or lab assistant for credit, or email your solution by midnight.