Developers Archive for the 'c++ programming' Category

Connecting MySQL database in C++

Connecting MySQL database in C++ Wednesday, December 13th, 2006

Connecting MySQL database in C++

 It is easy to connect the MySQL database in C++ Programming. With the Simple Syntax as follows.

#include

int main()
{
 Database db(”localhost”, “root”, “pass”, “databasename”);
 if (!db.Connected())
 {
  printf(”Database not connected - exiting\n”);
  exit(-1);
 }
}

 

But it must include the libmysqlwrapped.h otherwise it will not be connected.

 

Some Sample C++ codes, that executing SQL queries in MySQL database are :

#include

#include
using namespace std;
int main()
{
 Database db(”localhost”, “root”, “”, “tutorialdb”);
 if (!db.Connected())
 {
  cout

Passing Values in C++ :

Passing Values in C++ : Wednesday, December 13th, 2006

Passing Values in C++ :

There are many ways to get values in and out of functions in c++.

Data is passed into functions or routines via arguments. These arguments can be classified into call-by-value and call-by-reference.

• To call-by-value means to pass only the value of the data (a copy).

• To call-by-reference means to pass the reference of the data.

Here is a simple example to explain the difference between Call by Value and Call by reference.

Call by Value :

#include

using namespace std;

int double_fun(int m) { // i passed by value

m=m*2;

return m;

}

int main() {

int i=5;

int j = double_fun(i);

coutIn above example, while calling double_fun(i) argument value itself passed, So finally the value of “i” will be 5 after calling the fuction “double_fun()” and the value of “j” will be 10 which will be calculated and returned from double_fun.

Call by Reference :

#include

using namespace std;

int double_fun(int &m) { // i passed by reference, note the ambersand

m=m*2;

return m;

}

int main() {

int i=5;

int j = double_fun(i);

coutIn above example the value of “i” will be 10 after calling “double_fun()” function.

Note the only difference between above 2 programs is,

int double_fun(int &m) {

the above line will pass the reference of “m” variable. In double function m value will be doubled and returned. So the value of i will become 10.

Alias :

Call-by-reference parameter passing is one way that aliasing can be introduced. Aliasing is when multiple variables share a single value–changes to one mean all of the variables change.

Operator Overloading

Operator Overloading Wednesday, December 13th, 2006

Without changing the functionality of operators,
we can overload (extend) operators.
for instance, By Overloading + Operator ,
we can add two Strings or struct type variables.
This is done by defining operator function.

Syntax:
operator ()

example:
int operator + (1,2)

Limitations:
a. We can overload some set of operators only.
That is, Some exceptional are there(eg. :: operator).
b. We shouldn’t use operator overloading for unobvious relationships
(eg. multiplication of two string ).


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.