![]() |
CMPT 212
Spring 1998
|
|
|
|
class Person { public: Person(); ~Person(); friend ostream & operator<<(ostream &, Person &); private: char name[20]; int age; int weight; };Write a definition of the output operator << for the class Person that will produce output like that shown in the sample output below.
Name: Henry Ford Age: 32 Weight: 185
ostream & operator<<(ostream & os, Person & p) { os << "Name: " << p.name << endl << "Age: " << p.age << endl << "Weight: " << p.weight << endl; return os; } |
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. |
#include <iostream.h> class elephant { public: elephant(); elephant(const int i); elephant(const elephant & c); ~elephant(); elephant & operator=(const elephant & c); private: int size; }; elephant::elephant() { size = 3; cout << "default constructor: size = " << size << endl; } elephant::elephant(const int i) { size = i; cout << "int constructor: size = " << size << endl; } elephant::elephant(const elephant & c) { size = c.size; cout << "elephant constructor: size = " << size << endl; } elephant::~elephant() { cout << "destructor: size = " << size << endl; } elephant & elephant::operator=(const elephant & c) { size = c.size; cout << "assignment operator: size = " << size << endl; return *this; } int main() { elephant a; elephant b(7); elephant c = b; elephant * d = new elephant(4); delete d; c = a; return 0; }
default constructor: size = 3 int constructor: size = 7 elephant constructor: size = 7 int constructor: size = 4 destructor: size = 4 assignment operator: size = 3 destructor: size = 3 destructor: size = 7 destructor: size = 3 |
#include <iostream.h> class myObject { public: myObject(int i, int j) { x = i; y = j; } void SetX(int i) { x = i; } void SetY(int i) { y = i; } static void ShowX() { cout << "x = " << x << endl; } void ShowY() { cout << "y = " << y << endl; } static void increment() { x++; } private: static int x; int y; }; int myObject::x = 5; int main() { static myObject a(3, 4); myObject b(7, 8); a.ShowX(); a.ShowY(); b.ShowX(); b.ShowY(); a.SetX(1); b.increment(); myObject::increment(); a.ShowX(); a.ShowY(); b.ShowX(); b.ShowY(); return 0; }
x = 7 y = 4 x = 7 y = 8 x = 3 y = 4 x = 3 y = 8 |
the first one |
#include <stdlib.h> #include <iostream.h> int a = 4; static float b = 2.73; int evaluate(char c = 'X') { static bool d = true; int e = 0; if (c != 'X') { e++; d = (e < 5); } return e; } int main() { char * f = NULL; f = new char('H'); a = evaluate(*f); delete f; return 0; }
Scope | Lifetime | |
---|---|---|
a | global | static |
b | module | static |
c | local | automatic |
d | local | static |
e | local | automatic |
f | local | dynamic |
#include <iostream.h> int main() { int a = 3; int & b = a; int * c = &a; int d = 10; cout << b << endl; *c = 5; cout << b << endl; c = &d; a++; b++; (*c)++; d++; cout << a << endl << b << endl << (*c) << endl << d; return 0; }
3 5 7 7 12 12 |
#include <iostream.h> char test(int i) { cout << "one" << endl; return 'B'; } int test(int i, float f) { cout << "two" << endl; return 8; } float test(char c, int i = 3) { cout << "three" << endl; return 3.14; } int test(float f) { cout << "four" << endl; return 7; } int main() { int x = 1; float y = 2.0; char z = 'A'; cout << test(test(y)) << endl; cout << test(test(x)) << endl; cout << test(test(x, y)) << endl; cout << test(test(z, x)) << endl; cout << test(test(z)) << endl; return 0; }
four one B one three 3.14 two one B three four 7 three four 7 |
This page is maintained by simpson@cs.sfu.ca. | Last updated on 4 Mar 1998. |