C++案例程序

本文介绍了C++语言的基本输入输出操作、类的定义与成员函数、常量与宏的区别、字符比较、条件判断以及变量作用域的概念。通过具体代码示例深入浅出地讲解了C++的核心知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//该库包含了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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值