CMPT 212
Fall 1997
|
To allow the compiler to check, in one pass, that each function is being called with the correct number of parameters, with the correct parameter types, and with the correct return value type being expected. |
|
Neither. A car has an engine (object composition), but a Toyota is a special case of a car (object membership). |
Object membership. Lion, tiger, and leopard are all special cases of cat. |
Object composition. Age and first name are both parts of a person. |
Object membership. Elephant is a special case of mammal, and mammal is a special case of animal. |
#include <iostream.h> class myClass { public: myClass(); myClass(const int i); myClass(const myClass& c); ~myClass(); myClass& operator=(const myClass& c); private: int data; }; myClass::myClass() { data = 3; cout << "default constructor: data = " << data << endl; } myClass::myClass(const int i) { data = i; cout << "int constructor: data = " << data << endl; } myClass::myClass(const myClass& c) { data = c.data; cout << "myClass constructor: data = " << data << endl; } myClass::~myClass() { cout << "destructor: data = " << data << endl; } myClass& myClass::operator=(const myClass& c) { data = c.data; cout << "assignment operator: data = " << data << endl; return *this; } int main() { myClass a; myClass b(7); myClass c = b; myClass * d = new myClass(4); delete d; c = a; return 0; }
default constructor: data = 3 int constructor: data = 7 myClass constructor: data = 7 int constructor: data = 4 destructor: data = 4 assignment operator: data = 3 destructor: data = 3 destructor: data = 7 destructor: data = 3 |
#include <stdlib.h> #include <iostream.h> static int a = 6; const float b = 5.44; int calculate(char c = 'W') { bool d = true; static int e = 0; if (c != 'W') { e++; d = (e < 5); } return e; } int main() { char * f = NULL; f = new char('H'); a = calculate(*f); delete f; return 0; }
Scope | Lifetime | |
---|---|---|
a | module | static |
b | module (although global was accepted) | static |
c | local | automatic |
d | local | automatic |
e | local | static |
f | local | automatic (although dynamic was accepted) |
Parts of a program can be written in different languages. | true |
It makes it easier to check that functions are called with the correct parameters. | false |
It makes it easier for different programmers to work on different parts of the program. | true |
Recursive functions make good inline functions. | false |
Inline functions always make a program smaller. | false |
The compiler must always see the function definition of an inline function before it reaches the place where the function is called. | true |
Constant data members must always be initialized in the body of the constructor. | false |
Constant member functions must not change the instance for which they are called. | true |
A constant instance is just an instance whose data members are all declared with the const keyword. | false |
An address is a memory location, but a pointer is just a value. | false |
If we have these definitions:
int i = 4; const int * p = &i;then p must always point to i. |
false |
Any memory which is allocated using new should always be released using delete when it is no longer needed. | true |
#include <iostream.h> char myFunc(int i) { cout << "one" << endl; return 'B'; } int myFunc(float f) { cout << "two" << endl; return 7; } int myFunc(int i, float f) { cout << "three" << endl; return 8; } float myFunc(char c, int i = 3) { cout << "four" << endl; return 3.14; } int main() { int x = 1; float y = 2.0; char z = 'A'; cout << myFunc(myFunc(y)) << endl; cout << myFunc(myFunc(x)) << endl; cout << myFunc(myFunc(x, y)) << endl; cout << myFunc(myFunc(z, x)) << endl; cout << myFunc(myFunc(z)) << endl; return 0; }
two one B one four 3.14 three one B four two 7 four two 7 |
This page is maintained by simpson@cs.sfu.ca. | Last updated on 27 Oct 1997. |