欢迎来到得力文库 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
得力文库 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    c面向对象-实例题集锦.doc

    • 资源ID:630079       资源大小:87.99KB        全文页数:25页
    • 资源格式: DOC        下载积分:8金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要8金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    c面向对象-实例题集锦.doc

    -_C+面向对象实例:面向对象实例:C+面向对象类的实例题目二面向对象类的实例题目二 题目描述:编写一个程序,设计一个产品类 Product,其定义如下:cpp view plaincopyprint?1.class Product 2. 3. public: 4. Product(char *n,int p,int q); /构造函数 5. Product(); /析构函数 6. void buy(int money); /购买产品 7. void get() const; /显示剩余产品数量 8. private: 9. char * name; /产品名称 10. int price; /产品单价 11. int quantity; /剩余产品数量 12. ; 并用数据进行测试。code:cpp view plaincopyprint?1.#include 2.#include 3.using namespace std; 4.class Product 5. 6. char *name; 7. int price; 8. int quantity; 9. public: 10. Product(char *n,int p,int q); 11. Product(); 12. void buy(int money); 13. void get()const; 14. ; -_15. Product:Product(char *n,int p,int q) 16. 17. name = n; 18. price = p; 19. quantity = q; 20. 21. Product:Product() 22. 23. 24. void Product:buy(int money) 25. 26. int r,n; 27. n = money/price; 28. r = money%price; 29. if(n > quantity) 30. 31. cout 2.using namespace std; 3.class CData 4. 5. public: 6. CData(int y,int m,int d); 7. void setdate(int y, int m, int d); 8. void display(); 9. void add(); 10. private: 11. int day; 12. int month; 13. int year; 14. ; 15. CData:CData(int y,int m,int d) 16. -_17. day = d; 18. month = m; 19. year = y; 20. 21. void CData:setdate(int y,int m,int d) 22. 23. day = d; 24. month = m; 25. year = y; 26. 27. void CData:display() 28. 29. coutday)day+; 40. else 41. 42. month+; 43. if(month>12) 44. 45. year+; 46. month = 1; 47. 48. day = 1; 49. 50. 51. else /平年的情况 52. 53. if(a0month-1>day)day+; 54. else 55. 56. month+; 57. if(month>12) 58. 59. year+; 60. month = 1; -_61. 62. day = 1; 63. 64. 65. 66. int main() 67. 68. CData date(2013,12,31); 69. date.display(); 70. date.add(); 71. date.display(); 72. date.setdate(2014,11,11); 73. date.display(); 74. date.add(); 75. date.display(); 76. return 0; 77. 结果输出:cpp view plaincopyprint?1.31/12/2013 2.1/1/2014 3.11/11/2014 4.12/11/2014 C+面向对象类的实例题目四面向对象类的实例题目四 题目描述:以面向对象的概念设计一个类,此类包含 3 个私有数据:unlead、lead(无铅汽油和有铅汽油)以及 total(当天总收入,无铅汽油的价格是 17 元/升,有铅汽油的加个是 16 元/升),请以构造函数方式建立此值。试输入某天所加的汽油量,本程序将列出加油当天的总收入。程序代码:cpp view plaincopyprint?1.#include 2.using namespace std; 3.class Gas 4. 5. public: 6. Gas(double ulp,double lp) -_7. 8. unprice = ulp; 9. price = lp; 10. 11. void show() 12. 13. total = unlead*unprice + lead*price; 14. cout>unlead; 21. cout>lead; 23. 24. private: 25. double unprice; 26. double price; 27. double lead; 28. double unlead; 29. double total; 30. ; 31. int main() 32. 33. Gas g1(17,16); 34. g1.getdata(); 35. g1.show(); 36. return 0; 37. 程序输出:cpp view plaincopyprint?1.请输入当天无铅汽油的总量:10 2.请输入当天有铅汽油的总量:20 3.无铅汽油的价格为 17 元/升,有铅汽油的价格为 16 元/升 4.total:490 -_C+面向对象类的实例题目五面向对象类的实例题目五 题目描述:编写一个程序,采用一个类求 n!,并输出 5!的值。程序代码:cpp view plaincopyprint?1.#include 2.using namespace std; 3.class CFactorial 4. 5. public: 6. CFactorial(int n) 7. 8. num = n; 9. total = 1; 10. 11. void calculate() 12. 13. int n = num; 14. while(n>0) 15. 16. total *= n-; 17. 18. 19. void display() 20. 21. cout 2.using namespace std; 3.class Rectangular 4. 5. public: 6. Rectangular(double w,double l) 7. 8. width = w; 9. length = l; 10. 11. double getc() 12. 13. circumference = width + length; 14. return circumference; 15. 16. double adddata(Rectangular 19. 20. private: 21. double width; 22. double length; 23. double circumference; 24. ; 25. int main() 26. 27. Rectangular r1(2,3); -_28. cout 2.#include 3.using namespace std; 4.class Student 5. 6. public: 7. void getdata(); 8. void showdata(); 9. private: 10. string number; 11. string name; 12. class Cdegree -_13. 14. public: 15. double math; 16. double english; 17. double phy; 18. degree; 19. ; 20. void Student:getdata() 21. 22. cout>number; 24. cout>name; 26. cout>degree.math; 28. cout>degree.english; 30. cout>degree.phy; 32. 33. void Student:showdata() 34. 35. cout 2.using namespace std; 3.class Student 4. 5. public: 6. void getscore(); /获取一个学生成绩 7. void display(); /显示一个学生成绩 8. void sort( Student *); /将若干个学生按总分从高到低排序 9. private: 10. int english; 11. int computer; 12. int total; 13. ; 14. void Student:getscore() 15. 16. cout>english; 18. cout>computer; 20. total = english + computer; 21. 22. void Student:display() -_23. 24. couttotal > total) /p 指向的对象比该对象大的时候,则交换对象的值29. 30. int t1,t2,t3; 31. t1 = p->english; 32. p->english = english; 33. english = t1; 34. t2 = p->computer; 35. p->computer = computer; 36. computer = t2; 37. t3 = p->total; 38. p->total = total; 39. total = t3; 40. 41. 42. int main() 43. 44. Student st3; 45. for(int i = 0; i 2.#include 3.using namespace std; 4.class Person 5. 6. public: 7. void get() 8. 9. cout>number; 11. cout>name; 13. 14. void show() 15. -_16. cout>class_number; 31. cout>grade; 33. 34. void show() 35. 36. Person:show(); 37. cout>title; 52. cout>department; 54. 55. void show() 56. 57. Person:show(); 58. cout 2.using namespace std; 3.class Vehicle 4. 5. public: 6. Vehicle(int wl,double wh):wheels(wl),weight(wh); 7. void show() 8. 9. cout 2.#include 3.#define PAI 3.1415 4.using namespace std; 5.class Shape 6. 7. public: 8. virtual float area() /定义一个求面积的成员函数 9. 10. return 0; 11. 12. virtual void ShapeName() = 0;/定义一个纯虚函数 13. ; 14. class Triangle:public Shape 15. 16. public: 17. Triangle(float x,float y,float z):a(x),b(y),c(z); 18. void ShapeName() 19. 20. coutShapeName(); 68. coutarea()ShapeName(); 72. coutarea()ShapeName(); 76. coutarea() 2.#define PAI 3.1415 3.using namespace std; 4.class Shape 5. 6. public: 7. virtual void ShapeName()=0; 8. virtual void area() 9. 10. return ; 11. 12. virtual void volume() 13. 14. return ; 15. 16. ; 17. class Cube:public Shape 18. 19. public: 20. Cube(float len):length(len); 21. void ShapeName() 22. 23. coutShapeName(); 86. pt->area(); 87. pt->volume(); 88. coutShapeName(); 91. pt->area(); 92. pt->volume(); 93. coutShapeName(); 96. pt->area(); 97. pt->volume(); 98. cout 2.using namespace std; 3.class Vehicle 4. 5. public: 6. Vehicle(int wl,double wh):wheels(wl),weight(wh); 7. void show() 8. 9. cout<<“wheels:“<<wheels<<“,weight:“<<weight<<endl; 10. 11. protected: 12. int wheels; 13. double weight; 14. ; 15. class Car:private Vehicle 16. 17. public: 18. Car(int wl,double wh,int pl):Vehicle(wl,wh),passager_load(pl); 19. void show() 20. 21. Vehicle:show(); 22. cout<<“passager_load:“<<passager_load<<endl; 23. 24. private: -_25. int passager_load; 26. ; 27. class Truck:private Vehicle 28. 29. public: 30. Truck(int wl,double wh,int pl,double psl):Vehicle(wl,wh),passager_load(pl),payload(psl); 31. void show() 32. 33. Vehicle:show(); 34. cout<<“passager_load:“<<passager_load<<endl; 35. cout<<“payload:“<<payload<<endl; 36. 37. private: 38. int passager_load; 39. double payload; 40. ; 41. int main() 42. 43. Vehicle v1(4,200); 44. v1.show(); 45. cout<<“=“<<endl; 46. Car c1(4,290,10); 47. c1.show(); 48. cout<<“=“<<endl; 49. Truck t1(4,500,50,200); 50. t1.show(); 51. return 0; 52. 结果输出:cpp view plaincopyprint?1.wheels:4,weight:200 2.= 3.wheels:4,weight:290 4.passager_load:10 5.= 6.wheels:4,weight:500 7.passager_load:50 8.payload:200 -_

    注意事项

    本文(c面向对象-实例题集锦.doc)为本站会员(小**)主动上传,得力文库 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知得力文库 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于得利文库 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

    © 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

    黑龙江省互联网违法和不良信息举报
    举报电话:0468-3380021 邮箱:hgswwxb@163.com  

    收起
    展开