Monday, June 04, 2007

sample applications for Pointer to derived types and pointer to member

#include
class CConstructorThrow
{
public:
CConstructorThrow();
~CConstructorThrow();
};

CConstructorThrow::CConstructorThrow()
{
try
{
throw 100;
}
catch(...)
{
printf("\n Exception in CConstructorThrow::CConstructorThrow()...");
}

}
CConstructorThrow::~CConstructorThrow()
{

}

int main(int argc, char* argv[])
{
CConstructorThrow* pThrow = new CConstructorThrow();

if(pThrow == NULL)
{
printf("\n Constructor throw returned the null pointer ");

}
else
{
printf("constructor throw returned the valid pointer");
}
return 0;
}


I got the output as :

Exception in CConstructorThrow::CConstructorThrow()...
constructor throw returned the valid pointer








Example 2 :

Normally we

#include
#include
using namespace std;

void nothrowTesting()
{
int *p;

p = new(nothrow) int[20];

if( p == NULL)
{
printf("\n Memory allocation is failed...");
return;
}
else
{
printf("\n Memory Allocation is succeeded...");
}



if(p)
{
delete [] p;
p = NULL;
}
}


int main(int argc, char* argv[])
{
nothrowTesting();
return 0;
}


Example 3 :
-------------------------




#include
#include
using namespace std;
class nothrowClass
{
public:
nothrowClass() ;
~nothrowClass();


};

nothrowClass::nothrowClass()
{
printf("\n constructor..");
throw 100;
}

nothrowClass::~nothrowClass()
{
printf("\n destructor..");

}

void nothrowTestingClient()
{
nothrowClass* pClass;

try
{

pClass = new nothrowClass();

if(pClass == NULL)
{
printf("\n Invalid Pointer");

}
else
{

printf("\n valid Pointer");

if(pClass)
{
delete pClass;
pClass = NULL;
}


}
}
catch(...)
{
printf("\n Error in Allocation..");
}
}

int main(int argc, char* argv[])
{
nothrowTestingClient();
return 0;
}


output :
-----------
constructor
Error in Allocation (exception)
if the exception occurs the normal flow of program will be affected... So How can we handle this situation ?...

Example 4:
-------------------

#include

class Base
{
public:
Base()
{

}
~Base()
{

}

void DisplayBase()
{
printf("\n Base Display");
}


};

class Derived: public Base
{
public:
Derived()
{
}

~Derived()
{
}
void DisplayDerived()
{
printf("\n Derived Display");
}


};


int main(int argc, char* argv[])
{
Derived d;
Base* bp;

bp = &d; // we r typecasting the derived class to base class
bp->DisplayBase();

//bp->DisplayDerived() fn is an error we have to call like this...

((Derived*)bp)->DisplayDerived(); //This is the use of typecasting
return 0;
}


Example 5:
---------------------

#include

// .* and ->* are pointer to member operators

class PointerToMember
{
public:

int (PointerToMember::*fn)();

int (PointerToMember::*fn2)();

PointerToMember(int i)
{
val = i;
fn = double_val;
fn2 = double_val;

}


int val;
int double_val() { return val+val; }


int callPointer()
{
return (this->*fn)();
}





};

int main(int argc, char* argv[])
{


PointerToMember p(1); //Initialize the constructor of the class

printf("\n fn return value is : %d",p.callPointer());


return 0;
}

No comments: