8.11 Static Members
Use the keyword static. Initializing static member data must be done outside the class .
8.11.1 static member data
It is a class of objects that share data members, not just member data for an object. For example, class names and class sizes can be defined as static member data for student classes.
It differs from general member data in that for static member data, each object of the class shares unique data, that is, it only has one copy, and for general member data, each object of the class independently establishes its own copy to hold its own specific value.
The static member data int student::count=0 is defined in the Student class and cannot be initialized at the same time when static member data is defined, such as the static int count=0 is wrong.
8.11.2 static member functions
It is also known as a class member function.
Defines a static member function, PrintCount (), that accesses static member data in the function count. Accessing public static member data from the outside of the class is available in two ways:
The name of the object. public static member function;
Class Name:: public static member function;
The corresponding code for each is
Stu.printcount ();
Student::p rintcount ();
The member functions inside the class and the member data are accessed in relation to each other:
General member functions can access any type of function and data, while static member functions can access only static member functions and data.
8.12 Friends
Static member data provides a mechanism for sharing between all objects of the same class, while a friend is a mechanism for sharing data between member functions of different classes, member functions of classes, and general functions. The introduction of friends destroys the encapsulation and concealment of class data, so the use of friend mechanism is not advocated in object-oriented program design method.
It can be defined at the function level, or at the function class level.
8.12.1 friend function
Define a friend function, using the keyword friend. The General function printstudent () is declared as a friend function of the class student, and the function can access the private member id,name&age of the class student, Printstudent (STU), declaring an object first student Stu (1, "Li", ' F ');
8.12.2 Friend class
If all member functions of a class need to be declared as a friend function of another class, the practice of the friend function becomes cumbersome. A simpler approach is to declare a friend class, declaring a class as a friend function of another class. For example:
Class Administrator
{....};
Class Student
{
Friend class Administrator;
};
Example:
#include <iostream> #include <cstring>using namespace Std;class administrator{public:void createstudent ( int pId);} ; class student{public:student (int pid,char* pname,char psex); ~student (); friend void Printstudent (student& pstudent); friend void administrator::createstudent (int pId);p rivate:int id;char* name;char sex; };void administrator::createstudent (int pId) {Student stu (1, "Wang", ' M '); stu.id=pid;//name=new Char[strlen (pName) +1] ; cout<< "ID:" <<stu.id<< "," << "Name:" <<stu.name<< "," << "Sex:" << Stu.sex<<endl;} student::student (int pid,char* pname,char psex) {cout<< "construct one Student." <<endl;id=pid;sex=psex;name=new Char[strlen (pName) +1];if (name!=0) strcpy (name,pname);} Student::~student () {cout<< "Deconstuct one Student." <<endl;delete[] name; void Printstudent (student& pstudent) {cout<< "ID:" <<pStudent.id<< "," << "Name:" << pstudent.name<< "," << "Sex:" <<psTudent.sex<<endl;} int main () {Student stu (1, "Li", ' F ');p rintstudent (Stu); Administrator admin;admin.createstudent (2); return 0;} The General function printstudent () is declared as a friend function of the class student, and the function can access the private member of the class student Id,name&age
Static members and friends in a class