1. 程式人生 > 其它 >C++: const修飾函式引數

C++: const修飾函式引數

// const_yinyong.cpp : 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include <cstdio>
#include <iostream>

using namespace std;
//宣告一個函式,求體積,輸入引數只讀
double volume(const double &len, const double &width, const double &height);

int _tmain(int argc, _TCHAR* argv[])
{
	int a = 3, b = 4, c = 5;
	double v1 = volume(a, b, c);

	cout << "體積是:" << v1 << endl;

	double v2 = volume(10, 20, 30);
	double v3 = volume(89.4, 32.7, 19);
	double v4 = volume(a + 12.5, b + 23.4, 16.78);
	double v5 = volume(a + b, a + c, b + c);
	printf("%lf, %lf, %lf, %lf, %lf\n", v1, v2, v3, v4, v5);


	getchar();
	return 0;
}

double volume(const double &len, const double &width, const double &height)
{
	return len*width*height;
}

執行結果: