Original blog: Reprint please indicate the source: http://www.cnblogs.com/zxouxuewei/
It is often necessary to access the members of the object in the program. There are 3 ways to access members in an object:
- Access the members of the object through the object name and member operators;
- Access the members of the object through pointers to objects;
- Access the members of the object through the object's reference variable.
Accessing members in an object by object name and member operator
For example, the following statements can be written in a program:
stud1.num=1001; // assume that NUM is defined as a public integer data member
Represents the assignment of the integer 1001 to the data member num in the object stud1. where "." is a member operator that is used to qualify a member to indicate which object is accessed by the member. Note It is not possible to write only member names and ignore object names.
The general form of accessing members in an object is:
The name of the object. Member name
Not only can you reference the public data members of an object outside of the class, but you can also invoke the public member functions of the object, but you must also indicate the object name, such as:
Stud1.display (); // correct, call the public member function of the object STUD1 display (); // error, no indication of which object's display function
Because the object name is not specified, display is treated as a normal function at compile time. You should be aware that the members you are accessing are public or private (private) and that you can access only the members of the private and not the members. If NUM is already defined as a private data member, the following statement is incorrect:
stud1.num=10101; // num is a private data member and cannot be referenced by outsiders
Only public member functions can be called outside of the class. There should be at least one common member function in a class as an external interface, otherwise you cannot do anything to the object.
To access members in an object by pointing to the object's pointer
A pointer to a struct variable is described earlier (for details, punch: A pointer to a struct variable), you can refer to a member in the struct by pointer. Using pointers to access members in an object is similar to this. If you have the following program segments:
class Time { public// data members are common int hour; int minute; }; // define object t and pointer variable p // make P point to object T // The member of the object that the output p points to hour
on the premise that P points to T, P->hour, (*p). Hour and T.hour three are equivalent.
The following code reference is also available
#include <iostream>#include<string>using namespacestd;class_test_class{ Public: intAge ; stringname; voiddisplay () {cout<<" Age is"<<age<<Endl; cout<<"name is"<<name<<Endl; }};intMainintargcConst Char*argv) {_test_class Class1,*pointer; Pointer= &Class1; Pointer->age = A; Pointer->name ="Zhouxuewei"; Pointer-display (); return 0;}Accessing members of an object through reference variables of an object
If you define a reference variable for an object that is a total of the same storage unit, they are actually the same object, but they are represented by a different name. Therefore, it is possible to access the members of an object by referencing the variable.
If the time class has been declared, and has the following definition statement:
Time T1; // Defining Objects T1 Time &t2=t1; // define the time class reference variable t2 and initialize it to T1 cout<<t2.hour; // the member hour in the output object T1
Because T2 and T1 share the same storage unit (that is, T2 is the alias of T1), T2.hour is t1.hour.
The following reference code:
#include <iostream>#include<string>using namespacestd;class_test_class{ Public: intAge ; stringname; voiddisplay () {cout<<" Age is"<<age<<Endl; cout<<"name is"<<name<<Endl; }};intMainintargcConst Char*argv) {_test_class Class1; _test_class&display_f =Class1;//Create a reference to an object
22
" Zhouxuewei "
return 0
}
Simple use of classes and objects
#include <iostream>#include<string>using namespacestd;class_test_class{ Public: intAge ; stringname;};voidDisplay2 (_test_class &test) {cout<<" Age is"<<test.age<<Endl; cout<<"name is"<<test.name<<Endl;}intMainintargcConst Char*argv) {_test_class Class1,class2,*pointer1,*Pointer2; Pointer1= &Class1; Pointer1->age = A; Pointer1->name ="Zhouxuewei"; Pointer2= &Class2; Pointer2->age = at; Pointer2->name ="Wangmingxue"; Display2 (Class1); Display2 (Class2); return 0;}
References to C + + Object Members---12