static类型的局部变量,生存期在整个程序,局部可见。
void example()
{
static a=1;
int b=2
}
当调用完example函数后,b消亡,a仍然存在;
再次调用该函数时,a值不会再次被赋值,仍保持之前数据;而b需要重新建立,重新赋值。
1.用关键字static声明;
2.为该类的所有对象共享,静态数据成员具有静态生存期;
3.必须在类外定义和初始化,用(::)来指明所属的类.
这个数据属于类的信息,比如统计教师群体中教师的总数,即这个类实例化了多少个对象。
class A{
static int num; //在类内声明
}
int A::num=0; //在类外定义和初始化
1.类外代码可以使用类名和作用域操作符来调用静态成员函数;
2.静态成员函数主要用于处理该类的静态数据成员,可以直接调用静态成员函数;
3.如果访问非静态成员,要通过对象来访问。
class A{
private:
static int num;
int a=1;
public:
static void shownum(){cout<< num << endl;} //2.
static int change(int a);//一个定义的例子;
}
int A::num=0;
int A::change(int a){
A::num=a; //静态数据成员的引用需要加 “类::”来修饰
}
int main(){
A:shownum(); // 1.
}
友元是C++提供的一种破坏数据封装和数据隐藏的机制。
友元函数
在类声明中由关键字friend修饰说明的非成员函数,在它的函数体中能
够通过对象名访问 private 和 protected成员
class Point {
public:
Point(int x=0, int y=0) : x(x), y(y) { }
int getX() { return x; }
int getY() { return y; }
friend float dist(Point &a, Point &b);
private:
int x, y;
};
float dist( Point& a, Point& b) //非成员函数
{ double x = a.x - b.x; //通过对象名访问私有成员
double y = a.y - b.y;return static_cast<float>(sqrt(x * x + y * y));
}
int main(){
Point p1(1, 1), p2(4, 5);
cout <<"The distance is: ";
cout << dist(p1, p2) << endl;
return 0;
}
友元类
friend声明是单向授权声明,类A是类B的友元,意味着类A获得了访问类B的私有成员的权限,但类B不能反向访问类A的私有成员;
此处的friend更像是一种"上级声明"
class A {
friend class B;
public:
void display() {cout << x << endl;}
private:
int x;
};
class B {
public:
void set(int i);
void display();
private:
A a; //B类中含有A类成员,正常来说是看不到封装的私有成员的
};
void B::set(int i) { a.x=i; } //因为是友元,所以可以通过对象名访问私有成员
void B::display() { a.display(); }
const 类名 对象名 const A a(1,2);
const 类型说明符 &引用名 const int &p;const A &p;
类型说明符 const 数组名[大小]...
常指针:指向常量的指针(详见第6章)。
a[10]
数组名 a 是一个地址(指针)类型常量,对应数组首元素的内存地址,不能被赋值
数组名作函数参数
void rowSum(int a[][4], int nRow)
{
for (int i = 0; i < nRow; i++)
{
for(int j = 1; j < 4; j++)
a[i][0] += a[i][j];
}
}
int table[3][4] = {{1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}};
rowSum(table, 3);
Point a[3];
实例化3个Point类的对象,调用3次构造和3次析构函数;
double prices[5]={4.99, 2.33, 5.86, 6.42, 4.51};
for (double x : prices)
cout<<x<<std::endl;
清华大学《C++语言程序设计基础》线上课程笔记03---数据的共享和保护&数组
原文:https://www.cnblogs.com/j-c-y/p/9819227.html