|
|
|
|
|
Understanding C++ Classes TutorialAn Introduction to OO Programming for C and C++ ProgrammersTutorial article for C and C++ programmers covering OO terminology, use of header files in C++ programming, classes and notation.
IntroductionObject Oriented Programming is an important part of modern systems and application implementation. Most GUI based operating systems like Windows make heavy use of different 'classes' of window, widget and other GUI parts. Understanding why this paradigm is so important is vital to being able to make use of advanced techniques such as window subclassing. As a concrete example of OO programming in action, window subclassing is a good illustration of the concept. Essentially, each type of element in Windows is derived from one super-class that provides some basic functionality - the window. A button is a window with certain graphical properties that make it shade when clicked. A text box is a window that handles the keyboard in a certain way. A dialog box is also a window, that can contain other windows, and the list goes on. The power of object oriented programming means that we can also intervene, as a programmer, and make customized versions by subclasssing, say, the button class, to make an icon button. Before we can do that, however, we need to understand a little more about object oriented programming in C and C++. (An overview of object oriented design can be found here). OO and C++ TerminologyIn OO terms, each object becomes concrete when an instance of that object is created. In order to communicate with that object, we can send it messages. The object has also a set of internal methods which are used to manipulate the data that is needed to represent the state of the object at a given time. An important facet of OO programming is that the data is hidden from outside view. Only the object can access the data, and messages need to be exchanged in order to:
The result of causing it to act manifests itself though the sending of messages to affected objects. In C++ terms, we speak of a class (object) which can be used to declare an instance of that object as a variable. The class has a private and public part, the public part containing the member functions that act as messages, and the private part containing the internal data and methods. Using C++ Header FilesTraditionally, the class definition is stored in one or more header files. These are then accompanied by C++ code files which implement the prototype classes. It is possible to create all the code that defines a class inline, but the separation between the definition and implementation helps maintainability. The header file (.h) is then #included in the C++ code like any other header file. The C++ ClassA C++ Class definition follows a basic skeleton: class CMyClass { private: // Data and internal methods public: CMyClass(); // Constructor ~CMyClass(); // Destructor // Data access functions }; The constructor is called when an instance of the class is instantiated by making a call to the C++ new operator: CMyClass * oClass = new CMyClass(); In a similar way, the destructor is called when the class instance (object) is destroyed by a call to delete: delete oClass; Accessing ClassesThe object is accessed by way of a pointer: oClass-AFunctionCall(); If there are data elements in the public part of the class (bad style), then they are also accessed through the pointer. However, this violates the OO principle of data hiding. Functions (methods / messages) called in this way can also return values.
The copyright of the article Understanding C++ Classes Tutorial in Computer Programming Tutorials is owned by Guy Lecky-Thompson. Permission to republish Understanding C++ Classes Tutorial in print or online must be granted by the author in writing.
|
|
|
|