`

C++案例程序

 
阅读更多
//该库包含了C++语言的输入/输出操作中的相关内容
#include <iostream.h>
//声明被调函数
int add(int,int);
//主函数
void main(){
	int a,b;
	//endl表示换行
	cout<<"Enter a b"<<endl;
	cin>>a>>b;
	int c=add(a,b);
	cout<<"a+b="<<c<<endl;
}
//被调函数
int add(int x,int y){
	return x+y;
}

#include <iostream.h>
class A
{
	//修饰共有成员变量
	public:
		//带参数的构造函数
		A(int i)
		{a=i;}
		//成员函数fun1加法
		int fun1()
		{return a+a;}
		//成员函数fun2乘法
		int fun2()
		{return a*a;}
	//修饰私有成员变量
	private:
		int a;
};
void main(){
	//定义一个类A的对象x,并对它进行了初始化
	A x(5);
	cout<<x.fun1()<<endl;
	cout<<x.fun2()<<endl;
}

#include <iostream.h>
//在C++语言中通常使用const定义常量,而很少用宏定义常量
const double pi=3.14159265;
void main(){
	double a,r;
	r=1.5;
	a=pi*r*r;
	cout<<"a="<<a<<endl;
}

#include <iostream.h>
void main(){
	char ch1,ch2;
	cout<<"输入两个字符"<<endl;
	cin>>ch1>>ch2;
	if(ch1!=ch2)
		if(ch1>ch2)
			cout<<"大于"<<endl;
		else
			cout<<"小于"<<endl;
	else
		cout<<"相等"<<endl;
}

#include <iostream.h>
void main(){
	int a(3),b(5);
	if(a!=b)
		if(a==b){
			a+=3;
			cout<<a<<endl;
		}else{
			b-=2;
			cout<<b<<endl;
		}
		cout<<a+b<<endl;
}

#include <iostream.h>
static int a=5;
void main(){
	static int b;
	double d;
	char ch='h';
	cout<<a<<","<<b<<","<<d<<","<<ch<<endl;
	a=10;
	b=20;
	d=30.5;
	ch='m';
	cout<<a<<","<<b<<","<<d<<","<<ch<<endl;
	cout<<&a<<endl;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics