Programming in Any Language
The Inheritance Demo
Making sense of Inheritance is easy when looked at through the lens of a meaningful application. In this case we are going to use a car dealership.
Dealerships have all sorts of vehicles on hand. In our case we are going to limit it to three types of vehicles – cars, trucks and SUVs. And we are going to minimize the details we are going to track to keep this demo simple.
In our application we will track Make, Model, Mileage and Price for all vehicles. For cars we will also track the number of doors. For trucks we will track drive (4wd/2wd). For SUVs we will track the number of passengers it can hold. We could track much more, but this will suffice to allow us to set up a base class and several derived classes.
Now you can’t just jump into creating an app like this. I attempted to do so and got lost pretty quickly. A step-by-step approach works a lot better. And this video is the first step: Planning and Design.
In this video we set up the base class (Vehicle) and the derived classes (car, truck and SUV). We also set up a Vehicle structure for all the data, and a Vehicle list to store all that data. Functionality for the radio buttons is also coded here to get the text boxes related to the derived classes to show up when they are needed.
The purpose of this step is to prepare the application to accept data.
In this video we set up an instance of one of the derived classes based on which radio button is active. Car, truck or SUV data is entered, applied to a structure and that piece of data is added to a list. The purpose here is to let the user enter an unlimited number of vehicles to the list. The derived classes help to identify each type of vehicle being stored in the list. The list contains all the data and is available for other purposes.
We are done with the classes. This step is simple programming of stepping through a list and writing the data out to a file. In this case the file will be a comma delimited file so that we can import the data to a spreadsheet. Besides saving the date we will also pass a header record to the file so that the spreadsheet import will readily identify each of the columns in the spreadsheet.
This video demonstrates how to import a comma delimited file into a spreadsheet.
Click to enlarge
Click to enlarge