Skip to main content

Posts

Showing posts with the label class

C++ Smart Pointer

The following is the finalized code for a simple C++ smart pointer as demonstrated by my youtube video:  here Usage: void main(){      ...     Pointer<ObjectType> pointer(new ObjectType);     ... } /*  * Pointer.h  *  *  Created on: Mar 16, 2012  *      Author: ukaku  */ #ifndef POINTER_H_ #define POINTER_H_ #include <iostream> using namespace std ; /**  * Reference represents a counter that will be incremented or decremented as  * the number of references to a pecticular pointer is made;  *  * where data means: a symbol used to point to, or associate to another object  */ class ReferenceCounter { private :     int counter ; public :     ReferenceCounter ( ) {         counter = 0 ;   //initially set to zero     }     void increase ( ) {         counter ++;   //increase it     }     int decrease ( ) {         -- counter ;   //decrease it         if ( counter < 0 ) {             cout << "ReferenceCouner is <

C++ Interface

In this tutorial we will discuss interfaces in C++ and why one would be of benefit.  First off, an interface serves as a means to have a commonality of usage for varies objects.  What I mean by that is that say you have a class representing a basketball and a class that represents a soccer ball, it is understood that both are used for playing but how they are played with is different.  That difference of how they are used for playing is where interfaces come in. An Interface provides a common way of using a class of type basketball and a class of type soccer ball without having will being able to refer to each different object(basketball or soccer ball) with the same interface. Okay okay, this is confusing so let us take a couple of examples. Without an interface, to have an object of basketball and soccer ball perform the same behavior, playing, you could do this: class BasketBall{ public:      void play(){           cout << "playing with basketball" << end