分类: C/C++
2011-08-03 21:35:36
/*
* 对象指针的使用
* Lzy 2011-8-3
*/
#include
using namespace std;
class A
{
private:
int x, y;
public:
A(int=0, int=0);
void Display(void){cout<<" ("<
};
A::A(int i, int j):x(i),y(j){}
int main(void)
{
A a1, a2(5,5),*p;
p = &a1;
p->Display();
p = &a2;
p->Display();
return 0;
}
/*
* 对象指针作为函数的参数
* Lzy 2011-8-3
*/
#include
using namespace std;
class Num
{
private:
int x, y;
public:
Num(int i=5, int j=5):x(i),y(j){}
int sum(){return x+y;}
};
int sum(Num *p)
{
return p->sum();
}
int main(void)
{
Num n;
cout<
return 0;
}
/*
* 对象引用作为函数的参数
* Lzy 2011-8-3
*/
#include
using namespace std;
class Num
{
private:
int x, y;
public:
Num(int i=5, int j=5):x(i),y(j){}
int sum(){return x+y;}
};
int sum(Num &p)
{
return p.sum();
}
int main(void)
{
Num n;
cout<
return 0;
}
/*
* 对象数组
* Lzy 2011-8-3
*/
#include
using namespace std;
class Num
{
private:
int num;
public:
Num(int i=0):num(i){}
void Display(){cout<
};
int main(void)
{
Num aray[5]={1,2,3,4,5};
for(int i=0; i<5; i++)
aray[i].Display();
return 0;
}
/*
* 对象数组
* Lzy 2011-8-3
*/
#include
using namespace std;
class Num
{
private:
int num1, num2;
public:
Num(int i=0, int j=0):num1(i),num2(j){}
void Display(){cout<
};
int main(void)
{
Num aray[2]={Num(1,1),Num(3,3)};
for(int i=0; i<2; i++)
aray[i].Display();
return 0;
}