C++ 入门程序(二)

这篇主要讲C++中的函数

1.函数的定义

求长方形的面积

#include<iostream>

using namespace std;

int main()
{
	float A,B,S;
	cout<<"Please inout the length and width of the rectangle:";
	cin>>A>>B;
	float area(float a,float b);//声明函数
	S=area(A,B);
	cout<<"The area is:"<<S<<endl; 
 } 
 
float area(float a,float b)
{
	float z;
	z=a*b;
	return z;
}

2.函数的调用

利用函数调用求长方形的面积

#include<iostream>

using namespace std;

float length,width,square;

void sayHi()
{
	cout<<"Please input:";
}

void input(float a,float b)
{
	length=a;
	width=b;
}

int main()
{
	sayHi();
	float a,b;
	cin>>a>>b;
	input(a,b);
	void output();//声明函数 
	output();//调用函数 
}

void output()
{
	square=length*width;
	cout<<"The area is:"<<square<<endl;
}

3.函数的参数传递

(1)值传递

两个变量的值交换—一个错误的程序

/**
两个变量的值交换(一个错误的程序) 
*/
#include<iostream>

using namespace std;

void Destroy(int a,int b)
{
	int k;
	k=a;
	a=b;
	b=k;
}/*Destroy()*/ 

int main()
{
	int a,b;
	cout<<"Please input two integer number:";
	cin>>a>>b;
	Destroy(a,b);
	cout<<a<<","<<b<<endl;
	return 0;
 } /*main()*/

(2)引用型变量传递

两个变量的值交换

/**
两个变量的值交换 
*/ 

#include<iostream>

using namespace std;

void Destroy(int &a,int &b)
{
	int k;
	k=a;
	a=b;
	b=k;
}

int main()
{
	int a,b;
	cout<<"Please input two integer numbers:";
	cin>>a>>b;
	Destroy(a,b);
	cout<<a<<","<<b;
	return 0;
 } 

4.内联函数

利用内联函数求三角形的面积

/**
利用内联函数求三角函数的面积 
*/

#include<iostream>
#include<math.h>

using namespace std;

inline double area(double L,double W,double H)
{
	double a=0.5*(L+W+H);
	double s=sqrt(a*(a-L)*(a-W)*(a-H));
	return s;
}
int main()
{
	double x(3);//x=3
	double y(4);
	double z(5);
	double NewArea;
	NewArea=area(x,y,z);
	cout<<"The area is:"<<NewArea<<endl;
	return 0;
 } 

5.带默认参数的函数

(1)使用带默认参数的函数编程

#include<iostream>

using namespace std;

float area(float a=7,float b=8)
{
	int z;
	z=a*b;
	return z;
}

int main()
{
	float a,b,s1,s2,s3;
	cout<<"Please input two numbers:";
	cin>>a>>b;
	s1=area();
	s2=area(a);
	s3=area(a,b);
	cout<<"s1="<<s1<<endl;
	cout<<"s2="<<s2<<endl;
	cout<<"s3="<<s3<<endl;
	return 0;
}

(2)声明时指定默认形参值的使用

#include<iostream>

using namespace std;

int main()
{
	int add(int a=2,int b=3);//声明时赋值 
	int a,b;
	int c1,c2;
	cout<<"Please input two integer numbers:";
	cin>>a>>b;
	c1=add();
	c2=add(a,b);
	cout<<"c1="<<c1<<endl;
	cout<<"c2="<<c2<<endl;
	return 0;
 } 
 
 int add(int a,int b) 
{
	int c;
	c=a+b;
	return c;
}

(3)默认形参值为全局变量和局部变量的使用

#include<iostream>

using namespace std;

int L,W,S;

void input(int a=2,int b=3)//定义函数,指定全局变量 
{
	L=a;
	W=b;
}

void output()
{
	S=L*W;
	cout<<"The area is:"<<S<<endl; 
}

void show()
{
	void input(int a=5,int b=6);//声明函数,指定为局部默认形参值
	input();
	output(); 
}

int main()
{
	input();
	output();
	input(8,6);
	output();
	show();
	return 0;
 } 

6.重载函数

编程求2个或者3个整数中的最大值

#include<iostream>

using namespace std;

int max(int x,int y)//max()(1)
{
	if(x>y) return x;
	else return y;
}

int max(int x,int y,int z)//max()(2)
{
	if(x>y) y=x;
	if(y>z) z=y;
	return z; 
}

int main()
{
	cout<<"max(1,5)="<<max(1,5)<<endl;
	cout<<"max(1,2,5)="<<max(1,2,5)<<endl;
	return 0;
} 

7.函数模板

求三个整数,三个浮点数的最大值

#include<iostream>

using namespace std;
template <typename T>

T max(T a,T b,T c)//函数模板 
{
	if(a>b) b=a;
	if(b>c) c=b;
	return c;
}
int main()
{
	int ia=1,ib=2,ic=3;
	float fa=1.2,fb=2.3,fc=3.4;
	double da=1.2345,db=2.3456,dc=3.4567;
	cout<<"i_max="<<max(ia,ib,ic)<<endl;
	cout<<"f_max="<<max(fa,fb,fc)<<endl;
	cout<<"d_max="<<max(da,db,dc)<<endl;
	return 0;
} 

啦啦啦啦

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值