C++代码风格与规范
/*
* Copyright(c) 2008 by Microsoft Corporation
* All right reserved.
*
* 文件名称: polymorphism.h
* 文件标识:
* 描 述: 多态性演示
*
* 当前版本: 1.1
* 作 者: James
* 完成日期: 2/26/2008
*
* 取代版本: 1.0
* 作 者: Kacy
* 完成日期: 2/24/2008
*/
#ifndef _POLYMORPHISM_H_
#define _POLYMORPHISM_H_
const int MAX_LENGTH=100; //常量全用大写的字母,用下划线分割单词
int g_howManyPeople; //不得已需要全局变量,加前缀g_,表示global
class Shape; //类名用大写字母开头的单词组合而成,如class LeafNode, class Person
void Draw(Shape *aShape); //全局函数的名字应该使用"动词"或者"动宾词组",如DrawBox()
class Shape
{
public:
virtual void Draw(void)=0;
};
//类之间的空行
class Circle : public Shape
{
//采用以行为为中的类版式:将public类型的函数写在前面,而将private类型的数据写在后面
public:
Circle() : m_radius(0.0) {}
virtual void Draw(void); //函数名用大写字母开关的单词组合而成,如GetName(),Eat()
double GetRadius(); //用正确的反义词组命名相反动作的函数,如GetAge()和SetAge()
void SetRadius(double radius); //参数用小写字母开头的单词组合而成,如(int drawMode)
private:
//类的数据成员加前缀_m,表示member
double m_radius; //变量用小写字母开头的单词组合而成,如drawMode, newValue, oldValue
static int m_s_numberOfCircle; //静态变量用s_开头表示static
};
class Cube : public Shape
{
public:
virtual void Draw(void);
};
class Sphere : public Shape
{
public:
virtual void Draw(void);
};
#endif
/*
* Copyright(c) 2008 by Microsoft Corporation
* All right reserved.
*
* 文件名称: polymorphism.cpp
* 文件标识:
* 描 述: 多态性演示
*
* 当前版本: 1.1
* 作 者: James
* 完成日期: 2/26/2008
*
* 取代版本: 1.0
* 作 者: Kacy
* 完成日期: 2/24/2008
*/
#include <iostream>
#include "polymorphism.h"
using namespace std;
/*
* 函数功能: 画圆
* 输入参数: 空
* 输出参数: 无
* 返 回 值: 空
*/
void Circle::Draw(void)
{
cout<<"Draw a Circle!"<<endl;
}
double Circle::GetRadius()
{
return m_radius;
}
void Circle::SetRadius(double radius)
{
m_radius=radius;
}
//函数之间的空行
void Cube::Draw(void)
{
cout<<"Draw a Cube!"<<endl;
}
void Sphere::Draw(void)
{
cout<<"Draw a Sphere!"<<endl;
}
void Draw(Shape *aShape)
{
aShape->Draw();
}
void main()
{
Circle aCircle;
Cube aCube;
Sphere aSphere;
//函数内部的空行(加在逻辑上不太相关的语句之间)
Draw(&aCircle);
Draw(&aCube);
Draw(&aSphere); 
Circle anotherCircle; //变量的名字当使用"形容词+名词"
double radius; //变量的名字当使用"名词"
anotherCircle.SetRadius(2.33);
radius=anotherCircle.GetRadius();
cout<<radius<<endl;
}
int width=10; //定义并初始化width
int height=10; //定义并初始化height
int depth=10; //定义并初始化depth
if(condition)
{
return x;
}
else
{
return y;
}
return (condition ? x : y);
for(initialization; condition; update)
{
//program code
}
//空行
while(condition)
{
statement1;
//空行
if(condition)
{
//program code
}
else
{
//program code
}
//空行
statement2;
}//end of while
void Func(const int *x, const int *y, const int *z)
{
//program code
}
if((very_longer_variable1 >= very_longer_variable2)
&& (very_longer_variable3 <= very_longer_variable4)
&& (very_longer_variable5 <= very_longer_variable6)
{
//program code
}
HWND CreateWindow(LPCTSTR lpClassName, // registered class name
LPCTSTR lpWindowName, // window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // menu handle or child identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam ); // window-creation data