In this lesson, our instructor Alvin Sylvain goes through an introduction on part two of Object Oriented Programming. He discusses initialization lists, destructors, name components, friendship functions and classes designing for derivation, abstract class, compilation units, and include guards.
Add overloaded constructors for the “Vehicle” class you wrote in the last lesson
Keep one default constructor that sets all attributes to sensible values.
Add a constructor that sets the Vehicle’s name
Add a constructor that sets the Vehicle’s name and current location
Add a constructor that sets each of the Vehicle’s attributes
Initialize the attributes using an Initialization List
Add text to the Vehicle destructor that prints the Vehicle name and the fact that it is being destroyed
Repeat the questions above with the “Auto” class, and any other classes you wrote for the previous lesson.
Create a “friend” function for the “Auto” class called “inject_nitrous”. This function will multiply the current acceleration by 1.5 if it’s positive or 0.5 if it’s negative. Run this function in different places in your code to see its effect.
Create a “Driver” class, with attributes including the driver’s name, the sponsor’s name, and the number assigned for the race.
Have the “Driver” class be a “friend” of the “Auto” class
The “Driver” should not only call the “Auto’s” pedal pressing methods, but also an “inject_nitrous” method that works similarly to the function above.
Update the code so that the Driver is pressing the pedals.
Change the “Vehicle” to an abstract class. The “Auto” class can derive from it and you can instantiate and “Auto”. Note how this affects your design.
Divide all of the classes into separate compilation units. That is, you will need files for “Vehicle.h”, “Vehicle.cpp”, “Auto.h”, “Auto.cpp”, etc. The main function can be placed into a file named “RaceCarMain.cpp”.
Compile and run everything together and get it all working again.
Object Oriented Programming, Part 2
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.