没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
12页
1)输入下列程序,按要求进行实验,并记录实验的结果。 #include <iostream> using namespace std; class Coordinate { public: Coordinate(int x1, int y1){x=x1; y=y1; cout<<”Constructor is called.”<<endl; } Coordinate(Coordinate &p); ~Coordinate(){cout<<"Destructor is called."<<endl;} int getx(){return x;} int gety(){return y;} private: int x, y; }; Coordinate::Coordinate(Coordinate &p){ x=p.x; y=p.y; cout<<"Copy initianization constructor is called."<<endl; } int main(){ Coordinate p1(3,4); Coordinate p2(p1); Coo
资源推荐
资源详情
资源评论
第 1 页 共 12 页
实验报告
学院(系)名称:计算机科学与工程学院
姓名
学号
专业
计算机科学
与技术
班级
3 班
实验
项目
实验一 类和简单对象
课程名称
高级语言程序设计 II
课程
代码
2267023
实验时间
2021.3.24
实验
地点
7-220
考 核
标 准
软 件 运 行
(软件运行
情况与完整
性)30 分
特色(软件独立
完成情况)20
分
软件注释(软件
可读性)10 分
实验报告(实验
内 容 丰 富 度 与
格 式 清 晰 度 )
40 分
成绩
考核
内容
○运行正确,
完整
○运行正确,
不完整
○ 运 行 不 正
确,不完整
○特色点较多
○仅有一些特色
点
○无特色
○注释完整,可
读性较强
○注释较少,可
读性一般
○没有注释,可
读性较弱
○丰富,清晰
○较丰富,较清
晰
○丰富,不清晰
○不丰富,不清
晰
教师签字:
第 2 页 共 12 页
1. 实验目的
(1)理解类和对象的概念;
(2)掌握类与对象的定义方法;
(3)理解类的成员的访问控制的含义,公有和私有成员的区别;
(4)掌握构造函数和析构函数的含义与作用、定义方式和实现;
(5)能够根据给定的要求定义类并实现类的成员函数;
(6)掌握 string 类的使用方法
(7)了解 C++面向对象程序设计的基本思想、基本方法和基本步骤;
(8)掌握 Visual Studio 或 DEV C++调试 C++程序的基本方法、基本步骤。
2.实验工具
Visual Studio 或 DEV C++
3.实验原理
1)类类型声明的一般形式如下:
class 类名{
[private:]
私有数据成员和成员函数
public:
公有数据成员和成员函数
};
2)返回值类型类名::成员函数名(参数表)
{
函数体
}
3)通过对象名和对象选择符访问对象中的成员
其一般形式是:
对象名.数据成员名
或
对象名.成员函数名[(实参表)]
其中“.”叫做对象选择符,简称点运算符。
4.实验方案
阅读题目,编写代码,编译代码,调试代码,得出结果
5.实验步骤:(写出实验所用方法的实施的步骤、代码、效果图)
1)输入下列程序,按要求进行实验,并记录实验的结果。
#include <iostream>
using namespace std;
class Coordinate
{
public:
Coordinate(int x1, int y1){x=x1; y=y1; cout<<”Constructor is called.”<<endl;
}
Coordinate(Coordinate &p);
~Coordinate(){cout<<"Destructor is called."<<endl;}
int getx(){return x;}
int gety(){return y;}
第 3 页 共 12 页
private:
int x, y;
};
Coordinate::Coordinate(Coordinate &p){
x=p.x; y=p.y;
cout<<"Copy initianization constructor is called."<<endl;
}
int main(){
Coordinate p1(3,4);
Coordinate p2(p1);
Coordinate p3=p2;
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")"<<endl;
return 0;
}
(1) 记录程序的运行结果
(2) 将 Coordinate 类中带有两个参数的构造函数进行修改,在函数体内增加如下语句:
cout<<”Constructor is called.”<<endl;
重新记录程序的运行结果,并解释输出结果。
在给 p1 赋初值时,会调用带有两个参数的构造函数,此时会输出“constructor is called”
在给 p2 和 p3 赋初值时,会调用拷贝构造函数,此时会输出两次“Copy initianization constructor is
called”
当程序运行结束时会释放对象 p1,p2,p3,此时会调用三次析构函数,输出三次“Destructor is
called”
(3) 按下列要求进行测试。
在主函数体内,添加如下语句:
剩余11页未读,继续阅读
资源评论
敲敲柚
- 粉丝: 4
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功