VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 401|回复: 2

怎么把这个程序改成,show()动态联编,可以输出多组数据的?

[复制链接]
65_avatar_middle
最佳答案
0 
在线会员 发表于 2022-4-29 11:26:19 | 显示全部楼层 |阅读模式


怎么把这个程序改成,show()动态联编,可以输出多组数据的?



  1. #include<bits/stdc++.h>

  2. using namespace std;

  3. class vehicle{//车类,作为基类

  4. protected:

  5. int MaxSpeed ,Weight ;

  6. //最大速度,重量

  7. public:

  8. void virtual show(){

  9. };

  10. };

  11. class bicycle: virtual public vehicle {//自行车类:公有继承虚基类vehicle

  12. protected:

  13. int Height ;

  14. //高度

  15. public:

  16. void Setdata1(int ms,int wt,int ht)

  17. { MaxSpeed=ms; Weight=wt; Height=ht;};

  18. void Show(){

  19. cout<<"公共属性:maxSpeed is"<<MaxSpeed<<",weight is"<<Weight

  20. <<",Bicycle属性:height is"<<Height<<endl;

  21. }

  22. };

  23. class motorcar : public vehicle {//摩托车类:公有继承自行车类和汽车类

  24. protected:

  25. int SeatNum ; //座位数

  26. public:

  27. void Setdata(int ms,int wt,int sn)

  28. { MaxSpeed=ms; Weight=wt;SeatNum=sn; };

  29. void Show(){

  30. cout<<"公共属性:maxSpeed is"<<MaxSpeed<<",weight is"<<Weight

  31. <<",Vehicle属性:seatNum is"<<SeatNum<<endl;

  32. }

  33. };

  34. main(){

  35. bicycle mt;

  36. motorcar mr;

  37. while(1){

  38. cout<<"Bicycle请输入-0\n"<<"Motocar请输入-1\n"<<"退出结束-2\n";

  39. cout<<"请输入:";

  40. int m;

  41. cin >>m;

  42. int maxspeed1,weight1,height1,seatnum1;

  43. switch(m){

  44. case 0:

  45. cout<<"请输入maxSpeed:" ;

  46. cin>>maxspeed1;

  47. cout<<"请输入weight:";

  48. cin>>weight1;

  49. cout<<"请输入height:";

  50. cin>>height1;

  51. mt.Setdata1(maxspeed1,weight1,height1);

  52. break;

  53. case 1:

  54. cout<<"请输入maxSpeed:" ;

  55. cin>>maxspeed1;

  56. cout<<"请输入weight:";

  57. cin>>weight1;

  58. cout<<"请输入seatNum:";

  59. cin>>seatnum1;

  60. mr.Setdata(maxspeed1,weight1,seatnum1);

  61. break;

  62. case 2:

  63. mt.Show();

  64. mr.Show();

  65. system("pause");

  66. return 0;

  67. }

  68. }

  69. }


复制代码

输入
0
1
2
3
1
5
6
7
2
输出
Bicycle请输入-0
Motocar请输入-1
退出结束-2
请输入:0
请输入maxSpeed:1
请输入weight:2
请输入height:3
Bicycle请输入-0
Motocar请输入-1
退出结束-2
请输入:1
请输入maxSpeed:5
请输入weight:6
请输入seatNum:7
Bicycle请输入-0
Motocar请输入-1
退出结束-2
请输入:2
公共属性:maxSpeed is1,weight is2,Bicycle属性:height is3
公共属性:maxSpeed is5,weight is6,Vehicle属性:seatNum is7




上一篇:静态库编译后在实例程序中编译运行没有报错,但是mfc中报语法错误
下一篇:Windows编程的数据类型
61_avatar_middle
最佳答案
0 
在线会员 发表于 2022-4-29 11:48:30 | 显示全部楼层
又学习了,不错不错!
17_avatar_middle
最佳答案
40 
在线会员 发表于 2022-4-29 14:16:24 | 显示全部楼层
本帖最后由 yoobaby 于 2022-4-29 14:20 编辑

不爱改代码,之前学习的笔记内容给你参考

  1. class Animal
  2. {
  3. public:
  4.     //加上关键字virtual 变成虚函数,那么编译的时候就不能确定函数地址了。
  5.     virtual void speak() {
  6.         cout << "动物在说话" << endl;
  7.     }
  8. };

  9. class Cat :public Animal {
  10.     void speak() {
  11.         cout << "小猫在说话" << endl;
  12.     }
  13. };

  14. //多态的满足条件:
  15. //1.存在继承关系,且基类使用虚方法。
  16. //2.在派生类中重新定义基类的方法。
  17. //多态使用:使用父类的指针或引用来指向子类对象
  18. void doSpeak(Animal& animal) {
  19.     animal.speak();
  20. }
  21. int main() {
  22.     Cat cat;
  23.     doSpeak(cat);
  24. }
复制代码


  1. class AbstractCalculator {
  2. public:
  3.     virtual int getResult(int num1, int num2) {
  4.         return 0;
  5.     }
  6. };

  7. class AddCalculator :public AbstractCalculator {
  8. public:
  9.     int getResult(int num1, int num2) {
  10.         return num1 + num2;
  11.     }
  12. };

  13. int main() {
  14. //使用指针指向子类的对象。
  15. AbstractCalculator* c = new AddCalculator;
  16. cout << c->getResult(3, 3) << endl;
  17. delete c;
复制代码
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-6-10 05:13

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表