|
Various code samples for various reasons.
newdel.cpp
/* Copyright (c) 2007, Peter Varga pvarga@pvrg.net
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
o Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
o Neither the name of pvrg.net nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <string>
#include <exception>
/* shows use of const and virtualness for a java programmer */
/* it was originally an interview question, turned into
* a good example to demonstrate virtualness, const, etc
*/
using namespace std;
class Name {
private:
string name;
public:
Name( char * n ) {
name = n;
}
Name( Name & ) {
cout << "Name::Name( Name & )" << endl;
}
Name & operator=( Name & ) {
cout << "Name::operator=( Name & )" << endl;
}
const string & getData( void ) {
return name;
}
};
class zero {
public:
zero() { }
virtual ~zero() { if ( n ) delete n; }
virtual const Name& typeInfo( void ) throw ( std::exception * ) {
throw( &e );
return *n;
}
virtual void setName( void ) {
n = new Name( "zero\0" );
}
virtual const string & myName( void ) {
return n->getData();
}
virtual const Name & getName( void ) {
return *n;
}
protected:
class Name *n;
std::exception e;
};
class one : public zero {
public:
one(){ };
virtual ~one(){ cout << "one dtor" << endl; }
virtual void setName( void ) {
n = new Name( "one\0" );
}
virtual const Name & getName( void ) {
return *n;
}
private:
char ar[1024*1024];
};
class two {
public:
two() { cout << "two ctor" << endl; }
virtual ~two() {
cout << " dtor " << endl;
for( unsigned u = 0; u < 10 ; ++u ) {
cout << " del " << u << " ";
/* with virtual const Name & one::getname() the below fails
cout << p[u]->getName().getData() << endl; */
/* with virtual const Name & one::getname() the below works */
cout << p[u]->myName() << endl;
delete p[u];
}
}
void make( void ) {
for( unsigned u=0; u< 10; ++u) {
one * o = new one ();
if( o ) {
p[u] = o;
p[u]->setName( );
}
}
}
protected:
zero *p[10];
private:
friend class zero;
one *o;
};
#ifdef LIB
extern "C" {
int libmain( int c, char ** v ) {
#else
int main( int c, char ** v ) {
#endif
two *t = new two();
one *o = new one();
t->make();
cout << "Created classes" << endl;
try {
o->typeInfo();
} catch( std::exception *e ) {
cout << "exception caught " <<endl;
}
cout << "Deleting instances" << endl;
delete t;
delete o;
return 0;
}
#ifdef LIB
}
#endif
|