c++学习-特殊类成员
静态变量:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class A{ public: A(){ total++; }
static int total; }; //@warn 静态成员变量必须在全局进行定义
int A::total = ; void main()
{
A a1;
A a2; cout << a1.total<< endl;
cout << a2.total<< endl;
cout << A::total << endl; }
静态成员函数:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class A{ public:
A(){ total++; }
static void show()
{
cout << total << endl;
} void echo()
{
cout << total << endl;
} private:
static int total;
int a;
}; int A::total = ; void main()
{
A::show(); A a1;
a1.echo();
A a1,a2;
a1.echo();
a1.show();//也可以通过对象调用静态函数
}
继承后静态成员函数和方法仍旧可用:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class A{ public:
A(){ total++; }
static void show()
{
cout << total << endl;
} void echo()
{
cout << total << endl;
} private:
static int total;
int a;
}; class B :public A{ }; int A::total = ; void main()
{
A::show();
B::show(); }
静态成员函数不能说明为虚函数
int(*fun)(int);//声明一个函数指针,指向的函数的参数为int,返回值为int
int*fun(int);//声明一个函数,函数的参数为int,返回值为int型指针
函数指针使用:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; int test1(int x,int y){
return x + y;
} int test2(int x, int y){
return x * y;
} void main()
{
int(*p)(int, int);
p = test1;
cout << p(, )<< endl; p = test2;
cout << p(, ) << endl; }
函数指针数组:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; int test1(int x,int y){
return x + y;
} int test2(int x, int y){
return x * y;
} void main()
{
int(*p[])(int, int) = { test1, test2 }; cout << p[](,) << endl;
cout << p[](, ) << endl; }
函数指针做参数:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; int test1(int x,int y){
return x + y;
} int test2(int x, int y){
return x * y;
} void show(int(*p)(int, int), int x,int y)
{
cout << p(x,y) << endl;
} void main()
{
show(test1, ,);
show(test2, , );
}
typedef来简化定义:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; typedef int(*p)(int, int); // p代表 int(*p)(int, int); int test1(int x,int y){
return x + y;
} int test2(int x, int y){
return x * y;
} void show(p vp, int x,int y)
{
cout << vp(x, y) << endl;
} void main()
{
show(test1, ,);
show(test2, , );
}
成员函数指针:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class A{
public:
int show(int x, int y){ return x*y; }
}; int(A::*p)(int, int); void main()
{
p = &A::show; A a1;
(a1.*p)(, ); //调用 }
多态和成员函数指针:
#include<iostream>
#include<string>
#include <typeinfo>
using namespace std; class human{
public:
virtual void say() = ;
}; class father :public human{
public:
void say(){ cout << "father say:" << endl; }
}; class mother :public human{
public:
void say(){ cout << "mother say:" << endl; }
}; void main()
{
void (human::*fun)()=;
fun = &human::say; human *p;
p = new father;
(p->*fun)(); p = new mother;
(p->*fun)(); }
c++学习-特殊类成员的更多相关文章
- C++学习46 getline()函数读入一行字符 一些与输入有关的istream类成员函数
getline函数的作用是从输入流中读取一行字符,其用法与带3个参数的get函数类似.即 cin.getline(字符数组(或字符指针), 字符个数n, 终止标志字符) [例13.7] 用get ...
- C++学习之路—继承与派生(一):基本概念与基类成员的访问属性
(本文根据<c++程序设计>(谭浩强)总结而成,整理者:华科小涛@http://www.cnblogs.com/hust-ghtao,转载请注明) 1 基本思想与概念 在传统的程序设计 ...
- python学习day20 面向对象(二)类成员&成员修饰符
1.成员 类成员 类变量 绑定方法 类方法 静态方法 属性 实例成员(对象) 实例变量 1.1实例变量 类实例化后的对象内部的变量 1.2类变量 类中的变量,写在类的下一级和方法同一级. 访问方法: ...
- 【c# 学习笔记】使用新成员隐藏基类成员
如果想在派生类中定义与基类成员同名的成员,则可以使用new关键字把基类成员隐藏起来. 如果不适应new关键字,在派生类中定义一个与基类成员同名的成员,编译器将产生警告信息,如下代码演示: public ...
- Java学习之多态---类成员变化
类成员 一.成员变量 编译时:变量(f)所属类(Fu)中是否有成员变量,有:编译成功,没有:编译失败 运行时:变量(f)所属类(Fu)中是否有成员变量,运行该类(Fu)中的成员变量 class Fu ...
- python2学习------基础语法3(类、类的继承、类成员函数、防御式编程)
1.类的定义以及实例化 # 类定义 class p: """ this is a basic class """ basicInfo={&q ...
- 再次学习C++类之构造函数
学习C++类,首先要说C中的结构体,虽然C++类扩展了C中的结构体,可以添加成员函数,但他们是有区别的.在结构体中,成员变量.成员函数都是公有的,而类中,一般是成员变量是私有的,成员函数是公有的,私有 ...
- C#定义类成员
1.成员定义 public--成员可以由任何代码访问. private--成员只能由类中的代码访问(如果没有使用任何关键字,就默认使用这个关键字). internal--成员只能由定义它的程序集(项目 ...
- 理解ATL中的一些汇编代码(通过Thunk技术来调用类成员函数)
我们知道ATL(活动模板库)是一套很小巧高效的COM开发库,它本身的核心文件其实没几个,COM相关的(主要是atlbase.h, atlcom.h),另外还有一个窗口相关的(atlwin.h), 所以 ...
随机推荐
- Android——GridView(显示文字)
activity_test9的layout文件: <?xml version="1.0" encoding="utf-8"?> <Linear ...
- [hdu 3605]Escape
这题的做法非常直观,却又非常不直观 先容许我吐一下槽吧~作者你的英语是读到火星上去了喵? 题目大体是说人类要移民,然后有 n 个人, m 个星球 每个人都有 m 个 0 . 1 数码表示他能否移民到该 ...
- jquery轻松操作CSS样式
$(this).click(function(){ if($(this).hasClass(“zxx_fri_on”)){ $(this).removeClass(“zxx_fri_on”); ...
- Oracle DBWR,LGWR,CKPT,ARCH 触发条件 总结
一. DBWR写磁盘数据触发条件 1. 当进程在辅助LRU链表和主LRU链表上扫描以查找可以覆盖的buffer header[空闲缓冲区]时,如果已经扫描的buffer header的数量到达一定的 ...
- nginx和apache配置目录浏览功能
今天工作需要,要给客户提供一个patch的下载地址,于是想用nginx的目录浏览功能来做,需要让客户看到指定一个目录下的文件列表,然后让他自己来选择该下载那个文件: 我们都知道在apache下可以配置 ...
- linux服务之drbd
http://www.drbd.org/docs/about/http://oss.linbit.com/drbd/ 一般我们会在生产环境的MYSQL中用drbd +ha做master 备份,当然这是 ...
- codevs-1447取出整数的一部分
说实在的,这个题目真不想写了…… 1447 取出整数的一部分 题目描述 Description 假如有一个整数(int):145678,现在我做截取该数一部份的操作,如输入4,返回前4位即1456;如 ...
- 一个完整的JENKINS下的ANT BUILD.XML文件(Jenkins可以参考)
一个完整的JENKINS下的ANT BUILD.XML文件 <?xml version="1.0" encoding="UTF-8"?> <p ...
- WEB/HTTP 调试利器 Fiddler 的一些技巧分享
1.原理简介: Fiddler 是目前最强大最好用的 Web 调试工具之一,它能记录所有客户端和服务器的http和https请求, 允许你监视,设置 CGI 请求的断点,甚至修改输入输出数据.同类的工 ...
- selenium+python自动化之环境安装
一.Python安装 1.操作系统:win7 64位系统 2.下载Python安装包,选择2.7版本和3.6版本都可以(最好安装2.7版本稳定)官网下载地址:https://www.python.or ...