《面向对象程序设计基础R》实验指导和实验报告(2017年).doc_第1页
《面向对象程序设计基础R》实验指导和实验报告(2017年).doc_第2页
《面向对象程序设计基础R》实验指导和实验报告(2017年).doc_第3页
《面向对象程序设计基础R》实验指导和实验报告(2017年).doc_第4页
《面向对象程序设计基础R》实验指导和实验报告(2017年).doc_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验报告 实践报告课程名称: 面向对象程序设计基础R 实验、实践名称: 面向对象程序设计基础R 实验、实践地点: 逸夫楼201 专业班级: 软件1601 学号: 2016005 学生姓名: 指导教师: 宋春花 2017年 4月 17 日2实验名称 实验一 熟悉Visual Studio 开发环境实验目的和要求(1) 熟悉基本的输入输出方法;(2) 掌握函数的定义,调用和声明方法,函数的参数传递机制,函数嵌套调用和递归调用,内联函数,带默认形参的函数,重载函数;(3) 理解命名空间的概念,掌握命名空间的使用;(4) 熟悉const关键字的使用;(5) 掌握内存的动态分配的概念和使用;(5) 掌握类的定义和对象的定义和使用;实验内容1.编写重载函数area()和perimeter(),分别计算圆、长方形、正方形的面积和周长,并在主函数中测试之。2.完善程序,并上机运行:(此程序见原模板)3.定义一个复数类Complex,复数的实部Real与虚部Image定义为私有数据成员。用复数类定义复数对象c1、c2、c3,用默认构造函数将c1初始化为c1=20+40i,将c2初始化为c2=0+0i,用拷贝构造函数将c3初始化为c3=20+40i。用公有成员函数Display()显示复数c1、c2与c3的内容。4. 定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对角线长度。在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。然后调用Show()显示矩形左上角与右下角坐标及对角线长度。最后用delete运算符回收为矩形动态分配的存储空间。主要仪器设备台式或笔记本电脑:1台/人实验记录(写出实验内容中1、2、3、4的程序代码和运行结果)(可分栏或加页)1.#include stdafx.h#includeiostreamusing namespace std;const double pi=3.141592;double area(double r);double area(double a,double b);double perimer(double r);double perimer(double a,double b);double area(double r)double s;s=pi*r*r;return s;double area(double a,double b)double s;s=a*b;return s;double perimer(double r)double p;p=2*pi*r;return p;double perimer(double a,double b)double p;p=2*(a=b);return p;int _tmain(int argc, _TCHAR* argv)double r;double m;double n;cout请输入圆的半径:r;cout圆的面积为:area(r) 圆的周长为:perimer(r)endl;cout请输入长方形的长和宽:mn;cout长方形面积为:area(m,n) 长方形的周长为:perimer(m,n)endl;cout请输入正方形的边长:m;cout正方形的面积为:area(m,m) 正方形的周长为:perimer(m,m)endl;getchar();getchar();return 0;/ 实验1-2.cpp : 定义控制台应用程序的入口点。/#includestdafx.h#include iostreamusing namespace std;const double pi = 3.14;class Circle public:double area() return x*x*pi; Circle(double a) :x(a) private:double x;class Rectangle public:Rectangle(double x,double y):len(x),wid(y)/写出矩形类的构造函数double area() return len*wid; /写出计算矩形面积函数private:double len, wid;class Square public:Square(double x) :len(x) /写出正方形类的构造函数double area() return len*len; /写出计算正方形面积函数private:double len;int main()cout Input shape endl;cout if circle, input c, if rectangle input r; if square input s shape;switch (shape)case c: double r;cout input radius r;Circle r1(r);cout circle area= r1.area() endl;system(pause);break;case r: double len, wid;cout input length and width len wid;Rectangle jx(len,wid); /构造矩形对象,宽为wid,高为len cout rectangle area= jx.area() endl;/输出矩形面积system(pause);break;case s: double len;cout input length len;Square fx(len);/构造正方形对象,边长为lencout square area= fx.area() endl;/输出正方形面积system(pause);break;default: cout input error! endl;break;return 0;3. #include stdafx.h#includeiostreamusing namespace std;class Complexpublic:Complex(double a,double b):real(a),image(b);Complex(Complex &c);void Dispaly();private :double real;double image;Complex:Complex(Complex & c)real = c.real;image = c.image;void Complex:Dispaly()cout ( real + image i) endl;int main()Complex c1(20,40);Complex c2(0, 0);Complex c3(c1);c1.Dispaly(); c2.Dispaly(); c3.Dispaly();system(pause);4. #include stdafx.h#includeiostream#includemath.husing namespace std;class Rectangleprotected:double Left,Top,Right,Bottom;public:Rectangle(double a,double b,double c,double d):Left(a),Top(b),Right(c),Bottom(d);void Show(Rectangle &d);double Diagonal(Rectangle &c);double Rectangle:Diagonal(Rectangle &c)double x=c.Right-c.Left;double y=c.Top-c.Bottom;double d=sqrt(x*x+y*y);return d;void Rectangle:Show(Rectangle &d) cout左 上 角 (Left,Top)endl; cout右 下 角 (Right,Bottom)endl; cout斜 角 线 长 度d.Diagonal(d)Show(*r1); delete r1; system(pause); return 0;遇到的问题和解决方法心得体会实验名称 实验二 类与对象的特性实验目的和要求(1) 掌握类的定义和对象的定义和使用;(2) 掌握静态数据成员和静态成员函数的定义和使用方法;(3) 理解类的作用域、对象的作用域及生存周期; (4) 掌握函数调用中参数的传递;(5) 掌握常量类型;(6) 掌握友元函数和友元类的定义及使用。实验内容 编写一个学生类。(1)输出每个学生的姓名、学号、成绩;(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。主要仪器设备台式或笔记本电脑:1台/人实验记录(写出实验内容中程序代码和运行结果)(可分栏或加页)#includestdafx.h#include using namespace std;/定义学生类class Studentint no; /学号char name10; /姓名double score; /成绩static int totalNumber; /学生人数static double totalScore; /总成绩static double lowestScore; /最低成绩static double highestScore; /最高成绩public:Student(int no_,char *name_,double score_); /构造函数static void Output(); /输出相关信息void StudentInformation(); /输出学生基本信息;int Student:totalNumber=0; /静态数据初始化double Student:highestScore =0.;double Student:lowestScore =100.;int main( ) Student stu1(1001,张小三y,97.5);stu1.StudentInformation ();Student stu2(1625,李 老 四,83.);stu2.StudentInformation ();Student stu3(1628,王 老 五,93.);stu3.StudentInformation ();Student stu4(1352,郭 小 六,62.5);stu4.StudentInformation ();Student stu5(1999,王 小 明,77.); stu5.StudentInformation ();Student:Output (); system(pause);return 0;Student:Student (int no_,char *name_,double score_) no=no_;strcpy(name,name_);score=score_;totalNumber+;totalScore+=score;if (scorehighestScore)highestScore=score;if (scorelowestScore)lowestScore=score;void Student:StudentInformation () cout学 号no 姓 名 name 成 绩:scoreendl;void Student:Output ()cout学 总 数 totalNumber 总 成 绩 totalScore endl;cout平 均 成 绩totalScore/totalNumber 最 高 成 绩 highestScore 最 低 成 绩lowestScore endl;遇到的问题和解决方法心得体会实验名称 实验三 继承与派生实验目的和要求(1)掌握类的继承和派生概念;(2)掌握派生类的定义与使用;(3)掌握派生类的构造函数与析构函数的应用及调用顺序;(4)理解赋值兼容原则的应用。实验内容考察一个点、圆、圆柱体的层次结构,计算圆和圆柱体的面积,阅读程序并运行。建立3个类,分别为点类、圆类、圆柱类,点类派生得到圆类,圆类派生得到圆柱类。主要仪器设备台式或笔记本电脑:1台/人实验记录(写出实验内容中的程序代码和运行结果)(可分栏或加页)#include stdafx.h#include #includeusing namespace std;class Point friend ostream &operator (ostream &, const Point &);public:Point(int = 0, int = 0);/ 带默认参数的构造函数void setPoint(int, int);/ 对点坐标数据赋值int getX() const return x; int getY() const return y; protected:int x, y; / Point类的数据成员;class Circle : public Point friend ostream &operator (ostream &, const Circle &);/ 友元函数public:Circle(double r = 0.0, int x = 0, int y = 0);/ 构造函数void setRadius(double); /置半径 double getRadius() const; /返回半径double area() const;/ 返回面积protected: double radius;/ 数据成员,半径;class Cylinder :public Circle friend ostream & operator(ostream &, const Cylinder &); / 友元函数public:Cylinder(double h = 0.0, double r = 0.0, int x = 0, int y = 0); / 构造函数void setHeight(double); / 置高度值double getHeight() const; /返回高度值double area() const; /返回面积 double volume() const; /返回体积protected:double height;/ 数据成员,高度;Point:Point(int a, int b) / 构造函数,调用成员函数对 x,y作初始化setPoint(a, b);void Point:setPoint(int a, int b) x = a; y = b; / 对数据成员置值ostream &operator (ostream &output, const Point &p) / 重载插入运算符,输出对象数据output p.x , p.y = 0 ? r : 0); / 对半径置值double Circle:getRadius() const return radius; / 返回半径值double Circle:area() const return 3.14159 * radius * radius; / 计算并返回面积值ostream & operator (ostream &output, const Circle &c) / 输出圆心坐标和半径值output Center = c.x , c.y ; Radius = setiosflags(ios:fixed | ios:showpoint) setprecision(2) = 0 ? h : 0); / 对高度置值double Cylinder:getHeight() const return height; / 返回高度值double Cylinder:area() const return 2 * Circle:area() + 2 * 3.14159*radius*height; / 计算并返回圆柱体的表面积double Cylinder:volume() const return Circle:area()*height; / 计算并返回圆柱体的体积 / 输出数据成员圆心坐标、半径和高度值ostream &operator (ostream &output, const Cylinder &cy)output Center = cy.x , cy.y ; Radius = setiosflags(ios:fixed | ios:showpoint) setprecision(2) cy.radius ; Height = cy.height endl;return output;#include #include using namespace std;int main()Point p(72, 115);/定义点对象并初始化cout The initial location of p is p endl;p.setPoint(10, 10);/置点的新数据值cout nThe new location of p is p endl;/输出数据Circle c(2.5, 37, 43);/定义圆对象并初始化cout nThe initial location and radius of c aren c nArea = c.area() n;c.setRadius(4.25); c.setPoint(2, 2); /置圆的新数据值/输出圆心坐标和圆面积cout nThe new location and radius of c aren c nArea = c.area() n;Cylinder cyl(5.7, 2.5, 12, 23);/定义圆柱体对象并初始化/输出圆柱体各数据和表面积,体积cout nThe initial location, radius and height of cyl aren cyl Area = cyl.area() nVolume = cyl.volume() n;/置圆柱体的新数据值cyl.setHeight(10); cyl.setRadius(4.25); cyl.setPoint(2, 2);cout nThe new location, radius and height of cyl aren cyl Area = cyl.area() nVolume = cyl.volume() n;system(pause);遇到的问题和解决方法心得体会实验名称 实验四 多态性实验目的和要求(1)掌握C+中运算符重载的机制和运算符重载的方式;(2)理解类型转换的必要性,掌握类型转换的使用方法;(3)理解多态性,掌握虚函数的设计方法;(4)掌握纯虚函数和抽象类的使用方法。实验内容某小型公司,主要有三类人员:管理人员、计时人员和计件人员。现在,需要存储这些人的姓名、编号、时薪、工时、每件工件薪金、工件数,计算月薪并显示全部信息。 设计4个类,分别为雇员类(为抽象类)、管理人员类、计时人员类和计件人员类,其中管理人员类、计时人员类和计件人员类由雇员类派生得到。主要仪器设备台式或笔记本计算机:1台/人实验记录(写出实验内容中的程序代码和运行结果) (可分栏或加页)#include stdafx.h#includeiostream#includeiomanip#includestring.husing namespace std;/Employee.hclass Employee /雇员类-抽象类public: Employee( int,const char*name_);virtual Employee(); /虚析构函数string getName() ; long getNumber() ;virtual double earnings(double) ;/纯虚函数,计算月薪virtual void print() ; /虚函数,输出编号、姓名protected:long number;/ 编号string name;/ 姓名;/ Manager.hclass Manager : public Employee /管理人员类public:const Manager( int,const char*name_, double );Manager() void setMonthlySalary(double); / 置月薪virtual double earnings() ; / 计算管理人员月薪virtual void print() ; / 输出管理人员信息private:double monthlySalary; /私有数据,月薪;/ HourlyWorker.hclass HourlyWorker : public Employee /计时人员类public: HourlyWorker(int, const char*name_, double, int);HourlyWorker() void setWage(double); /置时薪void setHours(int); /置工时virtual double earnings() ; / 计算计时工月薪virtual void print() ; / 输出计时工月薪private:double wage; /时薪double hours; /工时;/ PieceWorker.hclass PieceWorker : public Employee /计件人员类public: PieceWorker(int, const char*name_, double , int);PieceWorker() void setWage(double);/置每件工件薪金void setQuantity(int);/置工件数virtual double earnings() ; / 计算计件薪金virtual void print() ; / 输出计件薪金private:double wagePerPiece;/每件工件薪金int quantity;/工件数;int main()cout setiosflags(ios:fixed | ios:showpoint) print();basePtr = &hw1; basePtr-print();basePtr = &pw1; basePtr-print();system(pause);Employee:Employee( int nu_, const char*name_)number = nu_;name = name_;Employee:Employee()string Employee:getName() return name; long Employee:getNumber()ret

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论