静态数据成员与静态成员函数

一、静态数据成员的实现

       静态数据成员在.h中声明变量,只能在.cpp文件中给这个静态变量赋初值。赋值的时候不能加上static, 但需要加上类名,表明这个静态数据成员属于哪一个类。

    例如:

         .h文件

    public:

     static float m_fArea;            //静态数据成员变量

    .cpp

     float RectangleImp1::m_fArea = 0;         //正确

     static float RectangleImp1::m_fArea = 0;    //错误

     static float  m_fArea = 0;              //错误

     float  RectangleImp1::m_fArea = 0;        //错误

二、静态成员函数的是实现

       静态成员函数在.h中声明变量,在.cpp文件或者也可以在.h文件中定义这个静态成员函数赋。定义的时候不能加上static, 但需要加上类名,表明这个静态成员函数属于哪一个类。
      例如:

    .h文件

    public:

             static IRectangle *  CreateRectangle();

    .h文件或者.cpp文件

     IRectangle * IRectangle::CreateRectangle()      //正确
     {
         IRectangle * pIRectangle = new RectangleImp1;
    
         return pIRectangle;
     }

     static IRectangle * IRectangle::CreateRectangle()   //错误
     {
     }

     static IRectangle * CreateRectangle()          //错误
     {
     }

     IRectangle * IRectangle::CreateRectangle()      //错误
     {
     }

   2.1 在.h中声明,在.cpp中定义

#ifndef _RECTANGLE_H_
#define _RECTANGLE_H_

#include <iostream>
using namespace std;

//抽象接口类
class IRectangle
{
public:

    virtual ~IRectangle()
    {}
    virtual float GetWidth() = 0;
    virtual float GetHigh() = 0;
    virtual void Draw() = 0;
    static IRectangle *  CreateRectangle();
};


//矩形类
class RectangleImp1:public IRectangle
{
    
private:
    float m_fHigh;
    float m_fWidth;
public:
    RectangleImp1():m_fHigh(3),m_fWidth(2)
    {

    }
    virtual ~RectangleImp1()
    {
        
    }

    virtual float GetWidth();
    virtual float GetHigh();
    virtual void Draw();
};
#endif
#include <iostream>
#include "Rectangle.h"
using namespace std;

//静态成员函数
IRectangle * IRectangle::CreateRectangle()
{
    IRectangle * pIRectangle = new RectangleImp1;
    
    return pIRectangle;
}


float RectangleImp1::GetWidth()
{
    return m_fWidth;
}

float  RectangleImp1::GetHigh()
{
    return m_fHigh;
}

void RectangleImp1::Draw()
{
    cout << "我是矩形" << endl;
}

   2.2 在.h中声明,也在.h中定义

 1 #ifndef _RECTANGLE_H_
 2 #define _RECTANGLE_H_
 3 
 4 #include <iostream>
 5 using namespace std;
 6 
 7 //抽象接口类
 8 class IRectangle
 9 {
10 public:
11 
12     virtual ~IRectangle()
13     {}
14     virtual float GetWidth() = 0;
15     virtual float GetHigh() = 0;
16     virtual void Draw() = 0;
17     static IRectangle *  CreateRectangle();
18 };
19 
20 
21 //矩形类
22 class RectangleImp1:public IRectangle
23 {
24     
25 private:
26     float m_fHigh;
27     float m_fWidth;
28 public:
29     RectangleImp1():m_fHigh(3),m_fWidth(2)
30     {
31 
32     }
33     virtual ~RectangleImp1()
34     {
35         
36     }
37 
38     virtual float GetWidth();
39     virtual float GetHigh();
40     virtual void Draw();
41 };
42 
43 //静态成员函数
44 IRectangle * IRectangle::CreateRectangle()
45 {
46     IRectangle * pIRectangle = new RectangleImp1;
47     
48     return pIRectangle;
49 }
50 
51 
52 float RectangleImp1::GetWidth()
53 {
54     return m_fWidth;
55 }
56 
57 float  RectangleImp1::GetHigh()
58 {
59     return m_fHigh;
60 }
61 
62 void RectangleImp1::Draw()
63 {
64     cout << "我是矩形" << endl;
65 }
66 
67 #endif

三、.h文件中既声明了静态数据成员,又声明了静态成员函数。

    .h文件中既声明了静态数据成员,又声明了静态成员函数。则只能在.h中做声明, 在.cpp文件中做定义

#ifndef _RECTANGLE_H_
#define _RECTANGLE_H_

#include <iostream>
using namespace std;

//抽象接口类
class IRectangle
{
public:

    virtual ~IRectangle()
    {}
    virtual float GetWidth() = 0;
    virtual float GetHigh() = 0;
    virtual void Draw() = 0;
    static IRectangle *  CreateRectangle();
    virtual float GetArea() = 0;
};


//矩形类
class RectangleImp1:public IRectangle
{
    
private:
    float m_fHigh;
    float m_fWidth;
public:
    static float m_Area;

    RectangleImp1():m_fHigh(3),m_fWidth(2)
    {

    }
    virtual ~RectangleImp1()
    {
        
    }

    virtual float GetWidth();
    virtual float GetHigh();
    virtual void Draw();
    virtual float GetArea();
};
#endif
#include <iostream>
#include "Rectangle.h"
using namespace std;

//静态数据成员初始化
float RectangleImp1::m_Area = 0;

//静态成员函数
IRectangle * IRectangle::CreateRectangle()
{
    IRectangle * pIRectangle = new RectangleImp1;
    
    return pIRectangle;
}


float RectangleImp1::GetWidth()
{
    return m_fWidth;
}

float  RectangleImp1::GetHigh()
{
    return m_fHigh;
}

void RectangleImp1::Draw()
{
    cout << "我是矩形" << endl;
}

float RectangleImp1::GetArea()
{
    m_Area = m_fHigh * m_fWidth;
    
    return m_Area;
}
原文地址:https://www.cnblogs.com/xydblog/p/3502951.html