C++程序设计作业.doc_第1页
C++程序设计作业.doc_第2页
C++程序设计作业.doc_第3页
C++程序设计作业.doc_第4页
全文预览已结束

下载本文档

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

文档简介

编写程序:1. 定义一个Point类来处理三维点points(x,y,z).该类有一默认的constructor,一copy constructor, 一negate()成员函数将point的x,y和z值各乘-1, 一norm()成员函数返回该点到原点(0,0,0)的距离,一个print()成员函数显示x,y,和z的值。2定义一个Person类,它的每个对象表示一个人。数据成员必须包含姓名、出生年份、死亡年份,一个默认的构造函数,一析构函数,读取数据的成员函数,一个print()成员函数显示所有数据。3。定义一个Shape基类,由它派生出Rectanglr和Circle类,二者都有GetArea( )函数计算对象的面积。使用Rectangle 类创建一个派生类Square。4. 定义一个Shape抽象类,由它派生出Rectanglr和Circle类,二者都有GetArea( )函数计算对象的面积,GetPerim( ) 函数计算对象的周长。第一题目:#include #include class Point public: Point(float x=0, float y=0, float z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p.x_), y_(p.y_), z_(p.z_) void negate() x_ *= -1; y_ *= -1; z_ *= -1; double norm() return sqrt(x_*x_ + y_*y_ + z_*z_); void print() cout ( x_ , y_ , z_ ); private: float x_, y_, z_;void main() Point p(12,-3,4); cout p = ; p.print(); cout , p.norm() = p.norm() endl; p.negate(); cout p = ; p.print(); cout , p.norm() = p.norm() endl;第二题:#include class Person public: Person(char* =0, int =0, int =0); Person() delete name_; char* name() return name_; int born() return yob_; int died() return yod_; void print(); private: int len_; char* name_; int yob_, yod_;void main() Person cb(Charles Babbage,1792,1871); cb.print();Person:Person(char* name, int yob, int yod) : len_(strlen(name), name_(new charlen_+1), yob_(yob), yod_(yod) memcpy(name_, name, len_+1);void Person:print() cout tName: name_ endl; if (yob_) cout tBorn: yob_ endl; if (yod_) cout tDied: yod_ endl;第三题:class Shapeprotected:public: double GetArea()return 0;class Rectangle : public Shapeprotected: double length; double width;public: Rectangle() Rectangle(double l, double w)length = l;width = w; double GetArea()return length * width;class Circle : public Shapeprotected: double radius;public: Circle(double r)radius = r; double GetArea()return 3.1415926 * radius * radius;class Square : public Rectangleprotected:public: Square(double l) length = width = l; double GetArea()return length * width;第四题:class Shape public: Shape() virtual double GetArea()=0; virtual double Getperim()=0;class Rectangle:public Shape private: double h; double c; public: Rectangle()h=c=0; Rectangle(double hi,double ch)h=hi;c=ch; double GetArea()return c*h; double Getperim()return 2*c+2*h;class Circle:public Shape private: double r; const doub

温馨提示

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

最新文档

评论

0/150

提交评论