不要把程序中的复合表达式与“真正的数学表达式”混淆。
例如: if (a < b < c) // a < b < c 是数学表达式而不是程序表达式 并不表示 if ((a<b) && (b<c)) 而是成了令人费解的 if ( (a<b)<c )
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 //定义ex类 6 class ex_class 7 { 8 int a; 9 double b; 10 public: 11 ex_class(int n=1,double x=1.0):a(n),b(x) {} 12 void show_value(char *name) { 13 cout<<name<<" :"<<endl; 14 cout<<"a="<<a<<endl; 15 cout<<"b="<<b<<endl; 16 } 17 }; 18 19 //定义main()函数 20 21 int main(int argc, char** argv) { 22 //创建ex_class的对象并显示 23 ex_class obj1,obj2(100,3.5); 24 obj1.show_value("obj1"); 25 obj2.show_value("obj2"); 26 27 //创建ex_class的指针变量 28 ex_class *p; 29 30 //p指向obj1并显示 31 p=&obj1; 32 p->show_value("p->obj1"); 33 34 //p指向obj2并显示 35 p=&obj2; 36 (*p).show_value("(*p)obj2"); 37 38 //p指向动态创建的对象并显示 39 p=new ex_class; 40 p->show_value("p->new"); 41 42 delete p; //删除对象 43 44 return 0; 45 }