|
CMPT 212
Spring 1998
|
class rectangle {
public:
...
int getWidth() { return width; }
...
};
is the same as
class rectangle {
public:
...
inline int getWidth();
...
};
inline int rectangle::getWidth() {
return width;
}
provided that the definition of getWidth is
in the same file as the definition of rectangle.
int i = 37;
char * c = NULL;
class rectangle {
public:
rectangle();
void setSize(int x, int y);
int getWidth();
int getLength();
private:
int width;
int length;
};
rectangle::rectangle() {
width = 4;
length = 10;
}
rectangle a;
rectangle * b = new rectangle;
cout << a.getWidth(); // outputs 4.
cout << b->getLength(); // outputs 10.
class rectangle {
public:
rectangle();
rectangle(int x, int y);
...
};
rectangle a(6, 7);
rectangle * b = new rectangle(3, 2);
rectangle c; // defaults to 4 by 10.
class rectangle {
public:
rectangle(int x, int y); // no default constructor.
...
};
rectangle a; // error.
rectangle * b = new rectangle; // error.
rectangle a(12, 9);
rectangle b(a); // b is also 12 by 9.
void myFunc(rectangle r);
...
rectangle a;
myFunc(a);
rectangle myFunc();
...
rectangle a;
a = myFunc();
Return to lecture notes index |
|
| This page is maintained by simpson@cs.sfu.ca. | Last updated on 4 Feb 1998. |