The object of a class acts as a data member of another class.
A data member in a class can be an object of a class in addition to the basic data types such as int, char, float, and so on. Creates a new class with child objects.
In C + +, when the object of a class is used as the data member of the new class, the definition format of the new class can be expressed as:
Class X
{class Name 1 Member name 1;
Class Name 2 member Name 2;
......
Class name n member name N;
.../other Members
};
(3) If an object of Class A is a data member of another Class B, during the object creation of Class B, the data member (object of Class A) automatically invokes the constructor of Class A in the process of invoking its constructor.
Note, however, that if the constructor of Class A is a parameter function, you must add a ":" and the constructor of the called Class A In your program, and the argument value when you call Class A's constructor must come from the formal parameter in the formal parameter list of Class B. This method calls the constructor in a way that is called the initialization table. For example, with the class X defined above, when initializing an object of Class X, you must first initialize the child objects in it, that is, you must first call the constructors of those child objects. Therefore, the constructor for class X should be defined in the format:
X::x (parameter Table 0): member 1 (parameter Table 1), member 2 (parameter Table 2), ..., member n (parameter table N)
{ ......}
Where parameter Table 1 provides the parameters required to initialize member 1, Parameter Table 2 provides the parameters required to initialize member 2, and so on. and the parameters in each of these parameter tables come from parameter table 0, and the parameters required to initialize the non-object members of X are also provided by parameter table 0.
In the process of constructing an object for a new class, the system first invokes its child object's constructor, initializes the child object, and then executes the class X's own constructor, initializing the non-object member in the class. For different child objects in the same class, the system initializes the corresponding constructors according to the order in which they are described in the class, rather than in the order of the initialization table.
The following defines three student, teacher, and Tourpair, where the objects of the student class and the object of the Teacher Class act as data members of the Tourpair, observing the process of the object's construction and the order in which the constructor was executed.
#include <iostream.h>
Class Student
{public:
Student ()
{cout<< "construct student.\n";
semeshours=100;
gpa=3.5; }
Protected
int semeshours;
float GPA;
};
Class Teacher
{public:
Teacher ()
{cout<< "construct teacher.\n";
}
};
Class Tourpair
{public:
Tourpair ()
{cout<< "Construct tourpair.\n";
nomeeting=0; }
Protected
Student Student;
Teacher Teacher;
int nomeeting;
};
void Main ()
{Tourpair TP;
cout<< "Back in main.\n"; }
Its implementation results are:
Construct student.
Construct teacher.
Construct Tourpair.
Back in Main.
This shows: when the main function main () runs, it encounters an object to create the Tourpair class, and then calls its constructor Tourpair (), which first allocates the object space (including a student pair
Like, a teacher object, and an int data), and then call its constructors in the order of the members of the objects declared in the class. That is, the student () constructor is called first, then the teacher () constructor is invoked, and then the function body of its own constructor is executed.
Because the data members of the Tourpair class in the previous example student and teacher constructors are parameterless functions, the system automatically invokes the respective constructors teacher () and student () when constructing student and teacher objects. It does not need to be invoked in the form of an initialization table.
"Example 3-7" tries to analyze the results of the following programs:
#include <iostream.h>
#include <string.h>
Class Student
{public:
Student (char *pname= "No name")
{cout<< "Construction new classmate:" <<pName<<endl;
strncpy (name,pname,sizeof (name)); char * strncpy (char *dest, char *src, size_t N); Copy the maximum n characters in the string src to the character array dest (it does not have to be like strcpy to stop copying, but wait until you get enough n characters to start copying), and return a pointer to Dest. Determining data type length characters in C language
Name[sizeof (name) -1]= ';
}
Student (Student &s)
{cout<< "construct copy of" <<s.name<<endl;
strcpy (name, "copy of"); extern Char *strcpy (char *dest,char *src); Copy the null-terminated string from SRC to the array dest refers to
strcat (Name,s.name); extern Char *strcat (char *dest,char *src); Add the string src refers to the end of the dest (cover the ' dest ' at the end of the ending) and add ' the '.
}
~student ()
{cout<< "destructor" <<name<<endl;}
Protected
Char name[40]; };
Class Tutor
{public:
Tutor (Student &s): Student (s)//This is the initialization table, calling the
Copy Constructors for student
{cout<< "construction instructor \ n"; }
Protected
Student Student;
};
void Main ()
{Student st1; The constructor of the student is called Here Student (Char
*pname= "No name")
Student st2 ("Zhang"); Ditto
Tutor Tutor (ST2); The constructor of the tutor is called here Tutor (Student &s)
In the process of constructing the Tutor object, it is called with the initialization table
Copy constructors for Student classes Student (Student &s)
}
The results of the implementation are as follows:
Construct new classmate: No name
Construction new classmate: Zhang
Construct copy of Zhang
Structural Guidance Teachers
Deconstruction copy of Zhang
An analysis of Zhang
Destructors No Name
3.2.7 Use an initialization table to provide an initial value to a constant data member or reference member
As mentioned earlier, constructors can initialize data members of an object, but they are different if the data member is a constant member or a reference member, such as:
Class Sillyclass
{public:
Sillyclass ()//This constructor initializes errors for members ten and refi.
{ten=10;
Refi=i; }
Protected
const int Ten; Constant data member Ten
int &refi; Referencing refi
};
Description
1. The reason for the above error is that after the constructor of the Sillyclass class has entered (when the function body begins to execute), the object structure has been established, the data member ten and refi already exist, and its data members ten to Const and refi as references. Therefore, no new value can be assigned to it in the constructor body.
2. The solution to the above problem is to use the initialization table: After the parentheses of the constructor, add a ":" and initialize the table, the format of the initialization table is:
The name of the data member (value), and if there are more than one, you need to separate it with commas.
The example 3-8 class employee includes private data member X, and 2 Public Function members example, show. The initialization table used in the program is X (215).
# include <windows.h>
# include <iostream.h>
Define class employee
Class Employee
{private:
const int x;
Public
Employee ();
void Show ();
};
Outside class definition of employee
Employee:: Employee (): X (215)//initialization table
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.