Intro To C++ classes, operator overloading and inheritance by Martin Hristoforov
This tutorial is supposed to teach you how to do classes, operator
overloading, and inheritance. NOTE: You need to have a good knowledge about
NOTE: Please put #include < iostream.h> at the beginning
If you're reading this then I guess that you want to learn some more about classes and some of the goodies that come with them.
NOTE: This is a lengthy tutorial.
This is our basic class from which we are going to do everything else. You should notice the Constructor. This is a very useful feature because it lets you set the class variable(s) value(s) with it just like you would in a structure for instance. The constructor is basically an overloaded function so you should use it as you would overloaded functions. In our case if there are no arguments then number1 will be set to 0, and if an integer is passed then number1 will be set to the passed value. Lets see out how main goes:
Easy eh? I thought so. See, using classes is
same as using structures with the only difference being that you can have
functions in them which you use just like ordinary functions but with classname
and a dot before the function name(you need to declare it first just like you
would declare a structure.) You don't have access to the protected variable(s)
in the class so it is essential to use class functions if you want to change any
of those values. You can however declare the class variables as public instead
of private but this is kind a pointless since one of the major ideas if classes
is data hiding.
OurClass operator ++ ()
To see what happened let's make main looking like this:
First off you need to use the operator keyword when
overloading operators. In out case we don't need any arguments (the ++ and --
operators are the only ones that don't need arguments.) So when we say classname
++ we are basically calling the function ++ in classname and do what we need
OurClass operator + (OurClass second)
Please note the return. Let me clarify, we are basically defining a new class, we put 10 in the parentless with wich we are calling the constructor so we return a class in which the number1 integer is set to 10 (yes you can do this kind of stuff :).
Main will look like:
Now you may ask what the hell happened? Well, it's actually pretty easy. Remember when I told you that when you write classname ++ you are basically calling the ++ function in the classname. Well this is the same with the difference being that you say classname + classname2. So classname2 is the argument. See how easy it is? Read the note.
NOTE: classname - this is the class in wich the function + will be executed. The function name can be +. -. *, /, <, >, % and all other operators (my guess will be that you could even overload the AND(&&),OR(||) and NOT(!) ones. classname2 - this is the class wich is sended as an so you would need to use classname2.
I hope you understood that one. You can overload operators for various things. For instance check out the string.h header and look at the code. You'll see one way to put it in a good use. Next we are going to do some inheritance. So how do we do it? Well it is easy. First we will add 2 lines to our class, those lines will be( just after the the:
So then we are going to make the derived class. We are going to call it Derived. Here is the code:
class Derived : public OurClass
Let me try explain what the hell just happened. First of all we added the:
OurClass (which we should now refer as our BaseClass.) Protected is essentially the same as private with the only difference being that you can access this variable from the derived class, where you cannot access the private variables. So the derived class is a class that inherits all the functions of the base class and you can also add new functions to the derived class(and variables). So for instance if we have a class that describe a human being we could put in it stuff like height, eye color, hair color, and all the features that are the same between a man and a woman. So then we could make 2 derived classes, one for a man and one for a woman and in it we could put only the characteristics that are special for each one (like genitals or something.) Anyway, lets see out main (I created a new main so you donít get confused with all the previous stuff).
So this outta make it quite clear. A derived class has all the so-called traits of the base class plus itís own traits (traits are the functions and the variables). To use a variable from the base class when youíre in the derived class you need to get it as protected or as public. Declaring the derived class is made with: Class DerivedClassName : public BaseClassName This is how itís done. If you donít understand it then feel free to go and grab a book with a better explanation(I tried making this stuff as short as I could and if you get a book then you will read a lot more). So if you liked this tutorial send me an e-mail at firstname.lastname@example.org and I will try get more tutorials on the fascinating world of c++.
Add your tutorials to this site Mail Me
Copyright © 2001-04 [Gaurav Creations]. All rights reserved