Posted by Filip Ekberg on 13 Nov 2008
Classes, inheritance and polymorphism can sometimes be somewhat hard to understand. So a practial example suits many well. Therefore this will be a tutorial where i will touch the areas of classes, pointers, inheritance and polymorphism.
So to just clearify some expressions i will add a little dictionary at the top:
A class can somewhat be seen as a blueprint of your object. I.e. having a blueprint for a building, then this would be the class. Then a Constructor would build this to become an actuall touchable object.
When you inherit something you can think of it more like you extend this thing. I.e. having a Car and then having a Sports Car where there are some new variables to it. The Sports Car is actually a Car but it’s also extended with new properties.
This is taken from Wikipedia: “In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B”. Read more here
For a pointer explenation look here
I will be using Visual Studio 2008 with the built in C++ compiler for the following examples.
A good practice will be to create a Bank, when you get a new assignment or just want to create something new, start by thinking about the structure, what actuall needs does this have? Well when creating a Bank i think to myself that i’d like to be able to create Bank Accounts, attatch these to People and you can go on and on.
But, for the example i will only go as far as creating a Bank, a Bank Account ( Abstract ) and a Student Bank account which is the only Bank account you can actually get.
Amount of Deposits
Last Deposite Month
So the big difference between Account and Student Account is that the Student Account have different rules than Account and therefore calculate a lot of things differently. We won’t go through all the logics. There will of course be a window for you to evolve this application as you feel nessesary.
Now when we have a defined structure we can start by creating a new Project and it’s files. My project will be called Bank and the following files will be placed in it:
When all my files are created a start by definint my program entry point, this being my main. Notice that i will be posting code snippets as pictures, because you should Write this yourself and not copy my code!
We prepare for an input/output stream by including <iostream> and setting using namespace std;
Now the next part is to define the Bank.h We need an appropriet constructor, desctructor and some access methods, remember to never open up member variables publicly!
The #ifndef part is important for the compiler, we dont want to define our header file more than once, because that is not nessesary!
I assume all the parts are clear except the last one, read more about how that pointer type works in the previous post here on my wordpress.
The implementation itself will be up to you, i will only be showing the structure, header-files and some code from main and where polymorphism is concerned!
The next file to create would be the BankAccount, remember to add #include “BankAccount.h” to Bank.h when it has been done. Otherwise the BankAccount *accounts array will fail because undefined symbol.
Back to the third file, the BankAccount.h this is how i would structure it:
The part to look deeply on here is the Deposite method, watch closely, it’s defined as followed: virtual void Deposite(double amount) = 0; So this method is expected to be overrided in a inherited class and it has the return type of void and an in parameter of the type double.
Now the next file to create is the StudentAccount.h where we will be deriving from BankAccount.
So the following class StudentAccount is extending a class called BankAccount. So this means that StudentAccount is a BankAccount with extended properties and/or overrided functions.
Now, look closely again, here we have the virtual void Deposite part again, but without the =0, so this means we will be creating a definition for this function.
Also the Amount of Deposits and Last Deposit Month is just there to simulate some properties that might be nessesary to do something on a student accont. In my case this is for the Deposite. A Student Can’t deposite money more than three times per month, this is because a student gets 2% interest everytime they deposite money.
This is my Constructor for StudentAccount
As you can see, we pass the Variables Owner and Deposite back to the constructor of the base type, being BankAccount.
Now this is how my Deposite Method is implemented:
Now the last think i want you to look at, is my final Main:
I want you to try and create all the nessesary functions in Bank.cpp, BankAccount.cpp and in StudentAccount.ccp to get the following output from this Main.cpp:
I would happily answer any questions about this project.
comments powered by Disqus