Friday, January 22, 2010

Operator overloading

C++ supports Polymorphism concept with giving different & sepcial behaviour depending upon the context. Generally, this polymorphic behaviour is at compile time or at runtime.
Function overloading & operator overloading are two compile time polymorphism examples.
For this article, we will get an introduction to Operator overloading.

Opearator Overloading -

The existing supported operators are extended for their functionalities ( behaviour ) by operator overloading. This supports the extensibility concept of C++ which has made it to produce great amount of customised libraries.
Operator overloading adds up to the functionalities of the operator with preserving other features like presedence, number of arguments to be operated & basic functionality etc.


Prototype -

return_type operator op (argument list )
{
...
}
op - the operator to be loaded

The keyword "operator" is used for denoting the function as operator overloading function.
The function can not be static. But it can be made friend function.

Limitations -

There are operators which can not be supported for overloading as an exception -
a. Scope resolution operator (::)
b. size of operator (sizeof())
c. conditional operator (?:)
d. pointer to member variable (.*)

Also, when we make the function as friedn function, it can not overload the following operators -
a. =
b. ()
c. []
d. ->


Usability -
Its used for extensibility, reusability with additional features.

Example -

// code of Test2.hpp file

#include
#include

using namespace std;

template
class MyList : public std::list
{

public:

// Default ctor
MyList ( ) : std::list ( ) { };
MyList (size_t n) : std::list (n) { };
MyList (size_t n, const T& val) : std::list (n, val) { };

// overloading operator "[]" as member function
// returns the ith element from the list
const T& operator [] ( size_t i) const
{
typename std::list::const_iterator c_it=this->begin ( );
typename std::list::const_iterator cv_end=this->end ( );

size_t intCnt = 0;

// loop till ith element
while ( intCnt != i && c_it != cv_end )
{
c_it++ ;
intCnt++;
}

// return the ith element value
return *c_it;
}
};


// code of Test2.cpp file


#include "stdafx.h"
#include "Test2.h"

int main(int argc, char* argv[])
{
// instantiate the class MyList for string type values
MyList obj;
//add values
obj.push_back("Avaneesh");
obj.push_back("Anand");
obj.push_back("Amar");

//get the first element
std::cout< //get the second element
std::cout< //get the third element
std::cout<
return 0;
}

Wednesday, May 6, 2009

Pass by Reference in C++

Always try to pass objects by reference in C++ in funcation calls as it offers following advantages -

1. Efficient -
Pass by refernce just uses aliases for the object being passed. It does not make copy of the object.

2. Safe -
As reference variable is used for passing the object, possibility of NULL value is avoided.

3. Multiple value return -
Using the reference variable, one can make changes to the members of the object and return as a reference object. Thus, many changed values can be returned.

Wednesday, January 7, 2009

Embedding SQL into Unix Script

Embedding of SQL query in Unix Script -


The automation usability of the Unix Scripting can be enhanced with embedding the SQL queries into a Unix script file.
It can be achieved as mentioned in the below examples -

1.
isql -S -U -P << EOF

EOF

Use isql command with the aforesaid options.
<< EOF says, do the execution of the embedded sql after this command line till word "EOF" is encountered.

The output would be displayed on command prompt only.


2. isql -S -U -P << EOF >analytics_result.txt

EOF


Same as above with one addition , the output can bbe redirected to a file with indirection ">". Here the output file is "analytics_result.txt"


3.
isql -S -U -P -i analytics_query.sql << EOF
EOF


In case of big queries or multiple query execution, all sql stuffs could be packaged into a sql file and the file path could be mentioned with the option "-i".


Scope -

This automation could be used for database functionalities and running database queries as a scheduled job.




Avaneesh
07-01-2009

Tuesday, December 2, 2008

Dymanic cast

It is basically used for the safe type casting of base and derived classes.
Type casting from base class pointer to a derived class pointer is safe and does not require explicit casting, however reverse is not that much simple.
Type casting is required when we are pointing a base class object by a derived class pointer.
dynamic_cast is used for this type of herirachical casting ( upcasting ).
One pre requisite with this cast is - The base class should be polymorphic i.e. base class must have at least one virtual method.

In below example - Comment and comment out the virtual method of class A and check out the results.

#include "stdafx.h"
#include


class A
{
public :
A()
{
cout<<"A::A()"<}
~A()
{
cout<<"A::~A()"<}
//void virtual Test() {};
};


class B :public A
{
public :
B()
{
cout<<"B::B()"<}
~B()
{
cout<<"B::~B()"<}
};
int main(int argc, char* argv[])
{
try
{
A *objA = new A();
B *objB = new B();
objB = dynamic_cast(objA);
}
catch(...)
{
cout<<"error";
}
return 0;
}

Friday, October 3, 2008

Table Name Search for a Column

There could be situations when we need to know all the table names in our database which contain a required column name.The below stored procedure could be of use -
If exists ( select * from sysobjects where name

like 'GetTableName' and type = 'P'

)

BEGIN

DROP procedure GetTableName

END

GO

Create procedure GetTableName

@strString varchar(100)

As

Begin

Select name from sysobjects

where id in ( Select id from syscolumns where name like @strString )

End

GO

Monday, September 8, 2008

REALIZATION

In object oriented programming (OOPS), REALIZATION is a kind of relationship between two classes where one class provides the template for all the functions to be used , while the other class actually provides the functionalities to the defined functions.

The class providing the template is known as “Supplier” class while the class providing the implementation is called as “Consumer” class.

This type of relationship is used in Interface based architecture or template based architecture.

In the given example the class “Account” is an interface class and it gets implemented by the class
“SavingAccount”.

In UML the same thing is denoted as









#include "stdafx.h"


// Supplier class or Interface class
class Account
{
private:


protected:
float fPrincipal, fInterest, fTime, fRate;
public:
virtual void SetPrincipal(float fPrincipal)=0;
virtual void SetTime(float fTime)=0;
virtual void SetRate(float fRate)=0;
virtual void CalculateInterest()=0;
virtual void ShowInterest()=0;
};


// Consumer class or Implementor class
class SavingAccount : public Account
{
private:


protected:

public:
void SetPrincipal(float fPrincipal);
void SetTime(float fTime);
void SetRate(float fRate);
void CalculateInterest();
void ShowInterest();

};

void SavingAccount::SetPrincipal(float fPrincipal)
{
this->fPrincipal = fPrincipal;
}

void SavingAccount::SetTime(float fTime)
{
this->fTime = fTime;
}

void SavingAccount::SetRate(float fRate)
{
this->fRate = fRate;
}

void SavingAccount::CalculateInterest()
{
fInterest = (fPrincipal * fTime * fRate ) / 100;
}

void SavingAccount::ShowInterest()
{
printf("Intereset is - %f", fInterest);
}

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

objSavAcc.SetPrincipal(10000);
objSavAcc.SetRate(5.9);
objSavAcc.SetTime(2);
objSavAcc.ShowInterest();

return 0;
}






























Friday, August 29, 2008

Clustered and non clustered index

Just to explain the main difference between Clustered and non clustered index with a simple example.

Clustered index makes data storage in a sorted order based on the key/s used for indexing, while non clustered index does not.

Just have a look to the below piece of T-SQL statements to remember this -


Clustered Index Example -


drop table Clustered_Table
Go

create table Clustered_Table
(
id int,

name varchar(100)
)
Go


--Create Clsutered Index
Create Clustered Index Clust_ind_id
on Clustered_Table(id)
GO


-- Insert few records
Insert into Clustered_Table Values (1, 'Avaneesh')
GO
Insert into Clustered_Table Values (5, 'Suresh')
GO
Insert into Clustered_Table Values (3, 'Dinesh')
GO
Insert into Clustered_Table Values (2, 'Mahesh')
GO
Insert into Clustered_Table Values (4, 'Vikash')
GO


O/P –


Id name
1 Avaneesh
2 Mahesh
3 Dinesh
4 Vikash
5 Suresh



Note the output is in sorted order.





Non Clustered Index Example -

drop table Non_Clustered_Table
Go
create table Non_Clustered_Table
(
id int,

name varchar(100)
)
Go


-- Create non clustered index
Create nonClustered Index test_Clust_ind1
on Non_Clustered_Table (id)
GO


-- Insert the records
Insert into Non_Clustered_Table Values (1, 'Avaneesh')
GO
Insert into Non_Clustered_Table Values (5, 'Suresh')
GO
Insert into Non_Clustered_Table Values (3, 'Dinesh')
GO
Insert into Non_Clustered_Table Values (2, 'Mahesh')
GO
Insert into Non_Clustered_Table Values (4, 'Vikash')
GO


-- get the records with non clustred index used
select * from Non_Clustered_Table



O/P –

Id Name
1 Avaneesh
5 Suresh
3 Dinesh
2 Mahesh
4 Vikash


Note the order of the records. Same as we had entered.

Hope it will help you somewhere.
Bye.