The content we will gradually be completely c as the subject, which also thought that our tutorial officially entered the object-oriented programming. I have repeatedly explained it again, the mastery of the structure is very important, where is it important? It is important to have the same characteristics, but there is a big difference, and the class is the basis for object-oriented programming, but it is a close relationship with the structure. We created a structure c language we use the following method: // Author: Guan Ning site //: // All manuscripts www.cndev-lab.com are copyrighted To reprint, please be sure renowned source and author struct test {private: int number; public: float socre;}; mode and create structures look almost the same as the class code as follows: // author: @ tube Ning site: www.cndev-lab.com // All The manuscript has copyright. For reprint, please be sure to be famous and the author class test {private: int number; public: inflic: int r () {return number;} void setnum (int a) {number = a; }}; But everyone notes that there is no, the standard C is not allowed to declare the function in the structure, but the classes in C can be, this is the difference between the essence, very good embodiment of C object-oriented Features! The past C language is a non-dominant language. His characteristic is the program = algorithm data structure but C features are object = algorithm data structural programs = object object object object ....... So according to this feature, when we define a structural variable defined by your own, this variable should be called an object or an example, such as Test A; then A is an object of the Test structure (instance) TEST structure It can be called a component, such as: a.socre = 10.1f; then Number is the component of the Test structure of object A (or a data member, or a attribute) score; each member in the C language has their default storage Control is the default storage control of PUBLIC and C is private, so if members in the class If necessary, be sure to add keyword public declaration into public type, this feature is also used in the class in the class function, function The operation mode and the normal function difference are not large, for example, the RP () member function in the above example, we have the following definition TEST A; if you call RP (), you should write to a.rp (); member function call and The consistency of the consensus of ordinary member variables is consistent. This section gives a complete example in order to consolidate the contact. As follows (special and important place to have detailed notes): // Author: Guan Ning site //: // All manuscripts www.cndev-lab.com are copyrighted To reprint, please be sure renowned source and author #include
Using Namespace Std; Class Test {private: // The private member is not able to access the int number; public: // A total of members can directly access Float Socre; public: int rp () {return number;} void setnum INT a) {Number = a;}}; void main () {test a; //a.number=10 ;// error, private member cannot be external to A.Socre = 99.9f; COUT <
<
Using namespace std; int pp = 0; class test {private: int number; public: float socre; int pp; public: void rp ();}; void test:: rp () // External Utilization Domain Differential Definition Test class member function {:: pp = 11; // variable preamble to the domain distinguished to the global variable PP assignment PP = 100; // Setting the structural variable} void main () {test a; test b; a.rp (); cout <<
PP = 100; so you write the above member function as the following situation is also correct: void test :: rp () {:: pp = 11; this-> pp = 100; // THIS pointer is pointed to the pointer to a object} The members of the class and the normal function are the overloaded operation, and the overload function has been said, and it will not be described here. Examples are given a close look: // Author: Guan Ning site //: // All manuscripts www.cndev-lab.com are copyrighted To reprint, please be sure the source and author famous #include
Using namespace std; class test {private: int Number; public: float socre; int pp; public: void rp (int); void rp (float);}; void test :: rp (int a) // External Utilization The Member Function of the Domain Area Trich Definition Test class {cout << "calls member functions! A:" <
<
Using namespace std; }in; public: float socre; int pp; public: int rp (int);}; int test :: rp (int a) // Define TEST class Member function {Number = 100; return a number;} void run (test * p) // utilizes pointer to {COUT <
RP (100) <
#include "ballscore.h" Using namespace std; void main () {ballscore jeff; cout <