int n; n=atoi("54"); printf("%d%\n",n);You can do:
printf("%d\n",atoi("54"));
class Trace { public: Trace(char *); ~Trace(); private: char * fn; } // Trace Trace::Trace(char * mesg) { cerr << "now entering: " << mesg << endl; fn = strdup(mesg); } // trace constructor Trace :: ~Trace() { cerr << "now leaving: " << fn << endl; free(fn); } // trace destructorNow in any function you want to trace:
int foo(int a,int b) { Trace tracer("foo"); other stuff } // fooThe constructor is called at the beginning of the function and the destructor at the end. This can be expanded to use a file member to record tracing, read an environment variable to see if tracing is on, keep a list of functions to be traced and only trace those, etc.
class instrument { public: void play(note) { cout << "instrument::play" << endl;} } // instrument class wind : public instrument { public: void play(note) { cout << "wind::play" << endl;} }; // wind void tune(instrument& i) { i.play(note); } main() { wind flute; tune(flute); } // mainThis prints instrument::play. This is because the variable in tune() is of type instrument so it uses the function from there, even though we passed in a flute. Change the definition in instrument to:
virtual void play(note) { cout << "instrument::play" << endl;}Now running the program prints wind::play. The function to be called is determined at run time (late binding). This technique allows us to write functions like tune() that will work for any derived class from a base class. It is easier to extend the classes because tune() still works for other classes derived from instrument. Like drum or guitar. It will also work for lower levels. If class brass is derived from wind, tune() still works. If there is no overridden functions, the one next up the hierarchy is used.
class foo { public: friend int buddy(); } // foo int buddy() { } // buddyA good use of friends are functions that use multiple classes and can be more efficient if they have direct access to the inner parts of a class. Member functions of one class can be listed as friend functions in another. Use the scope resolution operator to make this clear.
class foo { public: friend int other_class::buddy(); } // foo class otherclass { int buddy(){} // buddyIf all the code in a class is a friend of another class, you can say
friend class other_class;