c++ 学习笔记之程序结构

来源:互联网 发布:名录数据 编辑:程序博客网 时间:2024/06/06 04:01

语句和控制流

基本和 java 语言一致,这里列出两处不同的地方。

//for 循环string str = "This is zhengdehua speaking";for each (auto c in str) //auto 关键字可以根据具体值自动判断类型{    cout << "[" << c << "]";}cout << "\n";
//goto 语句int n = 10;mylabel:    cout << n << ",";    n--;    if (n > 0) goto mylabel;    cout << "liftoff\n";

函数

函数类型

//有返回类型的函数int func(int a, int b){//……}//无返回类型的函数void func(int a, int b){//……}//无参数列的函数int func(void){ //void 是 c 风格的写法,c++ 中可以不写//……}

main 函数的返回值

main 函数如果没有指定返回值,编译器会假设其返回 0,代表程序执行成功执行完成,main 函数的返回值列表:

返回值 描述 0 程序成功执行完成 EXIT_SUCCESS 程序执行成功,返回值参考 <cstdlib> EXIT_FAILURE 程序执行失败,返回值参考 <cstdlib>

值传递和引用传递

示例:

//值传递函数void duplicateByVal(int a, int b){    a *= 2;    b *= 2;}//引用传递函数void duplicateByRef(int& a, int& b){    a *= 2;    b *= 2;}int x = 1, y = 2;duplicateByVal(x, y); //x, y 值不变duplicateByRef(x, y); //x, y 值翻倍

const 引用的效率

对于复合类型的参数,特别像是 string 类型,使用引用传递可以避免参数拷贝,提高效率,并且如果不希望该对象被函数内部逻辑改变,可以使用 const 关键字。

示例:

string concatenate (const string& a, const string& b){  return a+b;}

inline 函数

对于简单的函数,可以使用 inline 关键字进行声明,这样,编译器会将函数的内部逻辑提取出来执行,不用通过调用函数来执行逻辑,调高效率。

示例:

inline string concatenate (const string& a, const string& b){  return a+b;}

参数默认值

c++ 中函数参数可以在定义时指定默认值,在调用时使用默认值即可。

示例:

// default values in functions#include <iostream>using namespace std;int divide (int a, int b=2){  int r;  r=a/b;  return (r);}int main (){  cout << divide (12) << '\n';  cout << divide (20,4) << '\n';  return 0;}

函数声明

c++ 中函数的调用者可以直接调用函数声明,而函数体可以在此之后实现。

示例:

// declaring functions prototypes#include <iostream>using namespace std;void odd (int x);void even (int); //两种写法均有效int main(){  int i;  do {    cout << "Please, enter number (0 to exit): ";    cin >> i;    odd (i);  } while (i!=0);  return 0;}void odd (int x){  if ((x%2)!=0) cout << "It is odd.\n";  else even (x);}void even (int x){  if ((x%2)==0) cout << "It is even.\n";  else odd (x);}

递归

递归就是函数对自身的调用。

示例:

// 用递归实现阶乘#include <iostream>using namespace std;long factorial (long a){  if (a > 1)   return (a * factorial (a-1));  else   return 1;}int main (){  long number = 9;  cout << number << "! = " << factorial (number);  return 0;}

函数重载和模板

函数重载

定义:两个函数拥有同样的名称,但是两者的参数个数或类型不同。
注意: 返回类型不同不能成为重载的充分条件。

示例:

int operate (int a, int b){  return (a*b);}double operate (double a, double b){  return (a/b);}

函数模板

和重载类似,可以给函数指定模板,在调用时明确函数的定义。

模板函数定义:
template <template-parameters> function-declaration

模板函数调用:
name <template-arguments> (function-arguments)

示例:

// function templates#include <iostream>using namespace std;template <class T, class U>bool are_equal (T a, U b){  return (a==b);}int main (){  if (are_equal(10,10.0)) //同 are_equal<int,double>(10,10.0)    cout << "x and y are equal\n";  else    cout << "x and y are not equal\n";  return 0;}

特定类型参数模板

模板参数可以是特定的类型。

示例:

// template arguments#include <iostream>using namespace std;template <class T, int N>T fixed_multiply (T val){  return val * N;}int main() {  std::cout << fixed_multiply<int,2>(10) << '\n';  std::cout << fixed_multiply<int,3>(10) << '\n';}//注意: 函数 fixed_multiply 的模板参数在编译时就确定了,在调用时,//必须传递确定的常量值,不能用变量。

命名可见性

作用域 (scope)

示例:

int x = 10;int y = 20;{    int x; //inner scope    x = 30; //set value to inner x    y = 30; //set value to outer y    cout << "inner block:\n";    cout << "x:" << x << "\n";    cout << "y:" << y << "\n";}cout << "outer block:\n";cout << "x:" << x << "\n";cout << "y:" << y << "\n";

命名空间 (namespace)

示例:

// namespaces#include <iostream>using namespace std;namespace foo{  int value() { return 5; }}namespace bar{  const double pi = 3.1416;  double value() { return 2*pi; }}int main () {  cout << foo::value() << '\n';  cout << bar::value() << '\n';  cout << bar::pi << '\n';  return 0;}

同一命名空间可以分开定义:

namespace foo { int a; }namespace bar { int b; }namespace foo {     //int a; //重复的变量定义.    int c; }

using 关键字

使用 using 引入命名空间里的元素。

示例一:

// 引入指定元素#include <iostream>using namespace std;namespace first{  int x = 5;  int y = 10;}namespace second{  double x = 3.1416;  double y = 2.7183;}int main () {  using first::x;  using second::y;  cout << x << '\n';  cout << y << '\n';  cout << first::y << '\n';  cout << second::x << '\n';  return 0;}

示例二:

//引入整个命名空间#include <iostream>using namespace std;namespace first{  int x = 5;  int y = 10;}namespace second{  double x = 3.1416;  double y = 2.7183;}int main () {  using namespace first;  cout << x << '\n';  cout << y << '\n';  cout << second::x << '\n';  cout << second::y << '\n';  return 0;}

示例三:

//在不同的块引入#include <iostream>using namespace std;namespace first{  int x = 5;}namespace second{  double x = 3.1416;}int main () {  {    using namespace first;    cout << x << '\n';  }  {    using namespace second;    cout << x << '\n';  }  return 0;}

命名空间别名

示例:

namespace foo {    int x = 0;}int main(){    namespace newFoo = foo;    cout << newFoo::x << '\n';  }

存储类型

名称 对象 作用域 初始化 静态存储(static storage) 全局变量,命名空间 整个程序 未显示初始化的全局变量会自动初始化到 0 自动存储(automatic storage) 局部变量 变量所在当前作用域块 未显示初始化的局部变量不会自动初始化,会分配一个不确定的值(包括0).
0 0
原创粉丝点击