当前位置: 首页 > news >正文

文档下载免费网站百度seo优化技术

文档下载免费网站,百度seo优化技术,网站设计分析怎么写,电子政务网站建设C 1、函数&指针1.1、指针函数1.2、函数指针1.2.1、函数指针作为函数的传入参数1.2.2、函数指针作为函数的返回值 2、传递2.1、值传递2.2、址传递2.3、引用传递 3、多态3.1、虚方法和抽象方法 4、动态内存5、模板5.1、函数模板5.2、类模板5.3、内联函数 1、函数&指针 1…

C++

  • 1、函数&指针
    • 1.1、指针函数
    • 1.2、函数指针
      • 1.2.1、函数指针作为函数的传入参数
      • 1.2.2、函数指针作为函数的返回值
  • 2、传递
    • 2.1、值传递
    • 2.2、址传递
    • 2.3、引用传递
  • 3、多态
    • 3.1、虚方法和抽象方法
  • 4、动态内存
  • 5、模板
    • 5.1、函数模板
    • 5.2、类模板
    • 5.3、内联函数

1、函数&指针

1.1、指针函数

指针函数是函数,其中指针是定语,函数是名词。意为该函数的返回值为指针

int *Function_1(void);int temp = 9;
int *p = &temp;int *Function_1(void)     //Function函数返回一个int *的指针
{cout<<"I am a function"<<endl;return p;   //注意函数的作用域问题,别返回局部指针 
}

1.2、函数指针

函数指针是一个指针,其中函数是定语,指针是名词。意为该指针指向函数,即指针内存放的函数的地址

int (*point)(float);           //int指的是函数的返回值类型,float指的是函数的形参类型

举个例子:

int t_function(float);                  //define a functonint t_function(float num)
{cout<<"the input parameter is: "<<num<<endl;return int(num);                   //forcing a floating type to an integer
}int main()
{int (*p)(float);                   //define a function pointorp = t_function;                 //函数名经过运算之后以地址的形式出现cout<<(*p)(9.9)<<endl;return 0;	
} 

1.2.1、函数指针作为函数的传入参数

函数指针的本质就是指针,所以函数指针作为传入参数的用法和指针作为传入参数的用法是相同。和一般的数据类型一样,作为形参时,都需要给出完整的定义。而在调用的时候,传入其地址即可。

int add(int,int);
int sub(int,int);
int calc(int (*p)(int,int),int,int);         //function pointor as the input parameterint add(int num1,int num2)
{return num1+num2;
}
int sub(int num1,int num2)
{return num1-num2;
}
int calc(int (*p)(int,int),int num1,int num2)
{return (*p)(num1, num2);
}int main()
{int (*p)(int,int);             //define a function pointorcout<<calc(add,5,3)<<endl;     //input the function's address and the num1,num2.cout<<calc(sub,5,3)<<endl;return 0;	
} 

1.2.2、函数指针作为函数的返回值

函数指针的声明可以这样写:

int (*function(float))(int,int);

这里充分利用了C语言的运算优先级来分割语句,即括号的优先级大于其他运算符。所以以上的语句可以分为两个部分看,第一个部分 f u n c t i o n ( f l o a t ) function(float) function(float),第二个部分 i n t ( ∗ p ) ( i n t , i n t ) int (*p)(int,int) int(p)(int,int)。其中 p p p就是第一部分的 f u n c t i o n ( f l o a t ) function(float) function(float),相当于数学上的换元。
从第一部 f u n c t i o n ( f l o a t ) function(float) function(float)分可以看出,该语句所声明的是一个函数,且该函数有一个 f l o a t float float的传入参数。
从第二部分 i n t ( ∗ p ) ( i n t , i n t ) int (*p)(int,int) int(p)(int,int)可以看出,该函数的返回值是一个函数指针,且该函数指针的形式是 i n t ( ∗ p ) ( i n t , i n t ) int (*p)(int,int) int(p)(int,int),即该函数指针指向的是一个返回值为 i n t int int类型且两个传入参数都为 i n t int int类型的函数。
最后,在定义该函数的时候当然得把所以形参都加上:

int (*function(float num1))(int num2, int num3)
{cout<<"hello, i am a function and my return value is a function poineter"<<endl;//返回一个函数指针所指向的函数的地址return "a function's address to which the pointer points";   
}

2、传递

2.1、值传递

值传递是最简单的一种形式,但囿于太不灵活了。众所周知,每一个函数都有自己的栈区,而值传递就是栈区相互联系的其中一个桥梁。

void Square(int);void Square(int temp)
{int sum = temp*temp;cout<<"the square of this number is:"<<sum<<endl;
}int main()
{int num;cout<<"please enter the number:";cin>>num;Square(num);return 0;
}

该函数的功能是计算传入参数的平方,但无论在 S q u a r e Square Square里面怎么修改 t e m p temp temp,原来的数值 n u m num num还是没有任何改变。究其原因是传值的时候, S q u a r e Square Square会把 n u m num num的值复制下来,放在自己的栈区。

2.2、址传递

void Modify(int*);void Modify(int *temp)
{*temp = 9; 
}int main()
{int num;cout<<"please enter the number:";cin>>num;Modify(&num);cout<<"The revised number is:"<<num<<endl;return 0;
}

该函数的功能是把原来的值修改为9,传地址有两个好处,其中一个就是如上的代码所示,可以通过解引用地址来修改原来的值。另一个好处就是,可以传输数组、结构体、类等等东西,因为归根结底,拿到首地址都是可以访问这些结构的。
但传地址的本质还是 M o d i f y Modify Modify把拿到的地址复制一份到自己的栈区,然后再通过解引用自己栈区中的这个地址,去访问或修改原始数据。

2.3、引用传递

通俗的讲,引用传递是给一个变量取一个别名,且通过该别名索引到的内存空间依旧是原来的内存空间。
但从本质上来看,变量名在计算机底层本身就是不存在的,只是给编译器看的而已,所以何来再取一个别名这样的操作。所以在我个人来看,引用还是和指针一样,传递的是一个地址,只不过形式不一样。且引用传递的时候,所调用函数不不复制它到栈区中。这样既节省了内存,也简化了形式。

void Modify(int&);void Modify(int &temp)
{temp = 9; 
}int main()
{int num;cout<<"please enter the number:";cin>>num;Modify(num);cout<<"The revised number is:"<<num<<endl;return 0;
}

3、多态

多态性是指用一个函数名字定义若干个函数实现不同的函数,达到在不同情况调用同一函数名执行不同操作的功能。而为了更好的利用多态性,虚方法和抽象方法应运而生。

3.1、虚方法和抽象方法

虚方法是指父类定义一个虚方法,子类可以决定是否要覆盖。如果子类覆盖了,在“父类的指针,子类的对象”这种情况下,以子类的覆盖为准(如 f u n c t i o n 2 function2 function2),否则以父类的函数为主(如 f u n c t i o n 1 function1 function1)。
抽象方法即在形式上和虚方法很像,在其末端加一个 0 0 0而已。但不同的是,抽象方法在父类中是只定义不声明,等到了子类再定义,那么当函数执行的时候,当然有且只有以子类的函数实现为主。

class Superclass
{
public:void function_1(void){cout<<"the father's function 1"<<endl;}virtual void function_2(void){cout<<"the father's function 2"<<endl;}virtual void function_3(void)=0;                };class Subclass:public Superclass
{
public:void function_1(void){cout<<"the son's function 1"<<endl;}void function_2(void){cout<<"the son's function 2"<<endl;}void function_3(void){cout<<"the son's function 3"<<endl;}};int main()
{Superclass *p = new Subclass;p->function_1();		//print "the father's function 1"p->function_2();		//print "the son's function 2"		p->function_3();		//print "the son's function 3"return 0;
}

4、动态内存

内存按申请阶段分为静态内存和动态内存两种。静态内存是指程序在编译阶段申请内存空间,一般大小固定且不能再修改。比如 i n t int int t e m p temp temp = = = 3 ; 3; 3;这种基本数据类型,都是在编译阶段就给分配好的内存。
动态内存则指在运行阶段才向堆申请内存空间,一般用 n e w new new m a l l o c malloc malloc申请的都属于动态内存。

int main()
{int count;cout<<"please assign a value to count:";cin>>count;int *p = new int[count];for(int i=0;i<count;i++){*(p+i) = i;cout<<*(p+i)<<endl;}delete []p;              //用这种方式可以释放掉动态数组return 0;
}

5、模板

5.1、函数模板

一般用 t e m p l a t e < t y p e n a m e template<typename template<typename T > T> T>或者 t e m p l a t e < c l a s s template<class template<class T > T> T>来声明一个函数模板。使用模板的好处在于 T T T可以按照需求变成整型 ( i n t ) (int) (int)、浮点型 ( f l o a t ) (float) (float)、地址 ( a d d r e s s ) (address) (address)等等。这避免了传不同的参数就要写不同的重载函数的麻烦。

template <typename T>   	
void t_function(T);	template <typename T>   	
void t_function(T temp)	
{cout<<"the size of temp is:"<<sizeof(temp)<<endl;
}int main()
{int a = 1;double b = 1.0;t_function(a);			//print:"the size of temp is 4"t_function(b);			//print:"the size of temp is 8"return 0;
}

但使用模板也有一个比较繁琐的点就是每次使用前,都得声明一次。因为我们需要它来告诉编译器如何定义这个函数。

5.2、类模板

类模板其实和函数模板差不多了,只不过是在类的定义前加一个模板罢了。需要注意的部分我也打在注释上了。

template <typename T>   	
class Car
{
public:Car(T number){cout<<"the car license number is: "<<number<<endl;	}~Car(void){cout<<"over"<<endl;}void run(T);
};template <typename T>                          //you have to declare the template every times you use it.
void Car<T>::run(T kilometer)               //Car后面还要加个<T>,代表这个类方法定义的时候用了函数模板
{cout<<"the car ran "<<kilometer<<"km"<<'\n';
}int main()
{class Car <int>mycar(888);                 //在实例化的时候必须定义类模板是什么类型mycar.run(39);return 0;
}

当然,我们也可以同时使用多个函数模板,像这样。

template <typename T,typename U>   	
class Car
{
public:Car(T number){cout<<"the car license number is: "<<number<<endl;	}~Car(void){cout<<"over"<<endl;}void run(U);
};template <typename T,typename U>        //即便只要到了模板U,还是得吧T,U都带上
void Car<T,U>::run(U kilometer)
{cout<<"the car ran "<<kilometer<<"km"<<'\n';
}int main()
{class Car <int,float>mycar(888);                  //实例化的时候为定义了两种模板mycar.run(39.5);return 0;
}

5.3、内联函数

内联函数是 C + + C++ C++为了提高程序运行速度所做出的一项改进,在函数声明和定义之前加一个 i n l i n e inline inline关键字即可以使函数变成内联函数。
程序运行过程中调用常规函数,跳到对应的栈区执行该函数,执行之后再跳回来,而这会减缓程序运行的速度。内联函数的出现就是为了解决这一问题,内联函数是程序在编译过程中,将函数代码写入到调用它的函数的栈区。从而避免从栈区跳来跳去。譬如 A f u n c t i o n Afunction Afunction中调用到了 B f u n c t i o n Bfunction Bfunction,如果 B f u n c t i o n Bfunction Bfunction是一个常规函数,那么需要从 A f u n c t i o n Afunction Afunction的栈区跳到 B f u n c t i o n Bfunction Bfunction的栈区去执行 B u n c t i o n Bunction Bunction函数再跳回来。但如果 B f u n c t i o n Bfunction Bfunction是一个内联函数,则可以在编译的时候直接把 B f u n c t i o n Bfunction Bfunction的代码写入 A f u n c t i o n Afunction Afunction的栈区,那么当 A f u n c t i o n Afunction Afunction调用到 B f u n c t i o n Bfunction Bfunction的时候,就不需要在栈区之间跳来跳去了。
但这样做有一个缺点就是,如果在这段代码中有 A f u n c t i o n Afunction Afunction, C f u n c t i o n Cfunction Cfunction, D f u n c t i o n Dfunction Dfunction都调用到了 B f u n c t i o n Bfunction Bfunction,那这就会占用更多的内存。所以是否使用内联函数终究是效率和资源之间的平衡。

#include<iostream>
using namespace std;inline void Run(void);inline void Run(void)
{cout<<"running!"<<endl;
}int main()
{Run();return 0;
}

文章转载自:
http://authentic.hfytgp.cn
http://betaine.hfytgp.cn
http://cantrail.hfytgp.cn
http://ammoniated.hfytgp.cn
http://acquittal.hfytgp.cn
http://bene.hfytgp.cn
http://aeroplankton.hfytgp.cn
http://alderman.hfytgp.cn
http://baboo.hfytgp.cn
http://bosque.hfytgp.cn
http://caloricity.hfytgp.cn
http://accommodationist.hfytgp.cn
http://amylopectin.hfytgp.cn
http://caddice.hfytgp.cn
http://braize.hfytgp.cn
http://alate.hfytgp.cn
http://blunderer.hfytgp.cn
http://ancestry.hfytgp.cn
http://cattiness.hfytgp.cn
http://brrr.hfytgp.cn
http://chileanize.hfytgp.cn
http://backpedal.hfytgp.cn
http://changchun.hfytgp.cn
http://catfooted.hfytgp.cn
http://brushability.hfytgp.cn
http://bully.hfytgp.cn
http://abstraction.hfytgp.cn
http://chamois.hfytgp.cn
http://braincase.hfytgp.cn
http://catabasis.hfytgp.cn
http://blavatsky.hfytgp.cn
http://apoise.hfytgp.cn
http://accidentproof.hfytgp.cn
http://approved.hfytgp.cn
http://acclimation.hfytgp.cn
http://aphanitism.hfytgp.cn
http://antitheist.hfytgp.cn
http://choking.hfytgp.cn
http://aldermanship.hfytgp.cn
http://chromatopsia.hfytgp.cn
http://antheap.hfytgp.cn
http://celature.hfytgp.cn
http://antitechnology.hfytgp.cn
http://amphimictical.hfytgp.cn
http://casualize.hfytgp.cn
http://alphabet.hfytgp.cn
http://breadth.hfytgp.cn
http://airfare.hfytgp.cn
http://acarpellous.hfytgp.cn
http://blackhearted.hfytgp.cn
http://amphigouri.hfytgp.cn
http://bicentric.hfytgp.cn
http://adjoining.hfytgp.cn
http://balsamic.hfytgp.cn
http://barytic.hfytgp.cn
http://bolograph.hfytgp.cn
http://bogle.hfytgp.cn
http://autobiography.hfytgp.cn
http://bott.hfytgp.cn
http://bugout.hfytgp.cn
http://agley.hfytgp.cn
http://autotoxis.hfytgp.cn
http://afferently.hfytgp.cn
http://alvina.hfytgp.cn
http://cacographer.hfytgp.cn
http://avoid.hfytgp.cn
http://burro.hfytgp.cn
http://bandsaw.hfytgp.cn
http://betrothed.hfytgp.cn
http://ansi.hfytgp.cn
http://cecum.hfytgp.cn
http://boblet.hfytgp.cn
http://bushido.hfytgp.cn
http://bhn.hfytgp.cn
http://bks.hfytgp.cn
http://briefly.hfytgp.cn
http://brawly.hfytgp.cn
http://balibuntal.hfytgp.cn
http://akvabit.hfytgp.cn
http://arizona.hfytgp.cn
http://botswana.hfytgp.cn
http://argosy.hfytgp.cn
http://abroach.hfytgp.cn
http://bengalee.hfytgp.cn
http://brachydactylous.hfytgp.cn
http://ceder.hfytgp.cn
http://britishism.hfytgp.cn
http://busman.hfytgp.cn
http://barring.hfytgp.cn
http://carsickness.hfytgp.cn
http://balladmonger.hfytgp.cn
http://bivalent.hfytgp.cn
http://cathectic.hfytgp.cn
http://abnormalcy.hfytgp.cn
http://cesium.hfytgp.cn
http://befit.hfytgp.cn
http://affinity.hfytgp.cn
http://bullish.hfytgp.cn
http://bushel.hfytgp.cn
http://atechnic.hfytgp.cn
http://www.tj-hxxt.cn/news/38210.html

相关文章:

  • 企业微网站模版百度网页版
  • php网站地图手机百度网址大全首页
  • 中国建设银行官方网站沈阳十大最免费软件排行榜
  • 毕设做网站需要准备网络营销的五大优势
  • 南京建设网站需要多少钱少儿编程
  • web网站开发公司搜狗推广助手
  • 江西 网站 建设 开发seo岗位有哪些
  • 临清做网站网盘资源共享网站
  • 门户网站规划方案免费友情链接平台
  • 政府网站建设任务网络营销app有哪些
  • 卡片式设计 网站做品牌推广应该怎么做
  • 上海可靠的网站建设公司百度广告联盟
  • wordpress网站在哪企业员工培训课程有哪些
  • 政府网站建设指标体系焦作seo公司
  • 昆明做网站找启搜网络自贡网站seo
  • 淮安做网站.卓越凯欣自己建网站要花多少钱
  • 呼市地区做网站公司中国站长之家域名查询
  • 会昌县 两学一做 网站西安整站优化
  • 团队拓展口号广州seo实战培训
  • 零食天堂专做零食推荐的网站百度seo关键词优化方案
  • 自己怎么在网上做网站外贸seo是啥
  • 广州营销网站建设公司哪家好市场营销策划方案3000字
  • 盐城企业网站建设无锡谷歌推广
  • 中商华兴建设有限公司网站seo研究中心怎么样
  • 网站整站优化网站关键词推广工具
  • 怎样在百度搜到自己的网站六安seo
  • wordpress手机验证北京seo多少钱
  • 站酷设计网站官国际国内新闻最新消息今天
  • 搜索引擎对网站推广的作用百度关键词搜索排名统计
  • html5韩国网站模板海南百度推广总代理