实验2_3 C++对C的扩充(2)

一、实验目的和要求

 进一步掌握C++语言在结构化程序设计方面对C的扩充

 

二、实验环境(软、硬件及条件)

一台安装有Visual C++ 6.0的计算机

三、实验步骤

1.定义一个函数,比较两个数的大小,形参分别使用指针和引用。

2. 编写程序,利用new和delete操作符为数组分配空间,从键盘上接收6个整数,然后输出在屏幕上,最后释放数组空间;

3. 编写一个sum内联函数,能返回两个整数的和,其两个参数都带默认参数,然后由主函数进行调用。

4.自己设计一个程序,测试const的三种用法:指向常量的指针,常指针,指向常量的常指针。

5. 编写程序可分别求取2个整数,3个整数,2个浮点数,3个浮点数的和,要求用函数实现。

四、实验中遇到的问题及解决

实验结果及分析

1.

#include<iostream>

using namespace std;

int display(int *p,int &q)

{

if(*p>q)return *p;

else return q;

}

void main()

{int x=5,y=6;

cout<<"最大值为"<<display(&x,y)<<endl;

}

2.

#include<iostream>

using namespace std;

void main()

{

int i,n=6;

int *p=new int[n];

cout<<"请输入六个整数:"<<endl;

for(i=0;i<n;i++)

cin>>p[i];

for(i=0;i<n;i++)

cout<<p[i]<<endl;

delete []p;

}

3.

#include<iostream>

using namespace std;

inline int sum(int a,int b);

void main()

{int x=4,y=5;

cout<<sum(x,y)<<endl;

};

int sum(int a,int b)

{int s;

s=a+b;

return s;

}

4.

#include<iostream>

using namespace std;

class A

{public:

void x(int a){const int *p=&a;

          cout<<*p<<endl;}

void y(int b){int *const q=&b;

          cout<<*q<<endl;}

void z(int c){const int*const k=&c;

          cout<<*k<<endl;}

};

void main()

{A a;

a.x(4);

a.y(5);

a.z(6);

}

5.

#include <iostream>

using namespace std;

int he1(int x,int y)

{int he1;

he1=x+y;

cout<<x<<"+"<<y<<"="<<he1<<endl;

return he1;

}

int he2(int x,int y,int z)

{int he2;

he2=x+y+z;

cout<<x<<"+"<<y<<"+"<<z<<"="<<he2<<endl;

return he2;

}

double he3(double x,double y)

{double he3;

he3=x+y;

cout<<x<<"+"<<y<<"="<<he3<<endl;

return he3;

}

double he4(double x,double y,double z)

{double he4;

he4=x+y+z;

cout<<x<<"+"<<y<<"+"<<z<<"="<<he4<<endl;

return he4;

}

void main()

{cout<<"he1="<<he1(3,5)<<endl;

cout<<"he2="<<he2(3,5,9)<<endl;

cout<<"he3="<<he3(3.5,5.2)<<endl;

cout<<"he4="<<he4(3.5,5.2,9.1)<<endl;

}

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值