- 3
- 0
- 约2.53万字
- 约 12页
- 2017-03-16 发布于贵州
- 举报
24李后浪实验一和对象实验报告
实验一 类和对象
实验课程名:高级语言程序设计Ⅱ
专业班级: 12网工2班 学号: 座机电话号码0224 姓名: 李后浪 实验时间: 4.8-4.15 实验地点: K4-201 指导教师: 祁文青 一、实验目的和要求
1 理解类和对象的概念,掌握声明类和定义对象的方法。
2 掌握构造函数和析构函数的实现方法。
3 初步掌握使用类和对象编制C++程序。
4 掌握对象数组、对象指针和string类的使用方法。
5 掌握使用对象、对象指针和对象引用作为函数参数的方法。
6 掌握类对象作为成员的使用方法。
7 掌握静态数据成员和静态成员函数的使用方法。
8 理解友元的概念和掌握友元的使用方法。 二、实验内容
任务名称
输入下列程序
//test4-1.cpp
#include
using namespace std;
class Coordinate
public:
Coordinate int x1,int y1 x x1; y y1; Coordinate Coordinate p ;
~Coordinate cout ”Destructor is calleded\n”;
int getx return x;
int gety return y;
private:
int x,y;
;
Coordinate::Coordinate Coordinate p x p.x;
y p.y;
cout ”copy-initialization Constructou is called\n”; int main Coordinate p1 3,4 ;
Coordinate p2 p1 ;
Coordinate p3 p2;
cout ”p3 “ p3.getx ”,” p3.gety ” \n”;
return 0 ; 写出程序的运行结果。
copy-initialization Constructou is called
copy-initialization Constructou is called
p3 3,4
Destructor is calleded
Destructor is calleded
Destructor is calleded
将Coordinate类中带有两个参数的构造函数进行修改,在函数体内增添下述语句:
cout ”Constructor is called.\n”;
写出程序的运行结果,并解释输出结果。
Constructor is called.
copy-initialization Constructou is called
copy-initialization Constructou is called
p3 3,4
Destructor is calleded
Destructor is calleded
Destructor is calleded
P1的值被p引用,执行构造函数Coordinate Coordinate p 时输出“Constructor is called.”其余和(1)一样,不变。
(3)按下列要求进行调试:
在主函数体内,添加下列语句:
Coordinate p4;
Coordinata p5 2 ;
调试程序时会出现什么错误?为什么?如何对已有的构造函数进行适当修改?
调用函数时找不到对应的函数,因为没有定义不含参数的函数,和只含一个参数的构造函数,所以调用函数时找不到这样的函数,应该给构造函数赋初值。Coordinate int x1 0,int y1 0
(4)经过以上第(2)步和第(3)步的修改后,结合运行结果分析:创建不同的对象时会调用不同的构造函数。
源代码:
//test4-1.cpp
#include
using namespace std;
class Coordinate
public:
Coordinate int x1 0,int y1 0 x x1; y y1;
cout Constructor is called.\n; Coordinate Coordinate p ;
~Coordinate cout Destructor is calleded\n;
int getx return x;
int gety return y;
private:
int x,y;
;
Coordinate::Coordinate Coordinate p x p.x;
y p.y;
cout copy-initialization Constructou is call
您可能关注的文档
- 2016年网络营推广方案策划书.doc
- 2016年玉溪市师招聘考试历年真题资料.doc
- 2016年考研武大学复试分数线.doc
- 2016年福建闽职业技术学院高职招考模拟题(含解析).docx
- 2016年自学考00055《企业会计学》模拟试题级答案解释.docx
- 2016年苏州大硕士研究生专业目录正式.doc
- 2016年软件评师考试大纲.doc
- 2016年软考网管理员考前模拟试题及答案(上).doc
- 2016年遵义医院公开招聘方案.doc
- 2016年职称英卫生类a历年真题.doc
- 小区绿化施工协议书.docx
- 墙面施工协议书.docx
- 1 古诗二首(课件)--2025-2026学年统编版语文二年级下册.pptx
- (2026春新版)部编版八年级道德与法治下册《3.1《公民基本权利》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《4.3《依法履行义务》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《6.2《按劳分配为主体、多种分配方式并存》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《6.1《公有制为主体、多种所有制经济共同发展》PPT课件.pptx
- 初三教学管理交流发言稿.docx
- 小学生课外阅读总结.docx
- 餐饮门店夜经济运营的社会责任报告(夜间贡献)撰写流程试题库及答案.doc
原创力文档

文档评论(0)