|
|
|
|
When a Base Class has a virtual member function, any class that inherits from the Base Class can redefine the function with exactly the same prototype i.e. only functionality can be redefined, not the interface of the function. |
A Base class pointer can be used to point to Base Class Object as well as Derived Class Object. When the virtual function is called by using a Base Class Pointer, the Compiler decides at Runtime which version of the function i.e. Base Class version or the overridden Derived Class version is to be called. This is called Run time Polymorphism. |
|
|
So what exactly about a pointer allows for the compiler to decide this at run-time? |
An object that has virtual functions contains a look-up table of the actual, exact functions to call. When the object is built during execution of the programme, that look-up table is populated. |
Ch1156 wrote: |
---|
I got the bounds checking and the error throwing, I also broke everything up into separate files. |
|
|
std::cout
of an error message for now.I don't really see the point of writing all the code in the exercises. I appreciate the help, I just don't understand how to use virtual and inheritance properly, I think just bare functions with pseudo code could be more helpful as I have spent more time writing functions than I even have solving my problem. |
TheIdeasMan wrote: |
---|
Here are some concepts about what I am expecting for the way you to go about doing this project: * Keep it simple * Follow the instructions * Do 1 exercise at a time, get that working and complete with all the features and style requirements I am mentioning here. Only then move onto the next exercise. * When starting with a new exercise, start with the code you had in the previous exercise. Save each exercise in a different folder on your computer. There are going to be multiple *.hpp and *.cpp files (multiple classes), so it will be best to copy the whole folder to help avoid confusion. In your IDE, each exercise will be a separate project. Find out how to import existing files into the new project. * Think about the code you are writing, be self critical of it. Don't spend 3 minutes writing the code, then post it. Instead review each line of code (LOC), see if you can see any potential problems with it. Are you mixing types? Is the LOC going to work for different values, are there any values which might cause it to fail? Remember the goal of always striving to write code that doesn't fail. * There is going to be some planning / design required. That is, you might need to write down ideas, draw pictures to organise the design of the code, before writing any code. * when writing the code, write comments about what the code is going to do. Then go back and write the actual code for each comment. Leave the comments in the code as a bit of documentation. This is going to help you organise your thoughts. I might ask to see all the comments first, before you write any actual code. * For each function, write 3 lines of comments about what the function does * For each function argument, write a comment about what the valid range of values is. * This project is about real life objects (A pistol, so far) , think about what happens in real life, make you code work that way. That is, actions one would carry out in real life become functions in the code. As the project moves forward, there will be more objects and classes. * Choose meaningful names for variables and functions. Good code should be mainly self documenting on the names you choose, it should tell a story of what is happening, just by the names of the variables and functions. * Functions should only do one conceptual thing, and they should be relatively short - at the most say 30 LOC. * There will be times where you need to do your own research. Even though we are helping you in detail, you should be able to go and look things up yourself. We don't want to write a brand new wiki with everything there is to know about C++. * As the project gets more complex, hopefully it is instructive to see how the code changes. This project is different to a real life project in that we are starting very small (1 class) then gradually adding stuff. In real projects, modules worth of things are designed and coded all at once, so there is much less change in the code. |