CMPT 212
Fall 1997
|
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();
#include <stdlib.h> #include <iostream.> class rectangle { public: rectangle(); // default constructor. ~rectangle(); // destructor. ... }; rectangle::rectangle() { cout << "a new rectangle" << endl; } rectangle::~rectangle() { cout << "one less rectangle" << endl; } int main() { rectangle a; rectangle * b = NULL; cout << "one" << endl; b = new rectangle; cout << "two" << endl; delete b; cout << "three" << endl; return 0; }
a new rectangle one a new rectangle two one less rectangle three one less rectangle
Return to lecture notes index |
|
This page is maintained by simpson@cs.sfu.ca. | Last updated on 6 Oct 1997. |