Académique Documents
Professionnel Documents
Culture Documents
1 2
inline void f (unsigned) f cout << "f (unsigned) called\n"; g unsigned height, width;
int main (void) f screen size (height, width);
f (1); // calls f (int)
f (1U); // calls f (unsigned)
g However, it is harder to tell if arguments
are modied with this approach!
5 6
new can both help avoid common errors The << and >> stream I/O operators are
with malloc and also ensure that construc- (1) type-safe and (2) extensible to user-
tors and destructors are called dened types
9 10
void init symbol table (struct Symbol Table *); int len;
int lookup (struct Symbol Table *); int i;
static struct Symbol Table sym tab; if (src == *dst) return;
int main (void) f
char s[100]; if (*dst != 0) free (*dst);
init symbol table (&sym tab); len = strlen (src);
/* */ *dst = (char *) malloc (len + 1);
g
:::
13 14
C++ C++
#include <iostream.h> class Rectange : public Shape f
#include <math.h> public:
class Shape f Rectangle (
oat x1,
oat y1,
oat x2,
oat y2);
public: virtual
oat area (void) const;
Shape () fg private:
virtual
oat area (void) const = 0;
oat x1, y1, x2, y2;
g; g;
class Triangle : public Shape f
oat Rectangle::area (void) const f
public: return fabs ((x1 , x2) * (y1 , y2));
Triangle (
oat x1,
oat x2,
oat x3, g
oat y1,
oat y2,
oat y3); class Circle : public Shape f
virtual
oat area (void) const; public:
private: Circle (
oat x,
oat y,
oat r);
oat x1, y1, x2, y2, x3, y3; virtual
oat area (void) const;
g; private:
oat Triangle::area (void) const f
oat x, y, r;
return fabs ((x1 * y2 , x2 * y1) + g;
(x2 * y3 , x3 * y2) +
oat Circle::area (void) const f
(x3 * y1 , x1 * y3)) / 2; return M PI * r * r;
g g
15 16
Tips for C Programmers (cont'd)
Tips for C Programmers (cont'd)
static members (cont'd)
Use static member variables and functions { C++
instead of global variables and functions, #include <iostream.h>
and place enum types in class declarations class My Lib f
public:
enum Color Type f RED, GREEN, BLUE g;
This approach avoid polluting the global static Color Type color;
name space with identiers, making name static unsigned char even parity (char c);
con
icts less likely for libraries g;
My Lib::Color Type My Lib::color = My Lib::RED;
{C int main (void) f
My Lib::color = My Lib::GREEN;
#include <stdio.h> cout << hex (int (My Lib::even parity ('Z')))
enum Color Type f RED, GREEN, BLUE g; << "\n";
enum Color Type color = RED; g
unsigned char even parity (void);
int main (void) f
color = GREEN; Note that the new C++ \namespaces"
printf ("%.2x\n", even parity ('Z')); feature will help solve this problem even
g
more elegantly
17 18
19 20
Initialization vs Assignment (cont'd)
C++ Traps and Pitfalls (cont'd) { Constructing \neighbor" object is costly
Initialization vs Assignment 1. Name::Name gets called with parameter \Joe"
{ Consider the following code
class String f 2. Name::Name has no base initialization list, so
public: member object \`neighbor.s"' is constructed
String (void); // Make a zero-len String by default String::String
String (const char *s); // char * --> String This will probably allocate a 1 byte area
String (const String &s); // copy constructor from freestore for the '\0'
String &operator= (const String &s); // assignment
private:
int len; 3. A temporary \Joe" String is created from
char *data; parameter t using the CONST CHAR * con-
g; structor
class Name f
public: This is another freestore allocation and a
Name (const char *t) f s = t; g strcpy
private:
String s;
g; 4. String::operator= (const string &) is
int main (void) f called with the temporary String
// How expensive is this?????????
Name neighbor = "Joe"; 5. This will delete the old string in s, use an-
g other new to get space for the new string,
and do another strcpy
21 22
23
C++ Traps and Pitfalls (cont'd)
Default Parameters and Virtual Functions
C++ Traps and Pitfalls (cont'd) extern "C" int printf (const char *, ::: );
Although a function with no arguments class Base f
must be called with empty parens a con- public:
structor with no arguments must be called virtual void f (char *name = "Base") f
with no parens! printf ("base = %s\n", name);
g
class Foo f g;
public: class Derived : public Base f
Foo (void); public:
int bar (void); virtual void f (char *name = "Derived") f
g; printf ("derived = %s\n", name);
int main (void) f g
Foo f; g;
Foo (); // declares a function returning Foo!
f.bar (); // call method
f.bar; // a no-op int main (void) f
.bar (); // error! Derived *dp = new Derived;
g dp->f (); /* prints "derived = Derived" */
Base *bp = dp;
bp->f (); /* prints "derived = Base" */
return 0;
g
24 25
26 27
Eciency and Performance
(cont'd)
Miscellaneous Tips
{ Use good memory (heap) management strate-
gies
{ Develop good utility classes (for strings, in par-
ticular)
{ Good object and protocol design (particularly,
really isolating large-grained objects)
{ Give attention to paging and other ways your
application uses system resources