静态成员函数,静态成员数据,常成员
来源:互联网 发布:郑州富士康网络招聘 编辑:程序博客网 时间:2024/05/28 06:05
#include<iostream>
using namespace std;
/*
为什么要使用静态成员变量,如果使用全局变量的话,太危险,容易出错。
*/
#if 0
int count;
class student
{
public:
student():m_count(0){m_count++;count++;}
void print(){cout<<m_count<<","<<count<<endl;}
private:
int m_count;
};
int main()
{
student a,b,c,d,e;
a.print();
b.print();
return 0;
}
#endif
#if 0
/*
静态成员变量:所有成员可以调用,并且受保护。类本身不占空间。
*/
class student
{
public:
student(){m_count++;}
void print(){cout<<m_count<<endl;}
private:
static int m_count;
};
int student::m_count;
int main()
{
student a,b,c,d,e;
a.print();
b.print();
c.print();
d.print();
e.print();
}
#endif
/*
a.h a.cpp test.cpp
静态成员的初始化一般放在a.cpp当中
*/
#if 0
class A
{
public:
A(){m_i++;cout<<m_i<<endl;}
void print(){cout<<m_i<<endl;}
static void test(A &a)
{
m_i = 6;
a.m_j = 9;
}//静态成员函数中不能直接访问非静态成员
//静态成员函数没有this指针,不能直接访问非静态成员,非静态成员函数里面可以访问静态成员或非静态成员。
private:
static int m_i;
int m_j;
};
int A::m_i;
int main()
{
A a,b,c,d;
a.print();
a.test(b);
b.print();
return 0;
};
#endif
#if 0
class B;
class A
{
public:
A(int i = 0):m_i(i){}
friend void fn(A &a,B &b);
private:
int m_i;
};
class B
{
public:
B(int j = 0):m_j(j){}
friend void fn(A &a,B &b);
private:
int m_j;
};
void fn(A &a,B &b)
{
cout<<a.m_i+b.m_j<<endl;
}
int main()
{
A a(5);
B b(7);
fn(a,b);
}
#endif
/*
友元不是类的成员函数(只是朋友),不能在类里面定义
友元关系是单向的
友元关系不能继承
*/
#if 0
class B;
class A
{
public:
A(int i = 0):m_i(i){}
void fn(B &b);
private:
int m_i;
};
class B
{
public:
B(int j = 0):m_j(j){}
friend void A::fn(B &b);
private:
int m_j;
};
void A::fn(B &b)
{
cout<<m_i + b.m_j<<endl;
}
int main()
{
A a(4);
B b(3);
a.fn(b);
return 0;
}
#endif
using namespace std;
/*
为什么要使用静态成员变量,如果使用全局变量的话,太危险,容易出错。
*/
#if 0
int count;
class student
{
public:
student():m_count(0){m_count++;count++;}
void print(){cout<<m_count<<","<<count<<endl;}
private:
int m_count;
};
int main()
{
student a,b,c,d,e;
a.print();
b.print();
return 0;
}
#endif
#if 0
/*
静态成员变量:所有成员可以调用,并且受保护。类本身不占空间。
*/
class student
{
public:
student(){m_count++;}
void print(){cout<<m_count<<endl;}
private:
static int m_count;
};
int student::m_count;
int main()
{
student a,b,c,d,e;
a.print();
b.print();
c.print();
d.print();
e.print();
}
#endif
/*
a.h a.cpp test.cpp
静态成员的初始化一般放在a.cpp当中
*/
#if 0
class A
{
public:
A(){m_i++;cout<<m_i<<endl;}
void print(){cout<<m_i<<endl;}
static void test(A &a)
{
m_i = 6;
a.m_j = 9;
}//静态成员函数中不能直接访问非静态成员
//静态成员函数没有this指针,不能直接访问非静态成员,非静态成员函数里面可以访问静态成员或非静态成员。
private:
static int m_i;
int m_j;
};
int A::m_i;
int main()
{
A a,b,c,d;
a.print();
a.test(b);
b.print();
return 0;
};
#endif
#if 0
class B;
class A
{
public:
A(int i = 0):m_i(i){}
friend void fn(A &a,B &b);
private:
int m_i;
};
class B
{
public:
B(int j = 0):m_j(j){}
friend void fn(A &a,B &b);
private:
int m_j;
};
void fn(A &a,B &b)
{
cout<<a.m_i+b.m_j<<endl;
}
int main()
{
A a(5);
B b(7);
fn(a,b);
}
#endif
/*
友元不是类的成员函数(只是朋友),不能在类里面定义
友元关系是单向的
友元关系不能继承
*/
#if 0
class B;
class A
{
public:
A(int i = 0):m_i(i){}
void fn(B &b);
private:
int m_i;
};
class B
{
public:
B(int j = 0):m_j(j){}
friend void A::fn(B &b);
private:
int m_j;
};
void A::fn(B &b)
{
cout<<m_i + b.m_j<<endl;
}
int main()
{
A a(4);
B b(3);
a.fn(b);
return 0;
}
#endif
0 0
- 静态成员函数,静态成员数据,常成员
- 常对象及常成员及静态数据成员和静态成员函数的学习内容。
- 静态成员函数与静态数据成员
- 静态成员函数 与 静态数据成员
- static 静态数据成员 静态成员函数
- 静态数据成员和静态成员函数
- 静态成员函数与静态数据成员
- 静态数据成员和静态成员函数
- C++ 静态数据成员 静态成员函数
- 静态数据成员与静态成员函数
- 静态成员数据和静态成员函数
- 静态数据成员和静态成员函数
- 静态数据成员及静态成员函数
- 静态成员函数,静态数据成员
- 静态数据成员和静态成员函数
- 静态数据成员和静态成员函数
- 静态数据成员和静态成员函数
- 静态数据成员和静态成员函数
- 把数值转换成中文数字(练习)
- annimate
- Java基础-IO学习之字符流
- NetBeans快捷键
- [POJ 2774] Long Long Message (字符串哈希+二分)
- 静态成员函数,静态成员数据,常成员
- 压测xx业务数据库资源大量等待,存在表锁问题,导致数据库无法正常执行解决办法
- 关于使用J-flash将程序下载到stm32f407ig中遇到的问题
- Poedu_计算机基础_复习_20160828
- 【noip】Mayan游戏 搜索
- Jmeter使用奇葩问题集锦
- LINUX IO 程序编程
- idea设置注释作者信息
- 使用setInterval()方法输出一个计时器