Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
cpp template example 2 and tutorials       Share
2008-09-28 |  RatheeshTR  | Viewed: 114  |    0

#include <iostream>
#include <memory>
#include <string>

using namespace std;


class myException

{

public:

char * errorMsg() { return "Oops."; }
};


template<class T>

class RCSmartPointer

{

public:

RCSmartPointer(T* ptrToRC = 0);

RCSmartPointer(const RCSmartPointer& rhs);

~RCSmartPointer();


RCSmartPointer& operator=(const RCSmartPointer& rhs);


T* operator->() const;

T& operator*() const;


private:

T * pRC; 

void initialize();
};


template<class T>

void RCSmartPointer<T>::initialize()
{

if ( pRC == 0 ) 
return;


if ( pRC->isShareable() == false )
{

pRC = new T(*pRC); 

}

pRC->addRef();

}


template<class T>

RCSmartPointer<T>::RCSmartPointer(T* ptrToRC):

pRC(ptrToRC)

{

initialize();

}


template<class T>

RCSmartPointer<T>::RCSmartPointer(const RCSmartPointer& rhs):

pRC(rhs.pRC)

{

initialize();

}


template<class T>

RCSmartPointer<T>::~RCSmartPointer()

{

if ( pRC )
pRC->removeRef();

}


template<class T>

RCSmartPointer<T>& RCSmartPointer<T>::operator=(const RCSmartPointer& rhs)

{

if ( pRC == rhs.pRC )
return *this;


if ( pRC )
pRC->removeRef();


pRC = rhs.pRC;


return *this;

}


template<class T>

T* RCSmartPointer<T>::operator->() const

{

return pRC;

}


template<class T>

T& RCSmartPointer<T>::operator*() const

{

return *pRC;

}



class ReferenceCounted

{

public:

void addRef() { ++referenceCount; }
void removeRef() {
if ( --referenceCount == 0 ) 
delete this; 

}


void markNotShareable() { shareable = false; }
bool isShareable() { return isShareable; }

bool isShared() { return referenceCount > 1; }


protected:

ReferenceCounted():referenceCount(0), shareable(true) {}

ReferenceCounted(const ReferenceCounted& rhs):referenceCount(0), shareable(true) {}

ReferenceCounted& operator=(const ReferenceCounted& rhs) { return *this; }

virtual ~ReferenceCounted(){}


private:

int referenceCount;
bool shareable;

};



class Point

{

public:

Po
int (int x, int y):myX(x), myY(y) { cout << "Point constructor called"<< endl;}
Po
int (const Point & rhs):
myX(rhs.myX),

myY(rhs.myY){ cout << "Po
int copy constructor called" << endl;}
~Point(){ cout << "Po
int destructor called" << endl;}
int GetX() const { return myX; }
void SetX(int x) { myX = x; }
int GetY() const { return myY; }
void SetY(int y) { myY = y; }
private:

int myX;
int myY; 
};


class Rectangle

{

public:

Rectangle( Po
int upperLeft, Point lowerRight ):
rcRect(new countedRect(upperLeft, lowerRight))

{}


Rectangle( Po
int * pUpperLeft, Point * pLowerRight ):
rcRect(new countedRect(*pUpperLeft, *pLowerRight))

{}



Rectangle( 
int upperLeftX, int upperLeftY, int lowerRightX, int lowerRightY ):
rcRect(new countedRect(new Point(upperLeftX,upperLeftY), new Point(lowerRightX,lowerRightY)))

{}



~Rectangle(){ cout << "In Rectangle's destructor" << endl;  }



int GetWidth() { return rcRect->myLowerRight->GetX() - rcRect->myUpperLeft->GetX(); }
int GetHeight() { return rcRect->myLowerRight->GetY() - rcRect->myUpperLeft->GetY(); }

void DangerousMethod() { throw myException(); }

 private:


struct countedRect : public ReferenceCounted

{

char * cString;
Po
int * myUpperLeft;
Po
int * myLowerRight;

countedRect( const Po
int * ul, const Point * lr) { initialize(ul,lr); }
countedRect( Po
int ul, Point lr ) { initialize(&ul, &lr); }
countedRect( const countedRect & rhs) { initialize(rhs.myUpperLeft, rhs.myLowerRight); }

~countedRect() 


delete myUpperLeft; 

delete myLowerRight;

}

void initialize(const Point * ul, const Point * lr)
{

myUpperLeft = new Point(*ul);

myLowerRight = new Point(*lr);

}



friend class RCSmartPointer<countedRect>;

};


RCSmartPointer<countedRect> rcRect;




};




int main()
{

try

{

cout << "Begin round 1..." << endl;

Po
int pUL(0,0);
Po
int pLR(20,30);
Rectangle myRectangle(pUL, pLR);

int w = myRectangle.GetWidth();
int h = myRectangle.GetHeight();
cout << "the Rectangle is " << w << " by " << h << endl;

myRectangle.DangerousMethod();

cout << "You never get here.\n" << endl;


}

catch ( myException & e )

{


cout << "caught exception: " << e.errorMsg() << "\n\n" << endl;

}

return 0;

}


Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
Cpp - Access a Class Member Function Without Creating a Class Object
Cpp - Writing Macro Definition
Cpp - using namespace std
Cpp - Using enum
Cpp - Pointers in C++
Cpp - How to use DLL file in the C++ Program
Cpp - creating and Linking DLL Files to C++ program
Cpp - 1) vector method and iterator (simple Example)
Cpp - 2) Sort and Binary search
Cpp - 3) Vector and Copy vector
Cpp - 4) for_each ( Vector )
Cpp - 5) List and Iterator
Cpp - 6) list and Vector example.2
Cpp - 7) example"fill, copy, list" statement
Cpp - 8) list and set

Related References
cpp - operator overloading
cpp - default arguments
cpp - c++ interview tips
cpp - virtual destructors
cpp - c++ destructors
cpp - csc interview questions
object oriented programming paradigm
elements of object oriented programming
basic concepts of oops and structure of c++ program
exception (runtime error) handling - cpp
cpp function pointers
cpp - access specifiers between base and derived classes
cpp - inautix interview questions
comparing popular programming languages

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us