In the last two articles, we emphasize the operation of the knowledge of the structure.
Later, we will gradually complete with C + + as the main body, which means that our tutorials officially into object-oriented programming.
The previous tutorial I have repeatedly explained that the structure of the master is very important, where is the important? Important in the structure and class have the same characteristics, but there is a big difference, the class is the foundation of object-oriented programming, but it is very close to the structure of the relationship.
We create a struct in the C language we use the following methods:
struct test
{
Private
int number;
Public
float Socre;
};
The class is created in almost the same way as the structure, looking at the following code:
Class Test
{
Private
int number;
Public
float Socre;
Public
int RP ()
{
return number;
}
void Setnum (int a)
{
Number=a;
}
};
But you notice that no, standard C is not allowed to declare functions in the structure, but C + + class can, this point and C have the essential difference, very good embodiment of C + + object-oriented characteristics!
The past C language is a non object-oriented language
His characteristics are:
program = algorithm + data structure
But the features of C + + are
Object = algorithm + data structure
Program = Object + Object + Object + Object + ...
So according to this feature, we are defining a struct variable of our own definition. This variable should be called an object or an instance.
For example
Test A;
Then A is an object (instance) of the test structure
The members of the test structure body can be called a component, for example:
a.socre=10.1f;
Then number is the component of object A of test structure (called data member, or attribute) score;
Members of a struct in C language their default storage control is public and the default storage control for classes in C + + is private, so members in a class that need to be externally dropped must be added with the keyword Public declaration as a common type, which is also used for member functions in the class. There is not much difference between the operation of a function and the ordinary function.
For example, the RP () member function in the above example, if we have the following definition:
Test A;
, calling RP () should be written as:
A.RP ();
The invocation of a member function is consistent with the invocation of a normal member variable using the. operator.
This section provides a complete example for consolidating contact.
The following (important and special places have detailed annotations):
#include <iostream>
using namespace Std;
Class Test
{
private://Private member classes cannot be accessed directly
int number;
public://Common member classes can be accessed directly
float Socre;
Public
int RP ()
{
return number;
}
void Setnum (int a)
{
Number=a;
}
};
void Main ()
{
Test A;
a.number=10;//wrong, private member cannot access external
a.socre=99.9f;
cout<<a.socre<<endl;//Public members can be accessed externally
A.setnum (100)//The assignment of private member number indirectly via public member function Setnum ()
COUT<<A.RP ()//indirect Returns the value of private member number
Cin.get ();
}
OK, here's how to define a member function (method) inside a class, and let's look at the domain specifier (::).