C++类的静态成员及应用实例

C++类的静态成员

定义:在C++类中声明成员时加上static关键字声明的成员。
【注意】声明为static的类成员或者类函数可以在类的范围内共享。

静态数据成员

部分资料来自百度百科
静态数据成员与普通数据成员的区别:
①.普通数据成员属于类的一个具体对象,只有对象被创建了,普通数据成员才会被分配内存;而静态数据成员属于整个类,即使没有创建对象,类的静态数据成员变量也存在。
②.因为类的静态数据成员的存在不依赖于任何类对象的存在,类的静态数据成员应该在代码中被显式初始化。

【注意】
1.用关键字static声明
2.静态数据成员为该类的所有对象共享,因此具有静态生存期
3.必须在类外定义和初始化,用(::)来指明所属的类

静态成员函数

静态成员函数与普通函数成员的区别:
①.外部访问类的静态函数成员可以直接通过类名进行访问,如CStudent::getcount()。虽然静态成员不属于类的某个对象,但我们仍然可以使用类的对象、指针来访问静态成员。
②.类的静态成员函数无法直接访问普通数据成员,而类的任何成员函数都可以访问类的静态数据成员。

【注意】
1.类外可以使用类名和作用域操作符来调用静态成员函数
2.静态成员函数只能引用属于该类的静态数据成员或静态成员函数

实例分析

【问题描述】

编写CStudent类,并建立对象数组,分析对象的创建与释放过程中构造函数与析构函数的调用。
具体要求:
1、CStudent类包括两个私有成员:姓名和年龄
2、CStudent类具有一个静态数据成员count,在对象被创建时自动加1,在对象被释放时自动减1
3、在构造函数中输出"*** is contructing", 在析构函数中输出“*** is destructing”
4、请根据main函数分析其他功能,并完成CStudent类的设计
5、请分析块作用域的对象创建与释放的原因

【样例输出】
现在有学生0
noname is contructing
noname is contructing
noname is contructing
noname is contructing
noname is contructing
name:noname
现在有学生5
noname is destructing
noname is destructing
noname is destructing
noname is destructing
noname is destructing
现在有学生0
Tom is contructing
Jerry is contructing
name:Tom
name:Jerry
现在有学生2
Jerry is destructing
Tom is destructing
现在有学生0

已给出main函数如下:

int main()
{
    cout<<"现在有学生"<<CStudent::getcount()<<endl;
    {
        CStudent stuB[5];
        stuB[4].printname();
        cout<<"现在有学生"<<CStudent::getcount()<<endl;
    }
    cout<<"现在有学生"<<CStudent::getcount()<<endl;
    {
        CStudent stuA[2] = {CStudent("Tom",3),CStudent("Jerry",2)};
        for (int i = 0; i < 2;i++)
        {
            stuA[i].printname();
        }
        cout<<"现在有学生"<<CStudent::getcount()<<endl;
    }
    cout<<"现在有学生"<<CStudent::getcount()<<  endl;
    return  0;
}

C++代码实现

#include <iostream>
#include <string>
using namespace std;
class CStudent
{
private:
    string name;
    int age;
    static int Count;//声明了Count变量但未分配内存
public:
    CStudent()//默认构造函数
    {
        cout<<"noname is contructing"<<endl;//根据题目要求输出
        name = "noname";
        age = 0;
        Count++;
    }
    CStudent(const std::string &s,int a)//构造函数
    {
        name = s;
        age = a;
        cout<<name<<" is contructing"<<endl;
        Count++;
    }
    ~CStudent()//析构函数
    {
        cout<<name<<" is destructing"<<endl;
        Count--;
    }
    void printname(){cout<<"name:"<<name<<endl;}//按题目要求输出姓名
    static int getcount(){return Count;}//返回学生个数
};
int CStudent::Count = 0;//定义静态成员变量,同时初始化为0
int main()
{
    cout<<"现在有学生"<<CStudent::getcount()<<endl;
    {
        CStudent stuB[5];
        stuB[4].printname();
        cout<<"现在有学生"<<CStudent::getcount()<<endl;
    }
    cout<<"现在有学生"<<CStudent::getcount()<<endl;
    {
        CStudent stuA[2] = {CStudent("Tom",3),CStudent("Jerry",2)};
        for (int i = 0; i < 2;i++)
        {
            stuA[i].printname();
        }
        cout<<"现在有学生"<<CStudent::getcount()<<endl;
    }
    cout<<"现在有学生"<<CStudent::getcount()<<  endl;
    return  0;
}

运行结果为:
在这里插入图片描述
本篇到此就结束了,如有错误,欢迎大家及时批评指正,谢谢大家!

发布了20 篇原创文章 · 获赞 16 · 访问量 1288

猜你喜欢

转载自blog.csdn.net/SAMSARS/article/details/105274145