1 / 14
文档名称:

《C 面向对象程序设计》实验报告.docx

格式:docx   大小:68KB   页数:14页
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

《C 面向对象程序设计》实验报告.docx

上传人:daoqqzhuanyongyou2 2020/11/16 文件大小:68 KB

下载得到文件列表

《C 面向对象程序设计》实验报告.docx

文档介绍

文档介绍:《C++面向对象程序设计》实验报告
实验序号:2 实验项目名称:类和对象
学 号
姓 名
专业
实验地点
指导教师
实验时间
一、 实验目的及要求
(1) 理解类和对象的概念;
(2) 了解C++在非面向对象方面对 C功能的扩充与增强。
(3) 初步掌握使用类和对象编制 C++程序。
(4) 掌握对象数组、对象指针和 string类的使用方法。
(5) 掌握使用对象、对象指针和对象引用作为函数参数的方法。
(6) 掌握类对象作为成员的使用方法。
(7) 掌握静态数据成员和静态成员函数的使用方法。
(8) 理解友元的概念和掌握友元的使用方法。
二、 实验设备(环境)及要求
Micorsoft Visual C++
三、 实验内容与步骤(题目、算法和结果描述)
1、输入下列程序。
#in clude<iostream>
using n amespace std;
class Coordin ate
{
public:
Coordi nate(i nt x1,i nt y1)
{ x=x1;
y=y1;}
Coordin ate(Coord in ate &p);
~Coord in ate()
{ cout<<"Destructor is called'n";}
int getx()
{ return x;}
in t gety()
{ return y;} private:
int x,y;
};
Coordin ate::Coord in ate(Coord in ate &p)
{
x=;
y=;
cout<<"Copy-i nitializati on Con structour is called' n"; } int mai n()
{
Coordinate p1(3,4);
Coordinate p2(p1);
Coordin ate p3=p2;
cout<<"p3=("<<()<<","<<()<<")\n"; return 0;
}
写出程序的运行结果。
【运行结果截图】:
Copy—iniitxa 1 ion nw七七outp is called
Gapv-initiall£ation Co nstruetour is called
called called called to continue
p3-<3,4>
Destrujctoi? is Desrti'uctav is Destrmctor is Press *ny he*/
将Coordinator类中带有两个参数的构造函数进行修改,在函数体内增添下列语
句: cout<< "con structor is called.' n
【运行结果截图】
' ■rC:\Docu»ents and Sett iiLgs\To>\S:iIi\3f^3Cf|
conctructor is called.
fCopy^initialisat ion Consti'ucttjut* is cailL&tl k^optj—in it iallzat ion Construe to up ie ca_lled k>3=C3,4> tft&S tl^UCtiOF
Des tt*uctoi*
Des:tif'uictoi'
IS
is
ZLS
any hey
called called called to Gontir)u^H
【运行结果分析】:带有两个参数的构造函数在主函数中只被调用了一次,其他的都是调 用使用对象引用作为函数参数的构造函数。
按下列要求进行调试:在主函数体内,添加下列语句:
Coordin ator p4;
Coordin ator p5(2);
调试程序时会出现什么错误?为什么?如何对已有的构造函数进行适当修改?
【运行结果截图】:
consttMictor is called
Gopsf-in itialization Construe tour is called Cnpy-initializatior Construe tour is called constructor is c&lled.
consti^LljCtoi1 is called.
pestruc'toi?
is
Destructor
is
Destructoi?
is
Destructor
t is
Destructor
is
Press any
called called celled called, calle