面向对象C++习题参考解答_第1页
面向对象C++习题参考解答_第2页
面向对象C++习题参考解答_第3页
面向对象C++习题参考解答_第4页
面向对象C++习题参考解答_第5页
免费预览已结束,剩余8页可下载查看

下载本文档

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

文档简介

4 8 定义一个 Dog 类 包含 age weight 等属性 以及对这些属性操作的方法 实现并测试 这个类 include using namespace std class Dog public void setAge int a age a int getAge return age void setWeight float w weight w float getWeight return weight private int age float weight void main Dog d d setAge 3 d setWeight 30 cout 小狗 d getAge 岁 重 d getWeight 斤 endl 4 9 设计并测试一个名为 Rectangle 的矩形类 其属性为矩形的左下角与右上角两个点的坐 标 根据坐标能计算矩形的面积 include include using namespace std class Rectangle public Rectangle int xx1 int yy1 int xx2 int yy2 x1 xx1 y1 yy1 x2 xx2 y2 yy2 float getArea return fabs x2 x1 fabs y2 y1 private int x1 y1 int x2 y2 void main Rectangle rec 0 0 10 20 cout 矩形面积 rec getArea endl 4 11 定义并实现一个矩形类 有长 宽两个属性 由成员函数计算矩形的面积 include using namespace std class Rectangle public Rectangle int l int w length l width w float getArea return length width private int length int width void main Rectangle rec 10 20 cout 矩形面积 rec getArea endl 4 13 定义一个 Circle 类 有数据成员 radius 半径 成员函数 getArea 计算圆的面积 构造一个 Circle 的对象进行测试 include using namespace std const float PI 3 1415 class Circle public Circle float r radius r float getArea return radius PI PI private float radius void main Circle c 5 5 cout 圆的面积 c getArea endl 4 20 定义一个复数类 Complex 使得下面的代码能够工作 Complex c1 3 5 Complex c2 4 5 c1 add c2 c1 show 源程序如下 include using namespace std class Complex public Complex float r 0 0 float i 0 0 real r image i void add Complex b real real b real image image b image void show cout real image i endl private float real 实部 float image 虚部 void main Complex c1 3 5 Complex c2 4 5 相当于 Complex c2 4 5 c1 add c2 c1 show 5 7 定义一个 Cat 类 拥有静态数据成员 numOfCats 记录 Cat 的个体数目 静态成员函数 getNumOfCats 读取 numOfCats 设计程序测试这个类 体会静态数据成员和静态成员 函数的用法 include using namespace std class Cat public Cat numOfCats Cat numOfCats static int getNumOfCats return numOfCats private static int numOfCats int Cat numOfCats 0 void main cout 现在的 Cat 数量 Cat getNumOfCats endl Cat a cout 现在的 Cat 数量 a getNumOfCats endl Cat b cout 现在的 Cat 数量 b getNumOfCats endl 5 14 定义 Boat 与 Car 两个类 二者都有 weight 属性 定义二者的一个友元函数 getTotalWeight 计算二者的重量和 include using namespace std class Car class Boat public Boat float w weight w friend float getTotalWeight Boat b Car c private float weight class Car public Car float w weight w friend float getTotalWeight Boat b Car c private float weight float getTotalWeight Boat b Car c return b weight c weight void main Boat boat 3500 Car car 1000 cout 船和汽车共重 getTotalWeight boat car 公斤 endl 7 5 定义一个基类 Shape 在此基础上派生出 Rectangle 和 Circle 二者都有 getArea 函数 计算对象的面积 使用 Rectangle 类创建一个派生类 Square include using namespace std const float PI 3 14 class Shape public Shape float a float b 0 0 this a a this b b protected float a b class Rectangle public Shape public Rectangle float l float w Shape l w float getArea return a b class Circle public Shape public Circle float r Shape r float getArea return a PI PI class Square public Rectangle public Square float l Rectangle l l float getArea return a a void main Rectangle r 10 20 Circle c 5 Square s 10 cout 矩形的面积 r getArea endl cout 圆的面积 c getArea endl cout 正方形的面积 s getArea endl 7 6 定义一个哺乳动物类 Mammal 再由此派生出狗类 Dog 定义一个 Dog 类的对象 观 察基类与派生类的构造函数和析构函数的调用顺序 include using namespace std class Mammal public Mammal cout Constructing Mammal endl Mammal cout Desstructing Mammal endl class Dog public Mammal public Dog cout Constructing Dog endl Dog cout Desstructing Dog endl void main Dog d 7 8 定义一个 Document 类 有数据成员 name 从 Document 派生出 Book 类 增加数据成 员 pageCount include using namespace std class Document public Document char n strcpy name n void show cout name private char name 50 class Book public Document public Book char n int p Document n pageCount p void show cout 书名 Document show cout endl 页数 pageCount endl private int pageCount void main Book book C 语言程序设计 529 book show 7 10 定义一个 Object 类 有数据成员 weight 及相应的操作函数 由此派生出 Box 类 增 加数据成员 height 和 width 及相应的操作函数 声明一个 Box 对象 观察构造函数与析构 函数的调用顺序 include using namespace std class Object public Object cout Constructing Object endl Object cout Destructing Object endl void setWeight int w weight w int getWeight return weight private int weight class Box public Object public Box cout Constructing Box endl Box cout Destructing Box endl void setHeight int h height h int getHeight return height void setWidth int w width w int getWidth return width private int height int width void main Box box box setHeight 5 box setWidth 10 box setWeight 8 cout 盒子 高 box getHeight 宽 box getWidth 重 box getWeight endl 8 4 include using namespace std class Counter public Counter int ii 0 i ii void print cout i i endl Counter operator int a Counter temp temp i i a return temp private int i void main Counter c c c 3 c print c c 5 c print 8 5 include using namespace std class Mammal public virtual void speak cout Mammal Speak endl class Dog public Mammal public virtual void speak cout

温馨提示

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

评论

0/150

提交评论