Before going straight into translating our code in OOP it could maybe be necessary to give an introduction to classes.
To give a simple explanation and for those of you that know the C language, we can say that classes are extensions of C data structures to which we have added functions called methods and member accessors to determine which data or method members are publicly accessible, accessible to derived classes or accessible only to the declaring class.
Here is how we could translate a C structure into a C++ class.
The C structure :
The Corresponding C++ class :
As you see, the writing is very similar, you replace the keyword struct by the keyword class and you have it. Except that there is still a hidden problem in our class. You must be aware that class members have a private access by default unlike the structures in which the members have only a public access which doesn't need to be specified.
So our class should be written like below for it to be as functional as the C structure :
Now our class member m_Member is accessible the same way as in the structure. But please note that this is for illustrations purposes only as in good OOP, class data members should normally not be accessible publicly.
Additionally to the public keyword which renders a member of a class publicly accessible, there are also the keywords protected and private.
The protected keyword allows a member or members of a class to be accessible to derived classes.
The private keyword allows a member or members of a class to be accessible only to the declaring class.
Some other rules apply, but for now we will skip them to keep things simple and to not confuse everyone.
Here is a class example with the three member accessors :
That's all about classes for now. In the next posts I will talk about special members called constructors and destructors.
If any question, don't hesitate to ask.
To give a simple explanation and for those of you that know the C language, we can say that classes are extensions of C data structures to which we have added functions called methods and member accessors to determine which data or method members are publicly accessible, accessible to derived classes or accessible only to the declaring class.
Here is how we could translate a C structure into a C++ class.
The C structure :
Inserted Code
struct MyStruct { int m_Member; };
The Corresponding C++ class :
Inserted Code
class MyClass { int m_Member; };
As you see, the writing is very similar, you replace the keyword struct by the keyword class and you have it. Except that there is still a hidden problem in our class. You must be aware that class members have a private access by default unlike the structures in which the members have only a public access which doesn't need to be specified.
So our class should be written like below for it to be as functional as the C structure :
Inserted Code
class MyClass { public: int m_Member; };
Now our class member m_Member is accessible the same way as in the structure. But please note that this is for illustrations purposes only as in good OOP, class data members should normally not be accessible publicly.
Additionally to the public keyword which renders a member of a class publicly accessible, there are also the keywords protected and private.
The protected keyword allows a member or members of a class to be accessible to derived classes.
The private keyword allows a member or members of a class to be accessible only to the declaring class.
Some other rules apply, but for now we will skip them to keep things simple and to not confuse everyone.
Here is a class example with the three member accessors :
Inserted Code
class MyClass { private: int m_PrivateMember; protected: int m_ProtectedMember; public : int PublicMethod(); // A function in a class is called a method. };
That's all about classes for now. In the next posts I will talk about special members called constructors and destructors.
If any question, don't hesitate to ask.
Simplicity is the Ultimate Sophistication.