Lab #11, Friday April 6
The purpose of this lab is to give you practice with inheritance.
Complete the following non-coding steps:
Set up a Visual Studio project with the OpenGL graphics library as described in Lab 9.
Copy these files into your new project source code folder, in place of the Main.cpp file from Lab 9. : Main.cpp,
Add the files to your project's source code
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.
- 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.
- 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.
- 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.
- Test that your Shape's draw() function is working by creating a Shape in the display() function in Main.cpp.
Shape s("Shape",150,150,50,50); should draw a red X in the screen from 150,150 to 200,200.
- 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.
- Try commenting out the line of code
In its place, use:
Shape *s = &ovals[i];
- Did the program run as expected? Explain to the TA or instructor what it appears is going on.
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.