文档介绍:第一章 C++中非面向对象的新特征
1
注释
C++支持两种注释格式
/* */
// 行注释
/*
program: example1
print 0 to 9
*/
#include <>
void main()
{
int i;
for( i=0;i<10;i++)
cout <<i<<"\n"; // display i
}
2
输出
C++ cout 输出方式流机制
需要有#include <>
格式:cout<<表达式1<< ...<<表达式n
cout<<"Hello " << " world .\n";
cout<<"Hello world . " <<endl;
3
输入
C++ cin
需要有#include <>
格式:cin>>变量1>>...>>变量n
cin>>i>>j;
4
const 说明符
C #define N 10
C++ const int N = 10;
格式 const 数据类型变量名=常量值
在运行时不能修改const修饰的对象的值
5
const 说明符
防止某些变量的值被意外地修改
void print_value(const int v)
{
cout << v <<"\n";
}
6
引用
int a=10;
int &b=a;
b=20;
cout<<"a="<<a<<"\n";
cout<<"b="<<b<<"\n";
a=20
b=20
7
引用
swap函数的实现
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b= temp;
}
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b= temp;
}
8
变量
C++允许在函数内的任何地方声明变量
void makeit(void)
{
// 200 lines of code
char *cp=new char[100];
for(int i=0;i<100;i++){
// do something in this loop
}
// more code
}
9
变量
struct student
{ int a;
float b
};
struct student s; /* C语法*/
student s; // C++语法
10