TV->SwitchOn() ;and to set the volume to max
TV->SetVolume(10) ;
Within our SetVolume() function we should check the value passed in. If it was outside the range 1-10 then we do nothing or output an error message.
There are other benefits as well. If a real TV had tubes and we changed the innards to transistor then we'd also need to change where the screwdriver goes. Accessing it through the controls means we're don't have to worry about that. Likewise in the code, we can change how it works inside the object but so long as the interface controls (accessor functions) remain the same we don't care how it works.
Inheritance
We can define an object based on an existing object and extend it. This is called inheritance. Lets say we now want our TV to take input from DVD or cable. So we have a TV version 2.0. This has another control with three settings: Aerial, DVD or cable.We define TV2 as a descendant of TV. This means that we get all of the accessor methods and data from the TV object and we just add these new members.
int inputdevice;
SetInputDevice(int device) ;
int getInputdevice() ;
Our TV2 has all the methods of TV so we can still call
tv->SetVolume(5) ;etc.
Polymorphism
Lets add another new feature. The ability to change volume and channel settings by remote control. A new TV3 object will do this. Now the SetVolume() and SetChannel() methods need to be altered. What we do is define these functions as 'virtual' in the original tv object by adding the keyword virtual.
virtual SetVolume(int volume) ;
virtual SetChannel(int channel) ;
and in our TV3, we again define these SetVolume() and SetChannel() with new code to handle input from the remote control. This means that if we change some code that already calls
TV->SetChannel()or
TV->SetVolume()to use tv3 objects instead of tv, it will use the new versions of these functions without needing any other code changes. Making these functions virtual meant we could override them in a descendant class and this is what polymorphism is about.
Understanding OOP is essential to C++ and C# development but don't worry if it hasn't quite sunk in yet. The tutorials for both languages include OOP code but we suggest you bookmark here and return at a future stage when you are doing those tutorials. It will eventually click for you.

