#include <iostream.h>
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area (void) {return (width * height);}
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
int main () {
CRectangle a, *b, *c;
CRectangle * d = new CRectangle[2];
b= new CRectangle;
c= &a;
a.set_values (1,2);
b->set_values (3,4);
d->set_values (5,6);
d[1].set_values (7,8);
cout << "a area: " << a.area() << endl;
cout << "*b area: " << b->area() << endl;
cout << "*c area: " << c->area() << endl;
cout << "d[0] area: " << d[0].area() << endl;
cout << "d[1] area: " << d[1].area() << endl;
return 0;
}
result:
a area: 2
*b area: 12
*c area: 2
d[0] area: 30
d[1] area: 56
- *x 读作: pointed by x (由x指向的)
- &x 读作: address of x(x的地址)
- x.y 读作: member y of object x (对象x的成员y)
- (*x).y 读作: member y of object pointed by x(由x指向的对象的成员y)
- x->y 读作: member y of object pointed by x (同上一个等价)
- x[0] 读作: first object pointed by x(由x指向的第一个对象)
- x[1] 读作: second object pointed by x(由x指向的第二个对象)
- x[n] 读作: (n+1)th object pointed by x(由x指向的第n+1个对象)