栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

第三次实验

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第三次实验

第三次实验内容一

本次实验学习了类和对象的基本应用,深刻理解了析构函数的含义和用法。

代码如下:using namespace std;
class Coordinate {
public:
Coordinate()//构造函数
{
times = 2; //默认值
cout << “Coordinate construction1 called!” << endl; //标记
}
Coordinate(int times1)//构造函数的重载 带参数的构造函数
{
times = times1;自定义次数
cout << “Coordinate construction2 called!” << endl;
}
~Coordinate()//析构函数 函数回收
{
cout << “Coordinate construction1 called!” << endl;
}
void InputCoord()//输入函数
{
for (int i = 0; i < times; i++)
{
cout << “Please input x:” << endl;
cin >> Coord[i][1];
cout << “Please input y:” << endl;
cin >> Coord[i][2];
}
}
void ShowCoord()//输出函数
{
cout << “The coord is:” << endl;
for (int i = 0; i < times; i++)
{
cout << “(” << Coord[i][1] << “,” << Coord[i][2] << “)” << endl;
}
}
void ShowAvgCoord()//求平均值
{
float avgx = 0;
float avgy = 0;
for (int i=0; i< times; i++)
{
avgx = avgx + Coord[i][1];
avgy = avgy + Coord[i][2];
}
avgx = avgx / times;
avgy = avgy / times;
cout << “The AVG coord is:” << endl;
cout << “(” << avgx << “,” << avgy << “)” << endl;
}
private://成员变量定义
int times;
float Coord[100][100];
};
int main()
{
Coordinate x;
x.InputCoord();
x.ShowCoord();
x.ShowAvgCoord();
Coordinate y(5);
y.InputCoord();
y.ShowCoord();
y.ShowAvgCoord();
return 0;
运行过程:首先调用含有默认值的构造函数,输入两组二维数据,然后显示这两组数据及平均值,然后调用含参数的构造函数,输入n(参数)组二维数据,然后显示这n组数据及平均值。最后调用析构函数。
特点:简单清晰,调用时刻恰当,没有多余的冲突。
本次实验较为简单 但细节上学要学习的东西很多,希望下一次实验可以独立自主的完成,将学到的知识应用起来。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/513128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号