




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1.#include class Dateint year;int month;int day;public:Date(int y, int m, int d)year = y;month = m;day = d;void Display()cout year / month / day endl;int IsLearYear()return year%4=0&year%100!=0 | year%400=0;void setDay(int d)day = d;2. 2.ostream &operator(ostream &out, const Vector &v)for(int i=0; iv.size; i+)out v.datai ;return out;Vector:Vector(int v, int n)size = n;data = new intn;for(int i=0; in; i+)datai = vi;Vector:Vector()delete data;Vector:Vector(const Vector &v)size = v.size;data = new intv.size;for(int i=0; isize; i+)datai = v.datai;Vector& Vector:operator=(const Vector &v)if(this != &v)delete data;size = v.size;data = new intv.size;for(int i=0; isize; i+)datai = v.datai;return *this;int& Vector:operator(int index)return dataindex;1编写一个函数模板,用于求数组中各元素之和,并编写测试程序进行测试。函数模板声明如下:template Type Sum(Type tArray, int iSize)2定义一个抽象类CShape,它有一个纯虚函数GetLength();派生出四边型类CSquare和圆类CCircle,在派生类中重载函数GetLength(),用于求图形的周长,编写测试程序进行测试。1参考程序: #include using namespace std;template Type Sum(Type tArray, int iSize)Type tSum = 0;for (int i = 0; i iSize; i+)tSum = tSum + tArrayi;return tSum;int main(void)int a = 1, 2, 3;double b = 1.5, 2.8, 8.9, 8;cout Sum(a, 3) endl;cout Sum(b, 4) endl;return 0;2参考程序:#include using namespace std;class CShapepublic:virtual double GetLength() const = 0;class CSquare:public CShapepublic:CSquare(double dWidth, double dHeight)m_dWidth = dWidth;m_dHeight = dHeight;double GetLength() constreturn 2 * (m_dWidth + m_dHeight);private:double m_dWidth, m_dHeight;class CCircle:public CShapepublic:CCircle(double dRadius)m_dRadius = dRadius;double GetLength() constreturn 3.1415926 * m_dRadius * m_dRadius;private:double m_dRadius;int main(void)CSquare oSquare(2, 3);cout oSquare.GetLength() endl;CCircle oCircle(10);cout oCircle.GetLength() endl;return 0;编写程序,定义抽象基类Shape(形状),由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)和Square (正方形),用函数函数ShowArea()分别显示各种图形的面积,最后还要显示所有图形的总面积。#include / 预处理命令using namespace std;/ 使用标准命名空间stdconst double PI = 3.1415926;/ 定义常量PIclass Shapepublic:Shape() / 构造函数virtual Shape() / 析构函数virtual ShowArea() = 0;/ 显示面积static double totalArea;/ 总面积static void ShowTotalArea() cout 总面积: totalArea endl; ;class Circle: public Shapeprivate:double radius;/ 半径public:Circle(double r): radius(r) totalArea += PI * r * r; / 构造函数Circle() / 析构函数virtual ShowArea() cout 圆面积: PI * radius * radius endl; ;/ 显示面积;class Rectangle: public Shapeprivate:double length;/ 长double width;/ 宽public:Rectangle(double l, double w): length(l), width(w) totalArea += l * w; / 构造函数Rectangle() / 析构函数virtual ShowArea() cout 矩形面积: length * width endl; ;/ 显示面积;class Square: public Shapeprivate:double length;/ 边长public:Square(double l): length(l) totalArea += l * l; / 构造函数Square() / 析构函数virtual ShowArea() cout 正方形面积: length * length endl; ;/ 显示面积;double Shape:totalArea = 0;/ 初始化totalAreaint main(void)Circle c(1);/ 定义圆对象c.ShowArea();/ 显示面积Rectangle r(1, 2);/ 定义矩形对象r.ShowArea();/ 显示面积Square z(3);/ 定义正方形对象z.ShowArea();/ 显示面积Shape:ShowTotalArea();/ 显示总面积return 0; / 返回值0, 返回操作系统1设计一个类Rect,要求如下:(1)该类中的私有数据成员length,width存放它的长和宽,并且设置它们的默认值是0。(2)通过成员函数设置其长和宽,并确保长和宽都在(0,50)范围之内。 (3)实现求周长函数GetPerimeter()。2定义一个二维座标类Vector2d, 二个数据成员为double型x, y 为private属性。定义代二个参数的构造函数和一个Show( ) 函数用以输出x, y的值, 另外作为成员函数重载的运算苻”+”的功能是将此类二个对象的数据成员x和y对应相加。这些成员函数的属性均为public. 请用C+编写此程序, 并编写测试程序进行测试#include using namespace std;class Rect private:double length, width; public: Rect(double l = 0, double w = 0): length(l), width(w) void Set(double l, double w) if (length = 50 | width = 50 ) throw 数据不在指定范围(0,50)!;/ 抛出异常length = l;width = w;double GetPerimeter() return 2 * (length + width); ;int main() try/ 检查异常Rect obj(1, 8);cout 周长: obj.GetPerimeter() endl;catch (char *str)/ 捕捉异常/ 处理异常cout 异常信息: str endl;/ 输出异常信息return 0;2参考程序:#include using namespace std;class Vector2d double x, y; public: Vector2d(double a, double b): x(a), y(b) void Show() cout x y endl; Vector2d operator+(Vector2d &obj); ;Vector2d Vector2d:operator+(Vector2d &obj) return Vector2d(x + obj.x, y + obj.y); int main() Vector2d d1(3.5, 4.5), d2(2.5, 5.5), d3(0.0, 0.0);d3 = d1 + d2; d3.Show(); return 0;1编写程序:定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、 Rectangle( 长方形)、Trapezoid (梯形)和Triangle (三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。 注:主函数中定义如下对象Circle circle(12.6); Square square(3.5); Rectangle rectangle(4.5,8.4); Trapezoid trapezoid(2.0,4.5,3.2); Triangle triangle(4.5,8.4); 1#include using namespace std;class Shapepublic: virtual double area() const =0; ;class Circle:public Shapepublic:Circle(double r):radius(r) virtual double area() const return 3.14159*radius*radius; protected: double radius; ;class Square:public Shapepublic: Square(double s):side(s) virtual double area() const return side*side; protected: double side;class Rectangle:public Shapepublic: Rectangle(double w,double h):width(w),height(h) virtual double area() const return width*height; protected: double width,height; ;class Trapezoid:public Shapepublic: Trapezoid(double t,double b,double h):top(t),bottom(t),height(h) virtual double area() const return 0.5*(top+bottom)*height; protected: double top,bottom,height; ;class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) virtual double area() const return 0.5*width*height; protected: double width,height; ;int main()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 阅读节活动策划方案范文(3篇)
- 线上促销活动赠品方案策划(3篇)
- 弧形铝板施工方案(3篇)
- 镜面墙壁施工方案(3篇)
- 砂砾石隧洞施工方案(3篇)
- 培训会活动宣传方案策划(3篇)
- 郫县电梯加装施工方案(3篇)
- 中职旅游考试题库及答案
- 地铁安检考试题库及答案
- 安徽省马鞍山市和县2024-2025学年高三下学期高考二模历史试题含参考答案
- 粮油保管员(高级)职业技能鉴定参考试题(附答案)
- 集团统借统还管理制度
- 供电一线员工服务规范培训
- 皮肤医美行业分析
- 劳务施工施工方案
- 新部编版五年级语文上册第八单元课件
- 2025年信息技术实习生培训协议
- 中国急性缺血性卒中诊治指南(2023)解读
- ESD防静电知识培训
- SJG 71-2020 桥梁工程设计标准
- 加入音乐家协会申请书
评论
0/150
提交评论