1 /* 2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited 3 */ 4 5 #include <deque> 6 #include <exception> 7 #include <iomanip> 8 #include <ios> 9 #include <algorithm> 10 #include <bitset> 11 #include <cctype> 12 #include <stdexcept> 13 #include <utility> 14 #include <cerrno> 15 #include <complex> 16 #include <istream> 17 #include <queue> 18 #include <set> 19 #include <cwchar> 20 #include <fenv.h> 21 #include <inttypes.h> 22 #include <stdbool.h> 23 #include <stdint.h> 24 #include <tgmath.h> 25 #include <map> 26 #include <sys/time.h> 27 #include <stdio.h> 28 #include <vector> 29 #include <hash_set> 30 #include <ext/hash_map> 31 #include <string.h> 32 #include <pthread.h> 33 #include <functional> 34 #include <list> 35 #include <unistd.h> 36 #include <numeric> 37 #include <iterator> 38 39 #include "cmsis_os.h" 40 #include "hal_trace.h" 41 //#include "hal_timer.h" 42 43 #define NUM_THREADS 2 44 45 //#pragma STDC FENV_ACCESS ON 46 47 //Setting this parameter represents the state value that can access floating-point operation exceptions 48 using namespace std; 49 using namespace __gnu_cxx; 50 51 ////////////////////////static and dynamic polymorphic///////////////// 52 Add(int left,int right)53static int Add(int left, int right) 54 { 55 return left + right; 56 } Add(float left,int right)57static float Add(float left, int right) 58 { 59 return left + right; 60 } 61 62 class TakeBus 63 { 64 public: TakeBusToSubway()65 void TakeBusToSubway() 66 { 67 char plan[]="go to Subway--->please take bus of 318"; 68 } TakeBusToStation()69 void TakeBusToStation() 70 { 71 char plan[]="go to Station--->pelase Take Bus of 306 or 915"; 72 } 73 }; 74 75 class Bus 76 { 77 int a; 78 public: 79 virtual void TakeBusToSomewhere(TakeBus& tb) = 0; 80 }; 81 82 class Subway :public Bus 83 { 84 public: TakeBusToSomewhere(TakeBus & tb)85 virtual void TakeBusToSomewhere(TakeBus& tb) 86 { 87 tb.TakeBusToSubway(); 88 } 89 }; 90 91 class Station :public Bus 92 { 93 public: TakeBusToSomewhere(TakeBus & tb)94 virtual void TakeBusToSomewhere(TakeBus& tb) 95 { 96 tb.TakeBusToStation(); 97 } 98 }; 99 100 101 TakeBus _tb; 102 Subway _sbw; 103 Station _statn; 104 105 /*********************************************************/ cpp_polymorphic(void)106static int cpp_polymorphic(void) 107 { 108 /////////////////static///////////////// 109 int a; 110 TakeBus tb; 111 float b; 112 a=Add(10, 20); 113 b=Add(10.5f,20); 114 /////////////////////dynamic///////////////////// 115 Bus* bu = NULL; 116 117 if ((rand() % 2) & 1) 118 bu = new Subway; 119 else 120 bu = new Station; 121 122 bu->TakeBusToSomewhere(tb); 123 TRACE("%-32s%s", __FUNCTION__, ((30!=a) || (30.5!=b) || (bu==NULL))?"FAIL":"PASS"); 124 delete bu; 125 bu=NULL; 126 return 0; 127 } 128 129