试你编写的类。
2、 #include 8、 float getMJ(){return radis*radis*pi;} 9、 float getZC(){return radis*2*pi;} 10、 R(float in){radis=in;} 11、 }; 12、 void main() 13、 { 14、 15、 16、 } 2编写一个程序。用名为max的函数模板计算三个参数中的最大值,用整数、字符和浮点数测试所编写的程序。 17、 #include 20、 O Max(O a,O b, O c){return a>b?a>c?a:c:b>c?b:c;} 21、 void main() 22、 { 23、 24、 25、 26、 } 27、 设计一个立方体类Box,它能计算并输出立方体的体积和表面积。 28、 #include 33、 float getBMJ(){return L*L*6;} R r(10); cout<<\"半径:10\\n周长:\"<<()<<\"\\n面积:\"<<(); cout< 37、 void main() 38、 { 39、 40、 41、 } 4、编写一个程序计算“三角形、正方形、圆形”三种图形的面积。 要求: a)抽象出一个基类base; b)在其中说明一个虚函数用来求面积; c)利用派生类定义“三角形、正方形、圆形”; d)编写主函数并测试。 Box r(10); cout<<\"边长:10\\n表面积:\"<<()<<\"\\n体积:\"<<(); #include 5. virtual float getMJ(){return H*W;} 6. float H,W; 7. }; 8. class R:public base 9. {public: 10. float getMJ(){return H*H*;} 11. R(float in){H=in;} 12. }; 13. class A:public base 14. {public: 15. float getMJ(){return (H*W)/2;} 16. A(float in_H,float in_w){H=in_H;W=in_w;} 17. }; 18. class S:public base 19. {public: 20. float getMJ(){return H*H;} 21. S(float in){H=in;} 22. }; 1. 23. void main() 24. { 25. R r(10); 26. A a(10,5); 27. S s(10); 28. cout<<\"圆:边长:10\\n29. 30. 31. } 5、定义一个处理日期的类TDate,它有3个私有数据成员:Month,Day,Year和若干个公有成员函数,并实现如下要求:①构造函数重载;②成员函数设置缺省参数;③定义一个友元函数来打印日期;④定义一个非静态成员函数设置日期;⑤可使用不同的构造函数来创建不同的对象。 面积:\"<<()< 5. public: 6. TDate():Year(1900),Month(1),Day(1){;} 7. TDate(int Y, int M=1, int D=1){Month=M;Day=D;Year=Y;} 8. void set(int Y=1990, int M=1, int D=1){Month=M;Day=D;Year=Y;} 9. friend void show(TDate& in); 10. private: 11. int Month,Day,Year; 12. }; 13. void show(TDate& in){cout<<<<\"年\"<<<<\"月\"<<<<\"日\\n\";} 14. void main() 15. { 16. TDate t1; 17. TDate t2(2014); 18. TDate t3(2015,6,5); 19. show(t1); 20. show(t2); 21. show(t3); 22. (1999); 23. show(t3); 24. } 1. 6、编程实现抽象类Employee,派生类Manger和HourlyWorker,Employee有数据成员姓名name和工号ID,Manger有数据成员sal,代表经理的月工资,HourlyWorker有wage和hours,分别代表钟点工的每小时的工资数和月工作时数,定义的所有类中必须包含构造函数、析构函数、修改和获取所有数据成员的成员函数,以及虚函数来计算职员的工资、输出职员的姓名name和工号ID。 #include 2. #include 6. string name; 7. int id; 8. virtual int getSal(){return 0;} 9. Employee():name(\"未命名\"),id(0){}; 10. ~Employee(){cout<<\"析构\\n\";} 11. void set(string N,int I){id=I; name=N; } 12. void showSal() 13. { 14. cout<<\"\\n姓名:\"< 21. public: 22. Manger(int S){sal=S;} 23. int getSal(){return sal;} 24. int sal; 25. }; 26. class HourlyWorker:public Employee 27. { 28. public: 29. HourlyWorker(int W,int H){wage=W;hours=H;} 30. int getSal(){return wage*hours;} 31. int wage,hours; 32. }; 33. void main() 34. { 1. 35. HourlyWorker h(10,20); 36. (\"小时工37. (); 38. Manger m(999); 39. (\"经理40. (); 41. } 8、 回答下面问题。 下面列出了由三个文件、和组成的一个程序。文件中实现了主函数;文件中定义了类MyClass;文件中实现了类MyClass的成员函数。题中没有给出三个文件的完整实现。仔细阅读所给的程序,根据题意补充文件和文件的内容。要求: a.将成员变量定义为私有的; b.不要增加题中没有用到的类成员。 A\",777); A\",888); /********************************************************/ 2. //文件 3. #include <> 4. #include \"\" 5. void main() 6. { 7. MyClass obj; 8. cout << () << endl; 9. ; 10. cout << () << endl; 11. } 12. /***********************************************************************/ 13. /文件 14. #include \"\" 15. float MyClass::GetMember() 16. { 17. return member; 18. } 19. void MyClass::SetMember(float member) 20. { 1. 21. MyClass::member=member; 22. } 23. //在这里写出构造函数的实现函数 24. MyClass::MyClass(float in) 25. { 26. Member=in; 27. } 28. /************************************************************* **********/ 29. //文件 30. //类MyClass的定义 31. class MyClass { 32. //在下面写出类的定义体 33. public: 34. MyClass(float in) 35. float GetMember(); 36. void SetMember(float member); 37. private: 38. float member; 39. }; 10、某公司雇员(employee)包括经理(manager),技术人员(technician)和销售员(salesman)。开发部经理(developermanger),既是经理也是技术人员。销售部经理(salesmanager),既是经理也是销售员。 以employ类为虚基类派生出manager,technician和salesman类;再进一步派生出developermanager和salesmanager类。 employee类的属性包括姓名、职工号、工资级别,月薪(实发基本工资加业绩工资)。操作包括月薪计算函数(pay()),该函数要求输入请假天数,扣去应扣工资后,得出实发基本工资。 technician类派生的属性有每小时附加酬金和当月工作时数,及研究完成进度系数。业绩工资为三者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。 salesman类派生的属性有当月销售额和酬金提取百分比,业绩工资为两者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。 manager类派生属性有固定奖金额和业绩系数,业绩工资为两者之积。工资总额也为基本工资加业绩工资。 而developermanager类,pay()函数是将作为经理和作为技术人员业绩工资之和的一半作为业绩工资。 salesamanager类,pay()函数则是经理的固 定奖金额的一半,加上部门总销售额与提成比例之积,这是业绩工资。 编程实现工资管理。特别注意pay()的定义和调用方法:先用同名覆盖,再用运行时多态。 同第6 题 9、 以点(point)类为基类,重新定义矩形类和圆类。点为直角坐标点,矩形水平放置, 由左下方的顶点和长宽定义。圆由圆心和半径定义。派生类操作判断任一坐标点是在图形内,还是在图形的边缘上,还是在图形外。缺省初始化图形退化为点。要求包括拷贝构造函数。编程测试类设计是否正确。 #include 2. #include 3. using namespace std; 4. const double PI=; 5. class Point{ 6. private: 7. double x,y; 8. public: 9. Point(){x = 0; y = 0; } 10. Point(double xv,double yv){x = xv;y = yv;} 11. Point(Point& pt){ x = ; y = ; } 12. double getx(){return x;} 13. double gety(){return y;} 14. double Area(){return 0;} 15. void Show(){cout<<\"x=\"< 20. Circle(){radius = 0;} 21. Circle(double xv,double yv,double vv):Point(xv,yv){radius = vv;} 22. Circle(Circle& cc):Point(cc){radius = ;} //拷贝构造函数 23. double Area(){return PI*radius*radius;} 24. void Show(){//注意怎样访问基类的数据成员 25. cout<<\"x=\"< 28. double distance = sqrt((getx()())*(getx()()) 1. 29. +(gety()())*(gety()())); 30. double s=distance-radius; 31. if(s==0) return 0; //在圆上 32. else if(s<0) return -1; //在圆内 33. else return 1; //在圆外 34. } 35. }; 36. class Rectangle:public Point{ 37. double width,length; 38. public: 39. Rectangle(){width=0; length=0; } 40. Rectangle(double xv,double yv,double wv,double lv):Point(xv,xv) { 41. width = wv; 42. length= lv; 43. } 44. Rectangle(Rectangle& rr):Point(rr){ 45. width = ; 46. length = ; 47. } 48. double Area(){return width*length;} 49. void Show(){ 50. cout<<\"x=\"< 54. int Rectangle::position(Point &pt){ 55. double s1,s2; 56. s1 = ()-getx()); s2=()-gety()); 57. if(((s1==0||s1==width)&&s2<=length)||((s2==0||s2==length)&&s1<=width)) return 0; 58. else if(s1 61. int main(){ 62. Circle cc1(3,4,5),cc2,cc3(cc1); 63. Rectangle rt1(0,0,6,8),rt2,rt3(rt1); 64. Point p1(0,0),p2(6,8),p3(3,3),p4(8,4),p5(8,8); 65. (); 66. (); 67. (); 68. (); 69. cout<<\"点70. (); 71. cout<<\"矩形72. (); 73. switch(p1)){ 74. case 0:cout<<\"在矩形上\"< 80. switch(p1)){ 81. case 0:cout<<\"在圆上\"< 87. cout<<\"矩形88. (); 89. switch(p2)){ 90. case 0:cout<<\"在矩形上\"< 96. switch(p2)){ 97. case 0:cout<<\"在圆上\"< rt3:\"<<'\'; cc3:\"<<'\'; p2:\"; rt3:\"<<'\'; cc3:\"<<'\'; } cout<<\"点p3:\"; (); cout<<\"矩形rt3:\"<<'\'; (); switch(p3)){ case 0:cout<<\"在矩形上\"< 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. } (); switch(p3)){ case 0:cout<<\"在圆上\"< cout<<\"矩形rt3:\"<<'\'; (); switch(p4)){ case 0:cout<<\"在矩形上\"< switch(p4)){ case 0:cout<<\"在圆上\"< cout<<\"矩形rt3:\"<<'\'; (); switch(p5)){ case 0:cout<<\"在矩形上\"< switch(p5)){ case 0:cout<<\"在圆上\"< 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuowenda.com 版权所有 湘ICP备2023022495号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务