This talk begins with the most important concepts in object-oriented design-classes, and some of the knowledge derived from classes.
In the process-oriented design, the module of the program is composed of functions, and the program module in object-oriented design is composed of class. The function is just the encapsulation of the statement and the data, and the class is the encapsulation of the function and the data, the contrast is certainly the object-oriented design more heavyweight, more suitable for the development of large-scale programs.
In fact, a class is a custom data type that has many similarities to general types such as int, char, and so on. We can define variables of type int, and we can also define a variable of a class type, the variable defined by the class is called the object of the class, and the process of defining the object is called instantiation.
1. declarations of classes
The syntax form for a class declaration is as follows:
Class name
{
Public
Public member (external interface)
Protected
Protected members
Private
Private members
}
Of course, the public, protected and private keywords here can be arbitrarily changed order, such as declaring a private member before declaring the other can also, each keyword can also appear several times, such as declaring some members of the public, followed by a public declaration of other members, It is also possible, but generally we declare the class in the form above.
Here's the clock. As an example, declare a class to describe a clock:
Class Clock
{
Public
void settime (int newh,int newm,intnews);
Voidshowtime ();
Private
int Hour,minute,second;
};
The class clock encapsulates the data and behavior of clocks, called the data members and function members of the Clocking class, respectively. In the declaration of a class, only the prototype of the function is declared, and the implementation of the function is that the function body can be defined outside the class, and of course it can be written in the class declaration, which becomes an implicitly declared inline function. I'll tell you later. The following is how the Write function is implemented in an out-of-class way:
void Clock::settime (int newh, int newm, int NewS)
{
HOUR=NEWH;
MINUTE=NEWM;
Second=news;
}
void Clock::showtime ()
{
cout<}
Note: The function name is preceded by the class to which it belongs, to indicate which class it belongs to.
This completes the declaration of the clock class. First, the class name is declared with the keyword class, and then the data members and function members of the clock class are described, the access control properties of the class members are explained by public and private keywords, and the implementation of the member functions is given.
2. access control for class members
We then look at the access control mechanism through the clock. In normal use we can only look at the time through the clock panel, adjust the time by a button or knob, so the Panel and button or knob is the external interface of the clock, clock class SetTime and Showtime are the Clocks class external interface. In addition to using the clock, we can not open the clock to change the internal time, which will break the clock, so the clock time is a private member, hour, Minute, second are private members, the external cannot directly access, can only be accessed through the external interface.
The control of a class member's access rights is achieved by setting the access control properties of the member. There are three types of access control properties: Public, private, and protection (protected).
The public type declares the outer interface of the class. Public-type members are declared with the Common keyword. External access must be done through an external interface. For example, for the clock class, external want to view or change time can only be achieved by SetTime and Showtime two public types of functions.
The private type member of the class is declared after private. If the access control attribute is not indicated, the default is private. For example, in the class clock declaration, if the public is not there, then both the SetTime and Showtime functions are private by default. Private type members can only be accessed by member functions in this class and cannot be accessed externally. Hour, Minute, and second are all private type members in the Clock class.
Members of a protection type and private type member permissions are similar, and the difference is that a function in a subclass derived from a class has access to its protected members, which is discussed later.
3. member functions of the class
The declaration of a function prototype is written in the body of the class, and the prototype describes the function's parameter type and number and the return value type. The implementation of the function is outside the class declaration, but unlike the normal function, the Write function is implemented by adding the class name, indicating the class to which it belongs. The specific form is:
Return value type class Name:: function member name (parameter table)
{
function body
}
The member function of a class can also be a default parameter value, and its invocation rules are the same as the normal functions previously spoken.
A simpler member function of a class can also be declared as an inline function, which, like a normal inline function, inserts the function body of an inline function into each place where it is called. There are two ways to declare an inline function: implicit and explicit declaration.
It is implicitly declared that the function body is placed directly within the body of the class. For example, the Showtime of a clock class is declared as an inline function, which can be written as:
Class Clock
{
Public
void settime (int newh,int newm,intnews);
void ShowTime ()
{cout<Private
int Hour,minute,second;
};
For the readability of the program, let everyone know that it is an inline function, or explicitly declared with the keyword inline. Just like the normal inline function, when the function is implemented, precede the function return value type with inline, the declaration does not add the function body, or take the Showtime example of the Clock class:
Inlinevoid Clock::showtime ()
{
cout<}
effect, explicitly declaring and implicitly declaring an inline function is exactly the same.
4. Objects
The object of a class is a specific entity that has that type of class. Just like a variable of a general type, a class is a custom data type, and the object is a variable of that class type. Declares an object and declares a variable in the same way: the name of the class name object;. For example, declaring an object of a clock class: Clockmyclock;. When you declare an object, you can access the public members of the object, which requires the use of "." operator, the general form of calling a public member function is the object name. Public member function name (parameter table), access public data member in the form of object name. Public data member. Of course, the general data is private, but it does not exclude the public type at times. For example, the member function ShowTime () of an object Myclock can be accessed in the form of Myclock.showtime ().
#include <iostream>
using namespace Std;
The first part
Class Clock
{
Public
void settime (int newh,int newm,int NewS);
Voidshowtime ();
Private
int Hour,minute,second;
};
Part II
void Clock::settime (int newh, int newm,int NewS)
{
HOUR=NEWH;
MINUTE=NEWM;
Second=news;
}
void Clock::showtime ()
{
cout<}
Part III
int _tmain (int argc, _tchar* argv[])
{
Clock Myclock;
Myclock.settime (8,30,30);
Myclock.showtime ();
return 0;
}
This procedure is divided into three separate parts like the annotation, the first part is the definition of the class clock, the second part is the concrete implementation of the member function, the third part is the main function main, and then add some statements to achieve some of the other functions you want.
This section gives you a class declaration, class member access control, class member functions and objects, a lot of content, we steady mind slowly understand.
VC + + Lesson (object-oriented II)