20.静态成员变量
程序1:
#pragma warning(disable:4996)
//2022年9月21日19:20:29
#include <iostream>
using namespace std;
class Maker
{
public:
Maker()
{
a = 20;
}
public:
//1.生命周期是整个程序,作用域在类内
static int a;
};
void test01()
{
Maker m;
}
int main()
{
system("pause");
return EXIT_SUCCESS;
}
输出结果:(静态成员变量要在类内声明,类外初始化)
程序2:
#pragma warning(disable:4996)
//2022年9月21日19:20:29
#include <iostream>
using namespace std;
class Maker
{
public:
Maker()
{
}
public:
//1.生命周期是整个程序,作用域在类内
static int a;
};
//2.静态成员变量要在类内声明,类外初始化
int Maker::a = 100;
void test01()
{
Maker m;
cout << m.a << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
输出结果:
100
请按任意键继续. . .
程序3:
#pragma warning(disable:4996)
//2022年9月21日19:20:29
#include <iostream>
using namespace std;
class Maker
{
public:
Maker()
{
}
public:
//1.生命周期是整个程序,作用域在类内
static int a;
};
//2.静态成员变量要在类内声明,类外初始化
int Maker::a = 100;
void test01()
{
//3.静态成员变量属于类内,不属于对象,是所有对象共享
cout << Maker::a << endl;
Maker m;
cout << m.a << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
输出结果:
100
100
请按任意键继续. . .
程序4:
#pragma warning(disable:4996)
//2022年9月21日19:20:29
#include <iostream>
using namespace std;
class Maker
{
public:
Maker()
{
}
public:
//1.生命周期是整个程序,作用域在类内
static int a;
};
//2.静态成员变量要在类内声明,类外初始化
int Maker::a = 100;
void test01()
{
//3.静态成员变量属于类内,不属于对象,是所有对象共享
cout << Maker::a << endl;
Maker m;
cout << m.a << endl;
//4.静态成员变量可以用类访问,也可以用对象访问
}
class Maker2
{
public:
Maker2()
{
}
//静态成员函数只能访问静态成员变量
static void setA(int a)
{
a2 = a;
cout << "a2 = " << a2 << endl;
//a1 = a;//err不能访问普通成员变量
}
public:
int a1;
static int a2;
};
int Maker2::a2 = 200;
void test02()
{
Maker2::setA(300);
}
int main()
{
test02();
system("pause");
return EXIT_SUCCESS;
}
输出结果:
a2 = 300
请按任意键继续. . .
程序5:
#pragma warning(disable:4996)
//2022年9月21日19:20:29
#include <iostream>
using namespace std;
class Maker
{
public:
Maker()
{
}
public:
//1.生命周期是整个程序,作用域在类内
static int a;
};
//2.静态成员变量要在类内声明,类外初始化
int Maker::a = 100;
void test01()
{
//3.静态成员变量属于类内,不属于对象,是所有对象共享
cout << Maker::a << endl;
Maker m;
cout << m.a << endl;
//4.静态成员变量可以用类访问,也可以用对象访问
}
class Maker2
{
public:
Maker2()
{
}
//静态成员函数只能访问静态成员变量
static void setA(int a)
{
a2 = a;
cout << "a2 = " << a2 << endl;
//a1 = a;//err不能访问普通成员变量
}
public:
int a1;
static int a2;
};
int Maker2::a2 = 200;
void test02()
{
Maker2::setA(300);
}
class Maker3
{
private:
static int a3;
public:
static void func()
{
cout << "a3 = " << a3 << endl;
}
};
int Maker3::a3 = 300;
void test03()
{
Maker3::func();
}
//const修饰的成员变量最好在类内初始化
class Maker4
{
public:
const static int a = 20;
const static int b;
};
//类外也可以初始化
const int Maker4::b = 30;
int main()
{
test03();
system("pause");
return EXIT_SUCCESS;
}
输出结果:
a2 = 300
请按任意键继续. . .
程序6:
#pragma warning(disable:4996)
//2022年9月21日19:20:29
#include <iostream>
using namespace std;
class Maker
{
public:
Maker()
{
}
public:
//1.生命周期是整个程序,作用域在类内
static int a;
};
//2.静态成员变量要在类内声明,类外初始化
int Maker::a = 100;
void test01()
{
//3.静态成员变量属于类内,不属于对象,是所有对象共享
cout << Maker::a << endl;
Maker m;
cout << m.a << endl;
//4.静态成员变量可以用类访问,也可以用对象访问
}
class Maker2
{
public:
Maker2()
{
}
//静态成员函数只能访问静态成员变量
static void setA(int a)
{
a2 = a;
cout << "a2 = " << a2 << endl;
//a1 = a;//err不能访问普通成员变量
}
public:
int a1;
static int a2;
};
int Maker2::a2 = 200;
void test02()
{
Maker2::setA(300);
}
class Maker3
{
private:
static int a3;
public:
static void func()
{
cout << "a3 = " << a3 << endl;
}
};
int Maker3::a3 = 300;
void test03()
{
Maker3::func();
}
//const修饰的成员变量最好在类内初始化
class Maker4
{
public:
const static int a = 20;
const static int b;
};
//类外也可以初始化
const int Maker4::b = 30;
//const修饰的静态成员变量最好在类内初始化
class Maker5
{
public:
void func()
{
cout << "a = " << a << endl;
}
public:
static int a;
};
int Maker5::a = 30;
void test04()
{
Maker5 m;
m.func();
//Maker5::func();//err普通成员函数 a是静态成员变量
}
int main()
{
test04();
system("pause");
return EXIT_SUCCESS;
}
输出结果:
a3 = 30
请按任意键继续. . .
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)