收藏 分销(赏)

面向对象程序设计(第二次上机实验).doc

上传人:仙人****88 文档编号:9280941 上传时间:2025-03-19 格式:DOC 页数:7 大小:44KB 下载积分:10 金币
下载 相关 举报
面向对象程序设计(第二次上机实验).doc_第1页
第1页 / 共7页
面向对象程序设计(第二次上机实验).doc_第2页
第2页 / 共7页


点击查看更多>>
资源描述
《面向对象程序设计》 上机报告 题目:第二次上机 班级: 学号: 姓名: 上机时间: 一、 题目要求 1.阅读程序,预测功能,再上机编辑、运行该程序,以验证自己的预测。 【思考与练习】 1. 构造函数的作用以及在什么情况下调用构造函数。 作用:不需要用户来调用它,在建立对象时自动识别它,有用户定义,根据初始化的要求设计函数体和函数参数。 2. 在什么情况下拷贝构造函数被调用。 在以下三种情况下,系统自动调用拷贝构造函数。 ⑴ 当用类的一个对象去初始化该类的另一个对象时; ⑵ 如果函数的形参是类的对象,调用函数,进行形参和实参的结合时; ⑶ 如果函数的返回值是类的对象,函数执行完成返回调用者时。 3.构造一个圆柱体的类,定义该类对象a,再定义一个和a 同底等高的圆柱体b,计算 a 圆柱体的底面积,计算b 圆柱体的体积。 二 1.定义一个复数类,通过重载运算符: +,-,*,/,直接实现二个复数之间的乘除运算。编写一个完整的程序,测试重载运算符的正确性。要求加法“+”用成员函数实现重载,减法“-”用成员函数实现重载。乘法“*”用友元函数实现重载,除法“/”用成员函数实现重载。 ⑵上机要求 增加重载复数的加法和减法运算符的功能,实现两个复数的加法,一个复数与一个实数的加法;两个复数的减法,一个复数与一个实数的减法。用成员函数实现加法运算符的重载,用友元函数实现减法运算符的重载。 自已设计主函数,完成程序的调试工作 二、题目分析 3,构造函数,通过调用函数实现运算。 2,运用运算符的重载。 三、源代码 1.1 #include <iostream.h> class Point { public: Point(int xx = 0, int yy = 0) { X = xx; Y = yy; cout<<"调用构造函数\n"; } Point(Point &p); int GetX(){ return X; } int GetY(){ return Y; } private: int X,Y; }; Point::Point(Point & p) { X = p.X; Y = p.Y; cout<<"调用拷贝构造函数\n"; } void main() { Point A(3,4); //Point B = A; //Point B; B = A; Point B(A); //b 行 cout<<B.GetX()<<"\n"; } 1.2 #include <iostream.h> class Point { public: Point(int xx = 0,int yy = 0) { X = xx; Y = yy; cout<<"调用构造函数\n"; } Point(Point & p); int GetX(){ return X; } int GetY(){ return Y; } private: int X,Y; }; Point::Point(Point & p) { X = p.X; Y = p.Y; cout<<"调用拷贝构造函数\n"; } void fun1(Point p) { cout<<p.GetX()<<"\n"; } void main() { Point A(4,5); Point B(A); cout<<B.GetX()<<"\n"; fun1(B); //调用拷贝构造函数,实现形参和实参结合 } 1.3 #include <iostream.h> class Point { public: Point(int xx=0,int yy=0) { X = xx; Y = yy; cout<<"调用构造函数\n"; } Point(Point & p); int GetX() {return X;} int GetY() {return Y;} private: int X,Y; }; Point::Point(Point &p) { X=p.X; Y=p.Y; cout<<"调用拷贝构造函数\n"; } Point fun2() { Point Temp(10,20); //调用构造函数 return Temp; } void main() { Point A(4,5); Point B(A); cout<<B.GetX()<<"\n"; B = fun2(); cout<<B.GetX()<<"\n"; } 思考3 #include <iostream> using namespace std; class Point { public: Point(int,int); Point(Point &p); int volume(); int area(); private: int radius; int height; }; Point::Point(int r,int h) { radius=r; height=h; } Point::Point(Point &p) { radius=p.radius; height=p.height; cout<<"调用拷贝构造函数\n"; } int Point::volume() { return(3.14159*radius*radius*height); } int Point::area() { return(3.14159*radius*radius); } int main() { Point c1(2,5); cout<<"The area of c1 is "<<c1.area()<<endl; Point c2(c1); cout<<"The volume of c2 is "<<c2.volume()<<endl; return 0; } 2 #include <iostream.h> class Complex { public: Complex(){real=0;imag=0;} Complex(double r){real=r;imag=0;} Complex(double r,double i){real=r;imag=i;} friend Complex operator + (Complex c1,Complex c2); friend Complex operator - (Complex c1,Complex c2); void display(); private: double real; double imag; }; Complex operator + (Complex c1,Complex c2) { return Complex(c1.real+c2.real, c1.imag+c2.imag); } Complex operator - (Complex c1,Complex c2) { return Complex(c1.real-c2.real, c1.imag-c2.imag); } void Complex::display() { cout<<"("<<real<<"+"<<imag<<"i)"<<endl; } int main() { Complex c1(1,5),c2(2,8),c3,c4; c3=c1+c2; c4=c1-c2; c3.display(); c4.display(); return 0; } 四、运行记录和调试过程 1.1 输出: 调用构造函数 调用拷贝构造函数 3 Press any key to continue 思考: 1,将b 行改写为Point B = A; 程序输出结果会有变化吗? 答:没有变化。 输出: 调用构造函数 调用拷贝构造函数 3 Press any key to continue 2,将b 行改写为Point B; B = A; 程序输出结果会有变化吗? 答:有变化。 输出: 调用构造函数 调用构造函数 3 Press any key to continue 1.2 输出: 调用构造函数 调用拷贝构造函数 4 调用拷贝构造函数 4 Press any key to continue 1.3 输出: 调用构造函数 调用拷贝构造函数 4 调用构造函数 调用拷贝构造函数 10 Press any key to continue 思考3 输出 The area of c1 is 12 调用拷贝构造函数 The volume of c2 is 62 Press any key to continue 2 输出 (3+13i) (-1+-3i) Press any key to continue 五、心得体会 通过这次上机,了解了运算符的重载,实现了复数的运算,也认识到了自己更多的不足,会继续努力。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服