24李后浪实验一和对象实验报告.docVIP

  • 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

文档评论(0)

1亿VIP精品文档

相关文档