NOTE: Please put #include < iostream.h> at the beggining
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 lenghty tutorial.
Now first we are going to create a class. We'll call it OurClass because it is ours :). Here's the code:
class OurClass
{
private: // optional but it is more readable that way
int number1;
public: // not optional
OurClass() : number1(0)// Constructor
{ } // nothing in here but feel free to add whatever you want
OurClass(int num): number1(num)//Constructor with arguments
{ }
void set_number(int num)
{ number1 = num; }
int get_number()
{ return number1; }
}; // don't forget the ; at the end of every class
This is our basic class from wich we are going to do everything else. You should notice the Constructor. This is a very usefull feuture 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:
int main()
{
OurClass oc1;
OurClass oc2(10);
cout << oc1.getnumber() << endl << oc2.get_number(); // returns 0 in the first line and
// 10 on the next one
oc1.set_number(100);
cout << endl << oc1.get_number(); // shows 100
return 0;
}
Easy eh? I tought so. See, using classes is esentially the same as using structures with the only difference being that you can have functions in them wich 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.
Next thing we should do is overload an operator. Let's overload the ++ operator. All we gotta do is write one new functions in our class. The function code would be (this should be added inside the class):
OurClass operator ++ ()
{
++count;
OurClas temp;
temp.count = count;
return temp;
}
To see what happend let's make main looking like this:
int main()
{
OurClass oc1;
OurClass oc2(10);
cout << oc1.getnumber() << endl << oc2.get_number(); // returns 0 in the first line and
// 10 on the next one
oc1.set_number(100);
cout << endl << oc1.get_number(); // shows 100
oc1++; cout << endl << oc1.getnumber(); // Now we get 101
oc2 = oc1++ ; cout << endl << oc2.getnumber(); // Getting 102
return 0;
}
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 acordingly. Now lets overload the + operator wich is a little bit harder. The code for the class will be (add it to the class):
OurClass operator + (OurClass second)
{
int answer = number1 + second.number1;
return MyClass(answer);
}
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 wich the number1 integer is set to 10 (yes you can do this kind of stuff :).
Main will look like:
int main()
{
OurClass oc1;
OurClass oc2(10);
cout << oc1.getnumber() << endl << oc2.get_number(); // returns 0 in the first line and
// 10 on the next one
oc1.set_number(100);
cout << endl << oc1.get_number(); // shows 100
oc1++; cout << endl << oc1.getnumber(); // Now we get 101
oc2 = oc1++ ; cout << endl << oc2.getnumber(); // Getting 102
OurClass oc3;
oc3 = oc1 + oc2; cout << endl << oc3.get_number; // It should be 203
return 0;
}
Now you may ask what the hell happend? Well, it's actually pritty easy. Remeber 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:
private: // optional but it is more readable that way
int number1;) :
protected:
int pnumber;
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
{
private:
// you can put variables in here but we won't do that in this tutorial
public:
Derived(int num) : pnumber(num)
{ }
int get_derivednum()
{ return pnumber; }
void set_derivednum(int num)
{ pnumber = num; }
};
Let me try explain what the hell just happened. First of all we added the:
protected:
int pnumber;
OurClass (wich 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 feutures 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 previlious stuff).
Int main()
{
Derived d1;
d1.set_derivednum(145); cout << d1.get_derivednum() << endl; // Those are the basic functions that are no different than the functions in the BaseClass
d1.set_number(10); cout << d1.get_number() << endl; // Here we are using the //base class functions
return 0;
}
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 mhrist@earthlink.net and I will try get more tutorials on the fascinating world of c++.
Martin
|