Wednesday, January 16, 2013

Smart Pointer (works as garbage collection) in c++

//smart (auto) pointer is a new feature of c++11
#include <memory>           //     <-- 0
#include <string>
using namespace std;

typedef auto_ptr<Car> CarPtr;


class Car{
    public:
        void startEngine()

        {
            //...................
        }
       
        void tuneRadioTo()

        {
            //...................
        }
};

void f()
{
  CarPtr p(new Car());         //    <-- 1
  p->startEngine();             //    <-- 2
  p->tuneRadioTo();            //    <-- 3
}                                    //    <-- 4

int main()
{
   f();
}
 

0: This gets the definition for auto_ptr
1: Create an object
2: Call a member function
3: Call another member function
4: Destroy the Car object

-----------------------------------------------------------------------------------------------

//Program to show drawback of auto pointer
#include <memory>             
#include <iostream>
using namespace std;

class Test{
    public:
        void print(){
            cout << "Test::print" << endl;
        }
};

int main(){
    auto_ptr<Test>  aptr1(new Test);
    aptr1 -> print();
   
    cout << aptr1.get();
   
    auto_ptr<Test>  aptr2(aptr1);
    aptr2 -> print();
   
    cout << aptr1.get();
    cout << aptr2.get();
   
    return 0;

}

OUTPUT:

To overcome this c++11 provide another mechanism called smart (shared) pointer.

Tuesday, January 15, 2013

A note on garbage value, memory allocation & variable auto intialization

// variable are global
#include <iostream>
using namespace std;

    int x,y,z,c,x2,x3;
    //y = x;
    //z = y;       // This gives error
    //c = z;

    int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;

int main()
{
    y = x;
    z = y;
    c = z;
   
    cout << "\n" ;
    cout << " value at x2: " << x2 << endl;
    cout << " value at x3: " << x3 << endl;
    cout << " value at  x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << " value at  y: " << y << endl;
    cout << "Address of y: " << &y << endl;
    cout << " value at  z: " << z << endl;
    cout << "Address of z: " << &z << endl;
    cout << " value at  c: " << c << endl;
    cout << "Address of c: " << &c << endl;
   
    cout << "\n\n";
    cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
    cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
    cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
    cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
    cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
    cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
    cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
    cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
    cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
    cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
    cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
    cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
    cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
    cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
    cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
    cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
    return 0;
}


OUTPUT:

 ----------------------------------------------------------------------------------------------
// variable initialization all in main not global
#include <iostream>
using namespace std;

int main()
{
    int x,y,z,c,x2,x3;
    y = x;
    z = y;
    c = z;
   
    cout << " value at x2: " << x2 << endl;
    cout << " value at x3: " << x3 << endl;
    cout << " value at  x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << " value at  y: " << y << endl;
    cout << "Address of y: " << &y << endl;
    cout << " value at  z: " << z << endl;
    cout << "Address of z: " << &z << endl;
    cout << " value at  c: " << c << endl;
    cout << "Address of c: " << &c << endl;
   
    int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;
    cout << "\n\n\n " << endl;
   
    cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
    cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
    cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
    cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
    cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
    cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
    cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
    cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
    cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
    cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
    cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
    cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
    cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
    cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
    cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
    cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
   
    return 0;
}


OUTPUT:

 ----------------------------------------------------------------------------------------------
 // variables in function
#include <iostream>
using namespace std;

void test()
{
    int x,y,z,c,x2,x3;
    y = x;
    z = y;
    c = z;
   
    cout << " value at x2: " << x2 << endl;
    cout << " value at x3: " << x3 << endl;
    cout << " value at  x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << " value at  y: " << y << endl;
    cout << "Address of y: " << &y << endl;
    cout << " value at  z: " << z << endl;
    cout << "Address of z: " << &z << endl;
    cout << " value at  c: " << c << endl;
    cout << "Address of c: " << &c << endl;
   
    int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;
    cout << "\n\n\n " << endl;
   
    cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
    cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
    cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
    cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
    cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
    cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
    cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
    cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
    cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
    cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
    cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
    cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
    cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
    cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
    cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
    cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
}

int main()
{
    test();
    return 0;
}


OUTPUT:

-----------------------------------------------------------------------------------------------
// variable in class ; using function ; object on STACK
#include <iostream>
using namespace std;

class Demo{
    public:
        int x,y,z,c,x2,x3;
        int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;  
   
    void test()
    {
        y = x;
        z = y;
        c = z;
       
        cout << "\n" ;
        cout << " value at x2: " << x2 << endl;
        cout << " value at x3: " << x3 << endl;
        cout << " value at  x: " << x << endl;
        cout << "Address of x: " << &x << endl;
        cout << " value at  y: " << y << endl;
        cout << "Address of y: " << &y << endl;
        cout << " value at  z: " << z << endl;
        cout << "Address of z: " << &z << endl;
        cout << " value at  c: " << c << endl;
        cout << "Address of c: " << &c << endl;
   
        cout << "\n\n";
        cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
        cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
        cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
        cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
        cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
        cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
        cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
        cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
        cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
        cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
        cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
        cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
        cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
        cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
        cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
        cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
    }
};

int main()
{
    Demo d;
    d.test();
    return 0;
}


OUTPUT:

-----------------------------------------------------------------------------------------------
// variable in class ; using function ; object on HEAP
#include <iostream>
using namespace std;

class Demo{
    public:
        int x,y,z,c,x2,x3;
        int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;   
   
    void test()
    {
        y = x;
        z = y;
        c = z;
       
        cout << "\n" ;
        cout << " value at x2: " << x2 << endl;
        cout << " value at x3: " << x3 << endl;
        cout << " value at  x: " << x << endl;
        cout << "Address of x: " << &x << endl;
        cout << " value at  y: " << y << endl;
        cout << "Address of y: " << &y << endl;
        cout << " value at  z: " << z << endl;
        cout << "Address of z: " << &z << endl;
        cout << " value at  c: " << c << endl;
        cout << "Address of c: " << &c << endl;
   
        cout << "\n\n";
        cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
        cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
        cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
        cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
        cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
        cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
        cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
        cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
        cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
        cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
        cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
        cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
        cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
        cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
        cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
        cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
    }
};

int main()
{
    Demo *d = new Demo();
   
    d->test();
   
    delete d;
    d = 0;
    return 0;
}


OUTPUT:

Sunday, January 13, 2013

Virtual Inheritance

Virtual inheritance:
                        Inheritance can be private (where even the public fields of the base class are treated as private), public (where the base class determines visibility), or virtual. With virtual inheritance there is only one copy of each object even if (because of multiple inheritance) the object appears more than once in the hierarchy. In the following example, derived1a and derived1b each contain a base object, butderived2 contains just one field.

#include <iostream>
using namespace std;

class base{
    public:
        int i;
};

class derived1a: virtual public base {
    public:
        int j;
};

class derived1b: virtual public base {
    public:
        int k;
};

class derived2: public derived1a, public derived1b{
    public:
        int product(){
            return i*j*k;
        }
};

int main()
{
    derived2 obj;
    obj.i = 10;
    obj.j = 3;
    obj.k = 5;
   
    cout << "\n product is " << obj.product() << '\n';
    return 0;
}

OUTPUT: