类、结构体、指针与引用
#include <iostream>
usingnamespacestd ;
structPerson
{
intage ,height;
doublemoney;
Person(int_age,int_height,double_money)//构造函数
{
age=_age ;
height=_height;
money=_money;
}//Person(int _age,int _height,int _money):age = (_age),height = (_height),money = (_money)
};
intmain()
{
Personp= {18,180,100.0};
cout<<p.money<<endl;
return0;
}
#include <iostream>
usingnamespacestd ;
chare,f;
intmain()
{
charc='a';
cout<< (void*)&c<<endl;
chard;
cout<< (void*)&d<<endl;
cout<< (void*)&e<<endl;
cout<< (void*)&f<<endl;
return0;
}
指针指向存放变量的值的地址。因此我们可以通过指针来修改变量的值。
#include <iostream>
usingnamespacestd ;
chara,b;
intmain()
{
inta=10;
int*p=&a;
cout<<*p<<endl;
*p=12;
cout<<*p<<endl;
cout<<a<<endl;
return0;
}
数组名是一种特殊的指针。指针可以做运算:
#include <iostream>
usingnamespacestd ;
chara,b;
intmain()
{
charc ;
chara[5] = {1,2,3,4,5};
cout<< (void*)&c<<endl;
cout<< (void*)a<<endl;
for (inti=0;i<5 ; i++) cout<< (void*)&a[i] <<endl;
return0;
}
#include <iostream>
usingnamespacestd ;
chara,b;
intmain()
{
int a[5] = {1,2,3,4,5};
int*p=a;
cout<<p<<endl;
cout<<p+1<<endl;
cout<<*(p+2) <<endl;
cout<<*(p+1) <<endl;
return0;
}
链表
#include <iostream>
#include <string.h>
usingnamespacestd ;
constintN=10000;
structNode
{
intdate;
Node*next;
} *head;
intmain()
{
for (inti=1;i<=5; i++)
{
Node*p=newNode();
p->date=i;
p->next=head;
head=p;
}
for (Node*p=head; p ; p=p->next) cout<<p->date<<' ';
cout<<endl;
return0;
};