Lab #11, Wednesday November 8
The purpose of this lab is to give you practice with virtual functions and more with graphics.
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 three files to your project's source code (or just the Ball.h and Ball.cpp if you already added Main.cpp)
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 5 red balls of different sizes and bounces them around on the screen. There are comments in the code - look through the comments to see how the program works. In particular, the init() function creates an array of 5 ball objects. The draw() function in the Ball class draws the ball on the screen. There is an updateCoordinate() function that updates the coordinates of the ball to make it bounce around.
- Make the draw() function in the Ball class a virtual function
- Create a new class, MultiColorBall, that is derived from the Ball class.
The MultiColorBall class should have an appropriate default constructor and a constructor that takes the radius of the ball.
Override the draw() function so it draws the inner 1/3 of the ball in blue, then next 1/3 of the ball in green, and the rest of the ball in red.
- Edit the Main.cpp file so an array is made of 5 Ball objects, but objects at index 3 and 4 are MultiColorBalls. There are already some lines commented out in the file that does this, you could just uncomment them. You also have to include MultiBall.h from Main.cpp.
- The program is set up to call the draw() function of whatever Ball object is in the array. Due to virtual functions, this means it should call the draw() function of the MultiColorBall for the last two entries even though the array is of type Ball. Your program should now run and bounce around two MultiColorBalls as illustrated in the image below.
Show your working code to the instructor or lab assistant for credit, or email your solution by midnight.