一、不同
1.可同时定义并初始化多个变量
2.有string字符串类型
3.可在循环中定义变量
# include <iostream>
using namespace std;
int main ( )
{ int a= 1 , b= 2 ; string name; char array[ 3 ] ; for ( int i= 1 ; i<= 3 ; i++ ) { cin>> array[ i] ; cout<< "array:" << array[ i] << "," << endl; } cin>> name; cout<< "name:" << name<< "\n" << endl; cout<< "a:" << a<< "\n" << "b:" << b<< endl;
}
4.动态内存分配关键字不同
# include <iostream>
# include <cstring>
using namespace std;
int main ( )
{ int * p= new int ; char * q= new char [ 3 ] ; strcpy ( q, "堆内存的动态分配" ) ; * p= 100 ; cout<< "指针p的值:" << * p<< endl; cout<< q<< endl; delete [ ] q; delete p; }
5.可以定义函数的结构体类型
# include <iostream>
using namespace std;
struct function { void sayHi ( char * name) { cout<< "hi," << name<< endl; } void sayBye ( char * name) { cout<< "bye," << name<< endl; }
} ;
int main ( )
{ struct function fun; fun. sayHi ( "c++" ) ; fun. sayBye ( "c++" ) ;
}
二、拓展
特殊运算符的应用
# include <iostream>
using namespace std;
int main ( )
{ int a= 10 , b= 12 , flag= ~ a; cout<< "~a:" << flag<< endl; flag= a& b; cout<< "a&b:" << flag<< endl; flag= a| b; cout<< "a|b:" << flag<< endl; flag= a^ b; cout<< "a^b:" << flag<< endl; flag= a<< 1 ; cout<< "a<<1:" << flag<< endl; flag= a>> 1 ; cout<< "a>>1:" << flag<< endl; }