1. Definition of member functions
Two forms: one is to give only the prototype of the member function in the class declaration, and the function body is defined outside the class.
void Point::setposit (int a, int y)
{x=a; y=b;}
int Point::getx ()
{return x; }
int Point::gety ()
{return y; }
The other is built-in functions: Define member functions within the class, that is, built-in functions, (one is to directly define the function inside, place the function outside the class definition body, and "inline" before the function definition body, acting as a built-in function.) )
classpoint{Private: intx, y; Public: voidSetposit (int,int); intGetx (); intgety ();}; InlinevoidPoint::setposit (intAinty) {x=a; y=b; }inlineintPoint::getx () {returnx; }inlineintpoint::gety () {returnY }
2. Copy Constructors
A copy constructor is a special constructor function. Used to establish a new object based on an existing object. The user can define the copy constructor system as needed or create a default copy constructor for the class
#include <iostream.h>classpoint{intx, y; Public: Point (intAintb) {x=a; y=b;} voidprint () {cout<<x<<" "<<y<<Endl;}};intMain () {point P1 ( -, +); Point P2 (p1); Point P3=P1; P1.print (); P2.print (); P3.print (); intx; CIN>>x; return 0;}
3. Array of objects
If a class contains a user-defined constructor, and the constructor has parameters, an array of objects can be assigned by an initializer, such as 3-13. When a class contains constructors, an array of objects can be defined by assigning values to an array of objects, such as 3-14, through constructors with no arguments or constructors with default parameters.
#include <iostream.h>classexam{ Public: intx; Public: Exam (intN) {x=N;} intget_x () {returnx;}};intMain () {exam ob[4]={ One, A, -, -}; for(intI=0; i<4; i++) cout<<ob[i].get_x () <<' '; cout<<Endl; return 0;}
#include <iostream.h>classpoint{Private: intx, y; Public: Point () {x=5; y=5; } Point (intAintb) {x=a; y=b;} intGetx () {returnx;} intgety () {returny;}};intMain () {Point op (3,4); cout<<"op x="<<op.getx () <<Endl; cout<<"op y="<<op.gety () <<Endl; Point op_array[ -]; cout<<"Op_array[1] x="<<op_array[1].getx () <<Endl; cout<<"Op_array[1] y="<<op_array[1].gety () <<Endl; intx; CIN>>x; return 0;}
4. Parameter passing:
When passing an object to a function, passing a value call to a function, which is a copy of the object instead of the object itself, is passed to the function, and the modification of the object in the function does not affect the object itself that invokes the function. You can also pass the address of an object to a function. The function's modification of the object will affect the object itself that called the function. Observe the example to compare the value unchanged
#include <iostream.h>classtr{inti; Public: TR (intN) {i=N;} voidSet_i (intN) {i=N;} intget_i () {returni;}};voidsqr_it (tr ob) {ob.set_i (ob.get_i ()*ob.get_i ()); cout<<"copy of obj has I value of"<<ob.get_i () <<"\ n";}intMain () {tr obj (Ten); Sqr_it (obj); cout<<"But, OBJ.I was unchanged in main:"<<obj.get_i () <<Endl; return 0;}
Value change
#include <iostream.h>classtr{inti; Public: TR (intN) {i=N;} voidSet_i (intN) {i=N;} intget_i () {returni;}};voidSqr_it (tr&ob) {ob.set_i (ob.get_i ()*ob.get_i ()); cout<<"copy of obj has I value of"<<ob.get_i () <<"\ n";}intMain () {tr obj (Ten); Sqr_it (obj); cout<<"But, OBJ.I was unchanged in main:"<<obj.get_i () <<Endl; return 0;}
5. Static data variables
In a class definition, a member of a data member is called a static data member if it is explained as static. Regardless of how many classes of objects are created, there is only one copy of the static data. static data belongs to a class, so you can use
"Class Name::"
Accesses a static data member.
A static data member cannot be initialized in a class because it does not allocate memory space in the class, and it must provide the definition somewhere outside the class.
#include <iostream.h>classstudent{Static intcount; intStudentno; Public: Student () {count++; Studentno=count; } voidprint () {cout<<"Student"<<StudentNo<<" "; cout<<"count="<<count<<Endl; }};int student::count=0;intMain () {Student Student1; Student1.print (); cout<<"-----------------------\ n"; Student Student2; Student1.print (); Student2.print (); cout<<"-----------------------\ n"; Student Student3; Student1.print (); Student2.print (); Student3.print (); cout<<"-----------------------\ n"; Student Student4; Student1.print (); Student2.print (); Student3.print (); Student4.print (); return 0;}One important reason for C + + to support static data members is that you can avoid using global variables. 6. Static member functionsA static member function is first a member function, using the term "class name::" as its qualifier, or to indicate which object it works on; second, static member functions do not belong to specific objects, and it is cumbersome and meaningless to have a static member function to access non-static members. In general, static member access is basically static members or global variables static member functions can be defined inline, or can be defined outside the class, at this time do not use the static prefix. One reason to use static functions is to handle static data members before the object is established. There is no this pointer in a static member function. In general, static member functions do not access non-static members in a class. If you do, a static member function can access a non-static member of the object only through the object name (or a pointer to the object).
7. Friends
In order to access private members of a class outside of the class, you need to find a way to access the private members of a class without discarding private data security. The UF element in C + + is an auxiliary means to realize this requirement. A friend can be a generic function that is not part of any class, a member function of another class, or a whole class. Declaring a friend function in a class definition requires that a keyword friend be placed in front of its function name, either in the public part or in the private part. The declaration of a friend function can be inside a class or outside of a class. The friend function improves the efficiency of the program running by directly accessing the private members of the object, but at the same time it destroys the concealment of the data and reduces the maintainability of the program, which is contrary to the object-oriented design idea, so the UF element should be cautious.
C + + essay (IV.)