Saturday, April 16, 2011

C++: Operator Overloading

#include<iostream>

using namespace std;

class A
{
        int a;
        public:
                A(int x=0):a(x){
                        cout<<"in Cons"<<endl;
                }
                friend A operator+(const A& _a1, const A& _a2);
                friend ostream& operator<<(ostream &out, const A& _a);
                int getData()
                {
                        return a;

                }
};

A operator+(const A& _a1, const A& _a2)
{
        cout<<"Inside operator+"<<endl;

        return  A(_a1.a + _a2.a);

}
ostream& operator<<(ostream & o, const A& _a)
{
        o << _a.a;
        return o;

}

int main()
{
        A x1(2), x2(3);
        A x3 = x1  + x2;
        cout << x3 <<",  "<<x1<<endl;

}

Wednesday, April 13, 2011

C++: Deep Copy

#include<iostream>
using namespace std;

class MyStr
{
        char *str;
        public:

        // Constructor
        MyStr(char *_s="")
        {
                if(_s)
                {
                        str = new char[strlen(_s)];
                        strcpy(str,_s);
                }

        }
        //Copy COnstructor
        MyStr(const MyStr & _rhs)
        {
                str = new char[strlen(_rhs.str)];
                strcpy(str,_rhs.str);
        }
        //Assignment Operator
        MyStr& operator=(const MyStr& _rhs)
        {
                //Check for self-assignment
                if(this == &_rhs)
                        return *this;
                // Deallocate memory if it is holding.
                delete [] str;
                //Allocate memory and copy the content
                if(_rhs.str)
                {
                        str = new char[strlen(_rhs.str)];
                        strcpy(str,_rhs.str);
                }
                else
                        str = 0;

                return *this;
        }
        //Destructor
        ~MyStr()
        {
                delete[] str;
                str=0;
        }
        char * getStr()
        {
                return str;

        }

};


int main()
{
        using namespace std;
        MyStr s("Dhananjay Kuamr"),s3;
        {
        s= s;
        }
        cout<<"THe out of s is after =:- "<<s.getStr() <<endl;
        s3 = s;
        cout<<"THe out of s3 is after =:- "<<s3.getStr() <<endl;

}

Monday, March 28, 2011

Reverse String

#include <iostream>
using namespace std;

//This function accepts a string and returns the reversed string to the caller function.
char* revStr(char *str)
{
        //checking for null pointer
        if(str == 0)
                return 0;

        //get the length of the string we received.
        int len = strlen (str);

         /**    Allocate memory in heap of the same data type.
                It'll be wrong to declare memory in stack e.g. char str1[len]
                because we are returning address to the caller function.
                The locally allocated memory will be destroyed once we return to the caller.
                so using new operator or malloc function
         **/
        char *str1 = new char[len];

        //We can't change the original string so copy the source string
        //to the newly allocated space for local manipulation
        strcpy(str1, str);

        // Loop: half of the string length
        for (int i=0, j = len-1 ; i<len/2; i++, j--)
        {
                // the standard swapping logic
                char t;
                t = str1[i];
                str1[i] = str1[j];
                str1[j] = t;
        }
        //returning the address of the memory in heap, which persistes even after retrun.
        //However str1 pointer will no longer be accessible after return.
        return str1;

}

//One of the calller function
char* test ()
{
        char *p = "abc";
        return revStr(p);

}

// main function
int main()
{
        char str[14];
        cout<<"Enter a string to reverse : ";
        cin>>str;

        // Testing multiple assignment operator
        char *str1, *str2;
        str1 = revStr(str);
        cout<<"The reversed string :"<<str1<<endl;
        cout<<"The original string :"<< str<<endl;
        str2 = test();
        cout<<"The function call...."<<str2<<endl;

        //Explicitly deallocating memory allocated in heap in revStr()
        delete str1;
        delete str2;

}