您的位置:首页 > 编程语言

模板函数及一些代码规范

2016-02-24 13:48 369 查看
使用Visual Studio 2012进行小程序编写过程,养成一定的代码规范,用于从学校到职场的过渡……



创建空项目,填写项目名。



添加 .cpp文件或 .h头文件



更改文件名,使用英文单词,令其功能一目了然

代码编写规范

/**********************************************************
*Name:         Ctemplate.cpp
*Content:      Used for demonstrating template function
*Instructions: none
*Version:      V1.0
*Author:       Caddress
*Data:         20160224
***********************************************************/
#include<stdio.h>
#include<iostream>
using namespace std;

//declarations of swap()
template<class Ta, class Tb>
void swap(Ta& a, Tb& b);

//declarations of abc()
template<class Ta, class Tb, class Tc>
Ta abc(Ta&a, Tb&b, Tc&c);

/**********************************************************
*Function:     main
*Input:        none
*Output:       string of results
*Return:       void
*Data          Version      Author          Content
*----------------------------------------------------------
*20160224      V1.0         Caddress        create
***********************************************************/
void main()
{
int x = 3, y = 4, z = 5;
swap(x, y);
cout <<"The results of the exchange is x = "<< x <<" y = "<< y <<";"<< endl;
cout <<"The results of the calculation is "<<abc(x,y,z) <<";"<<endl;
//Press any button to exit
getchar();
}

/**********************************************************
*Function:     swap
*Input:        a , b
*Output:       none
*Return:       void
*Data          Version      Author          Content
*----------------------------------------------------------
*20160224      V1.0         Caddress        create
***********************************************************/
template<class Ta, class Tb>
void swap(Ta& a, Tb& b)
{
int temp = a;
a = b;
b = temp;
}

/**********************************************************
*Function:     abc
*Input:        a , b , c
*Output:       none
*Return:       a + b * c
*Data          Version      Author          Content
*----------------------------------------------------------
*20160224      V1.0         Caddress        create
***********************************************************/
template<class Ta, class Tb, class Tc>
Ta abc(Ta&a, Tb&b, Tc&c)
{
return a + b * c;
}


运行结果



//使用模板函数可以创建创建适用不同参数类型,但功能相同的功能函数。

//使用引用参数能避免复制值给形参,减少操作。交换函数若使用传值参数,会造成无法对实参进行值交换。

//用关键字const来指明函数不可修改的引用参数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  visual studio 2012