The fifth chapter notes --- static data members and static member function

The fifth chapter notes --- static data members and static member function

【background】

Object-oriented design methods compatible with the protection and sharing of data, static members proposed to solve the problem of sharing data between different objects of. For example, to count the number of personnel, number of occurrences, etc., we need to use a static process.

【definition】

Static member, referring to the declaration can add static keyword in c ++ class members, the members of this statement is called static members (including static data members and static member functions).

Static data members

[Format] Statement

static 数据类型 数据成员名;

[Description]

  • In c ++, the static data members belong to the class, once a data member declared as static data members, each object of the class can access it.
  • Why use static data members instead of global variables? - because the global variables will bring insecurity, and undermine the information hidden object-oriented programming and encapsulation features.
  • In a class, no matter how many objects are created, only one is a copy of static data members, enabling data sharing between different objects of a class.
  • Initialize static data members must be initialized separately outside the class, and define an object prior to the initialization.
    Data type class name :: static data member name = initial value;
  • Static data members belong to the class, not part of an object.

    Code verification [5-4] [book]

    [A] point with a static data member count ++, output 1 when the first call, second call 2 output

    #include<iostream>
    using namespace std;
    class point {
        public:
            point(int x = 0, int y = 0) :x(x), y(y) {
                count++;
            }
            point(point& p) { //复制构造函数
                x = p.x;
                y = p.y;
                count++;
            }
            ~point() {  //析构函数
                count--;
            }
            void getX() {
                cout << x;
            }
            void getY() {
                cout << y;
            }
            void showcount() {
                cout << "count=" << count << endl;
            }

        private:
            int x, y;
            static int count;//声明静态数据成员
    };

    int point::count = 0;//用类名限定静态成员定义和初始化 
    int main() {
        point a(1, 1);
        a.showcount();
        point b(a);
        b.showcount();
        return 0;
    }

[Run] Screenshot

Static member function

[Format] Statement

static 返回类型 静态成员函数名 (实参表);

[Format] calls

类名::静态成员函数名(实参表); //----1
对象.静态成员函数名(实参表);   //----2
对象指针->静态成员函数名(实参表);//----3

[Description]

  • Static member function belongs to the class, not part of a class of objects, shared by all such objects
  • Private static class member functions can not be accessed outside of functions and objects
  • The reason for using a static member function that can use it to call the static member function before the establishment of any object, so static data member for processing, but the function is ordinary member functions can not do.
  • The difference between static member functions and non-static member functions are: static member function belongs to the class, no this pointer, static member function rather than an object belonging to this class, there is this pointer to the object.
  • Static member functions can be accessed through the object or outside the class by class to access directly, rather than static member functions can only be accessed by objects outside the class.
     类名::静态成员函数名();//在类外直接访问静态成员函数
  • Static member functions can only access static data members of the class vs non-static member functions can access static data members of the class with non-static data members.

Code verification [5-5] [book]

[A]

#include<iostream>
using namespace std;

class Point {
public:
    Point(int x = 0, int y = 0) :x(x), y(y) {//构造函数
        count++;
    }
    Point(Point& p) {  //复制构造函数
        x = p.x;
        y = p.y;
        count++;
    }
    ~Point() { count--; }

    int getX() { return x; }

    int getY() { return y; }

    static void showcount() {  //静态成员函数
        cout << "object count:"<<count;
    }
private:
    int x;
    int y;
    static int count;
};
int Point::count = 0;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

int main() {
    Point a(4, 5);
    cout <<"Point a:"<< a.getX ()<< " " << a.getY()<<endl;
    Point::showcount();
    cout << endl;
    Point b(a);
    cout << "Point b:" << a.getX() << " " << a.getY() << endl;
    Point::showcount();
    return 0;

}

[Run] Screenshot

[Practice]

Description [title]

Please write a program to calculate the date of the total sales of this product shall sum and the average selling price of each item. Requirements for static data members and static member functions.
(Hint: The discount discount, total sales and merchandising money sum total number of pieces n declared as static data members, and then define a static member function average (averaging selling price) and display (output)
[Code]

#include
  
  
   
   
using namespace std;
class Product {
public: void set_value(); //设置商品的price和单次销售数量num
void total(); //计算总销售额sum和销售总量total_num
static void ave_count(); //计算平均商品价格
static void display(); // 显示sum,total_num和 average
private:
int num;
double price;
static double discount; //折扣
static int total_num; //总销量
static double sum; //总销售额
static double average; //平均价格
};

/数据初始化/
double Product::discount=0.98;
int Product::total_num=0;
double Product::sum=0;
double Product::average=0;

void Product::set_value(){
cout << "输入销量num和产品价格price:";
cin>>num>>price;
}
void Product::total(){
total_num+=num;
if(num>10)
sum+=numpricediscount;
else
sum+=num*price;
}
void Product::ave_count(){
average=sum/total_num;
}void Product::display(){
cout<<"num="<<total_num<<endl <<"sum="<<sum<<endl <<"average="<<average<<endl;
}

int main(){
const int t=3;
Product p[t];
for(int i=0;i<t;i++){
p[i].set_value();
p[i].total();
}
Product::ave_count();
Product::display();
return 0;
}


[Run] Screenshot

Guess you like

Origin www.cnblogs.com/TSmeredithH/p/11610069.html