====hello world=====
#i nclude "stdafx.h"
int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
====累加器====
#i nclude "stdafx.h"
#i nclude "iostream"
using namespace std;
int main()
{
int ir=0;
for(int i=1;i<=100;i++){
ir+=1;
}
cout<<"value is "<<ir<<endl;
return 0;
}
========布尔型变量=====
#i nclude "stdafx.h"
#i nclude "iostream"
using namespace std;
int main()
{
bool t=true;
cout<<t<<endl;
t=false;
cout<<t<<endl;
return 0;
}
=======数组和指针方式定义字符串====
#i nclude "stdafx.h"
#i nclude "iostream"
using namespace std;
int main()
{
char* str1="hi";
char str2[]="hi,hi";
cout<<str1<<endl<<str2<<endl; //原来*str1代表着指针所指向的字符串的第一个值
return 0;
}
========判断字符串长度,构造了一个函数,用指针来移位.=====
#i nclude "stdafx.h"
#i nclude "iostream"
using namespace std;
int getleng(char* str){
int lengstr=0;
while(*str++){
lengstr+=1;
}
return lengstr;
};
int main()
{
char* msg="hello,world!";
int lengstr2;
lengstr2=getleng(msg);
cout<<lengstr2<<endl;
return 0;
};
========string 数据类型=======
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int main()
{
char* msg="hello,world!";
string str1="aaa";
string str2="bbb";
string str3(msg);
cout<<str1<<endl<<str3<<endl;
return 0;
};
=========定义枚举类型,结合case=====
#i nclude "stdafx.h"
#i nclude "iostream"
using namespace std;
enum family_member{father=0,mother=1,xiaoming=3};
void print_mem(family_member member){
switch(member) {
case father:
cout<<"oh,he is my fahter"<<endl;
break;
case mother:
cout<<"oh,he is my mother"<<endl;
break;
case xiaoming:
cout<<"oh,he is xiaoming"<<endl;
break;
default:
cout<<"I dont know who he is"<<endl;
}
}
int main()
{
for (int i=0;i<4;i++) {
print_mem((family_member) i);
};
return 0;
};
=========结构体的定义与使用=====
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
struct student {
string Name;
bool IsBoy;
int age;
};
void printinfo(student s){
cout<<"姓名:"<<s.Name<<endl;
cout<<"年龄:"<<s.age<<endl;
if (s.IsBoy) {
cout<<"性别为男"<<endl;
}
else{
cout<<"性别为女"<<endl;
};
};
int main()
{
student xiaoming;
xiaoming.Name="小明";
xiaoming.IsBoy=true;
xiaoming.age=23;
printinfo(xiaoming);
return 0;
};
=====数组的定义,赋值,取长,计算
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int main(){
int m[]={1,2,3,4,5};
int lengthm=sizeof(m)/sizeof(int); //数组的个数
for (int i=0;i<lengthm;i++) {
m[i]++;
};
for (i=0;i<lengthm;i++)
{
cout<<m[i]<<endl;
}
return 0;
}
====用指针来代表数组
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int main(){
int m[]={1,2,3,4,5};
int lengthm=sizeof(m)/sizeof(int);
int *p=m;
for (int i=0;i<lengthm;i++)
{
cout<<*p<<endl; //如果p,则输入指针地址
p++; //*p++也可以
}
return 0;
}
======指针定义,取地址与占空间=
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
struct student {
string Name;
bool IsBoy;
int age;
};
int main()
{
student xiaoming;
xiaoming.Name="小明";
xiaoming.IsBoy=true;
xiaoming.age=23;
student* pStu=0; //定义指针
pStu=&xiaoming; //指向xiaoming的地址
int* pInt=0;
pInt=&xiaoming.age;
bool* pBool=0;
pBool=&xiaoming.IsBoy;
string* pString=0;
pString=&xiaoming.Name;
cout<<pStu<<endl; //输入地址
cout<<pInt<<endl;
cout<<pBool<<endl;
cout<<pString<<endl;
cout<<"===="<<endl;
cout<<sizeof(pStu)<<endl; //指针所占的空间是一样的.
cout<<sizeof(pInt)<<endl;
cout<<sizeof(pBool)<<endl;
cout<<sizeof(pString)<<endl;
cout<<"===="<<endl;
cout<<sizeof(*pStu)<<endl; //指针的值所占空间
cout<<sizeof(*pInt)<<endl;
cout<<sizeof(*pBool)<<endl;
cout<<sizeof(*pString)<<endl;
cout<<"===="<<endl;
cout<<sizeof(xiaoming)<<endl; ////指针的值所占空间
cout<<sizeof(xiaoming.age)<<endl;
cout<<sizeof(xiaoming.IsBoy)<<endl;
cout<<sizeof(xiaoming.Name)<<endl;
return 0;
};
========指针加====
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int main()
{
int i=51;
int* p=&i;
cout<<i<<*p<<endl;
i++;
cout<<i<<endl;
i=*p+1;
cout<<i<<endl;
cout<<"==="<<endl;
i=*p+1;
cout<<*p<<endl;
return 0;
};
========获取类的静态成员=======
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
class M
{
public:
static int i;
static void print(int* ptr){
if(ptr){
cout<<*ptr<<endl;
}
}
};
int M::i=100;
int main()
{
M::i++;
M::print(&M::i);
return 0;
};
======变量生效的范围
#i nclude "stdafx.h"
#i nclude "iostream"
#i nclude "string"
using namespace std;
int i=9; //这个是全局的
void print(){
int i=20; //这个是print里的
cout<<i<<endl;
}
int main()
{
int i=100; //这个是main里的
cout<<i<<endl;
print();
::i++;
cout<<::i<<endl;
return 0;
};