效果:
1 //运行环境 dev c++ 5.9.2 2 #include <iostream> 3 #include <windows.h> //Sleep函数调用的头文件 4 #include <mmsystem.h> //媒体控制函数的头文件 5 //在VC2008和VC6.0环境中从此头文件 #pragma comment(lib, "winmm.lib") 6 using namespace std; 7 8 /* 9 了解类的调用 10 */ 11 12 class automobile 13 { 14 private: 15 char type[20]; 16 char color[20]; 17 float price; 18 int carry_weight; 19 int carry_customer; 20 public: 21 void set_date(char *t, char *c, float pri, int cw, int cc); //初始化或修改数据成员 22 void movecar(int l, int k); 23 void horming(int num); 24 void downcar(int l); 25 void play_mp3(char *ps); 26 char *show_type() 27 { 28 return type; 29 } 30 31 }; 32 33 void automobile::set_date(char *t, char *c, float pri, int cw, int cc) 34 { 35 strcpy(type, t); 36 strcpy(color, c); 37 price = pri; 38 carry_weight = cw; 39 carry_customer = cc; 40 } 41 //控制car 移动 42 void automobile::movecar(int l, int k) 43 { 44 cout<<"\n"<<type<<"水平直线运动:"<<endl; 45 for(int i=0; i<l; i++) 46 { 47 cout<<' '<<"o-o"; 48 Sleep(1000/k); 49 cout<<"\b\b\b"; 50 51 } 52 } 53 //汽车下降 54 void automobile::downcar(int l) 55 { 56 //垂直下降运动 57 cout<<"\n"<<type; 58 for(int i=0; i<l; i++) 59 { 60 cout<<"o_o"; 61 Sleep(500); 62 cout<<"\b\b\b"<<" "; 63 cout<<endl; 64 } 65 } 66 //调用系统提示音 67 void automobile::horming(int num) 68 { 69 for(int i=0; i<num; i++) 70 { 71 cout<<type; 72 cout<<"\007"<<"di.."<<endl; 73 Sleep(1000); 74 } 75 } 76 //重点 播放歌曲 77 78 void automobile::play_mp3(char *ps) 79 { 80 char str[100]="play "; //play后面有空格 81 strcat(str,ps); 82 cout<<str; 83 mciSendString(str, NULL, 0, NULL); 84 //如果不进行如下操作的话:会报错误: 85 // C:\Users\sjcnh\AppData\Local\Temp\cczFSVYd.o Car.cpp:(.text+0x2c6): undefined reference to `__imp_mciSendStringA' 86 // D:\060417\collect2.exe [Error] ld returned 1 exit status 87 //在Dec-C++环境中还要进行设置; 88 //工具\编译器选项\编译器\在连接器命令中加入一下命令-lwinmm 89 //-static-libgcc 90 char a; 91 cin>>a; //输入任何字符结束播放 92 } 93 94 int main() 95 { 96 automobile nisson; 97 char tp[]="奥迪A9"; 98 char ys[]="white"; 99 nisson.set_date(tp, ys, 200000, 5, 3); 100 nisson.horming(5); 101 nisson.movecar(10, 10); 102 nisson.downcar(5); 103 char mp[]="情深深雨濛濛-赵薇.mp3"; 104 nisson.play_mp3(mp); 105 return 0; 106 // 1a2s3d45f67j8k90l90l 107 108 }