尽量使用含义直观的常量来表示那些将在程序中多次出现的数字或 字符串
尽量使用含义直观的常量来表示那些将在程序中多次出现的数字或 字符串。
例如: #define MAX 100 /* C 语言的宏常量 */ const int MAX = 100; // C++ 语言的 const 常量 const float PI = 3.14159; // C++ 语言的 const 常量
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 //定义抽象类 6 class Base { 7 public: 8 //定义两个纯虚函数 9 virtual void aFn1(void)=0; 10 virtual void aFn2(void)=0; 11 }; 12 13 //派生类Derived_1中覆盖了基类中的纯虚函数 14 class Derived_1:public Base 15 { 16 public: 17 void aFn1(void) { 18 cout<<"aFnl is in First derived class."<<endl; 19 } 20 void aFn2(void) { 21 cout<<"aFn2 is in First derived class."<<endl; 22 } 23 }; 24 25 //派生类Derived_2中覆盖了基类中的纯虚函数 26 class Derived_2:public Base{ 27 public: 28 virtual void aFn1(void){ 29 cout<<"aFn1 is in Second derived class."<<endl; 30 } 31 void aFn2(void){ 32 cout<<"aFn2 is in Second derived class."<<endl; 33 } 34 }; 35 36 37 int main(int argc, char** argv) { 38 39 //用抽象类不能创建对象 40 // Base b; 语法错误 41 // b.aFn1(); 42 // b.aFn2(); 43 44 //创建和使用Derived_1类的对象 45 Derived_1 d1; 46 cout<<"Derived_1 d1:"<<endl; 47 d1.aFn1(); 48 d1.aFn2(); 49 cout<<"------------------"<<endl; 50 51 //创建和使用Derived_2类的对象 52 Derived_2 d2; 53 cout<<"Derived_2 d2:"<<endl; 54 d2.aFn1(); 55 d2.aFn2(); 56 return 0; 57 }