Apr 27, 2013
Display Coding in blogspot Post
Display Coding in blogspot Post :
This tool is used to display your HTML program codes in blogger post. To get the converted code, just copy your HTML code and paste in the text area and click "Get Converted Code" button. Now the converted code is displayed below the text area box. You can copy that converted code and paste it in your blogger post to display the code in HTML format.
Enter Your Code Here.
Apr 14, 2013
How can I handle easily a pointer to a method?
How can I handle easily a pointer to a method?
If you use templates, you won't have to write the annoying type of a pointer to a method (at the 'cost' of using a template). You can also use typedef to declare the type.
struct A
{
void m() {}
};
template
void f(A &a
8
PMETHOD pm)
{
(a.*pm)()
}
void function()
{
A a;
f(a, &A::m);
}
How do I declare, assign and call a pointer to a method?
How do I declare, assign and call a pointer to a method?
Note that a pointer to a method does not hold a second pointer to an instance of a
class. To use a pointer to a method, you need an instance of the class onto which this
method can be called (possibly a second pointer).
struct A
{
void m(int) {}
};
void function()
{
void (A::*pm)(int) = &A::m; // pointer on method
A a; // instance of a class
(a.*m)(1); // calling the method with parameter value 1.
}
How do I put the code of methods outside classes?
How do I put the code of methods outside classes?
It is possible to put in a class only the prototype of the methods, and to put all the
algorithms outside. This is recommended, because it allows the programmer to read
a short prototype of the class, and makes re-usability easier:
// in a header file (.h file)
struct A
{
int a;
A();
virtual ~A();
void f();
};
// in a compilation unit (.cpp file)
A::A()
{
/* ... */
};
A::~A()
{
/* ... */
};
void A::f()
{
/* ... */
};
How do I free the memory allocated to a class?
How do I free the memory allocated to a class?
The method called when the memory occupied by a class is freed is called the destruc-
tor. With derived classes, destructor should always be virtual. If a class is destroyed
through a base class pointer whose destructor isn't virtual, the result is undened
(only part of the destructors will be called).
struct A
{
A() {}
virtual ~A() {}
};
struct B: public A
{
B() {}
~B() {}
};
void function()
{
B *b = new B();
A *a = b;
delete a; // calls ~A and ~B. If ~A wasn't virtual, only ~A would be called.
}
How do I create an instance of a class?
How do I create an instance of a class?
The methods called when a class is created are called contructors. There are four
possible ways of specifying constructors; the fth method is worth mentioning for
clarifying reasons:
- default constructor
- copy constructor
- value constructor
- conversion constructor
- copy assignment (not a constructor)
struct A
{
A() { /* ... */ } // default constructor
A(const A &a) { /* ... */ } // copy constructor
A(int i, int j) { /* ... */ } // value constructor
A &operator=(const A &a) { /* ... */ } // copy assignment
};
struct B
{
B() { /* ... */ } // default constructor
B(const A &a) { /* ... */ } // conversion constructor
};
void function()
{
A a0(0, 0); // shortcut, value constructor
A a1(a0); // shortcut, copy constructor
B b1(a1); // shortcut, conversion constructor
B b; // shortcut, default constructor
b1 = a0; // conversion contructor
a0 = a1; // copy assignment
}
Who has access to class members?
Who has access to class members?
There are three levels of access to members
private : access is granted only to the class' methods and to friend functions
and classes (Question 6.17 explains friend).
protected : access is granted only to the methods and to derived classes' meth-
ods.
public : access is granted to everyone.
Restricting access to members is usefull for detecting illicit use of the members of a
class when compiling, as shown in the following code.
class A
{
private:
int a0;
void f0() { /* ... */ }
protected:
int a1;
void f1() { f0(); } // ok
public:
int a2;
void f2() { /* ... */ }
};
void function()
{
A a;
a.a0 = 0; // error
a.f0(); // error
a.a1 = 0; // error
4
a.f1(); // error
a.a2 = 0; // ok
a.f2(); // ok
}
How do I use class and struct?
How do I use class and struct?
A C++ struct (or class) is almost like a struct in C (i.e. a set of attributes), but
has two additional kinds of members: methods and data types. A class' method has
to be used with an instance of that class. The important is that methods have always
access to their instance's attributes and data types.
struct A
{
typedef char t_byte; // type
unsigned char i; // attribute
void m() // method
{
t_byte b = 1; // m can access type t_byte
3
i = b; // m can access attribute i
}
};
void function()
{
A a;
a.m(); // an instance is required to call a method.
C++ Functions
C++ Functions
1 How do I declare, assign and call a pointer to a function?
int f(int, char)
{
// ...
}
// ...
int (*pf)(int, char); // pointer to a function
pf = f; // assignment
pf = &f; // alternative
int r = (*pf)(42, 'a');
2 How do I declare a type of a pointer to a function?
typedef int (*pf)(int, char);
3 How do I declare an array of pointers to a function?
typedef int (*pf)(int, char);
pf pfarray[10]; // array of 10 pointers to a function
How do I use references?
How do I use references?
A reference is merely an initialized pointer. This reduces signicantly zero pointer and
un-initialized pointers errors. Prefer references to pointers. Although the two following
codes look equivalent, the second implementation prevents invalid compilation.
// in C // in C++
int i; int i;
int *pi = &i; int &ti = i;
int *pj; int &rj; // g++ refuses to compile that
*pi = 0; ri = 0;
*pj = 0; // segfault rj = 0;
How do I use namespaces?
How do I use namespaces?
Namespace allow to code dierent entities without bothering about unicity of names.
A namespace is a logical entity of code. A namespace can be included into another
namespace (and so on). If a namespace is anonymous, its code is only accessible from
the compilation unit (.cpp le), thus equivalent to using static and extern in C.
// in file1.cpp
namespace name1
{
void function1() { /* ... */ }
namespace name2
{
void function2() { /* ... */ }
}
}
namespace // anonymous namespace
{
void function() { /* ... */ }
}
void function3() { function(); } // ok
// in file2.cpp
void function4a() { function1(); } // error
void function4c() { name1::function2(); } // error
void function4c() { function(); } // error
void function4b() { name1::function1(); } // ok
void function4d() { name1::name2::function2(); } // ok
using namespace name1; //makes accessible the entire namespace name1
void function4e() { function1(); } // ok
void function4f() { name2::function1(); } // ok
using name1::name2::function2; //makes accessible function2 only
void function3g() { function2(); } // ok
What is C++
What is C++?
C++ is C with classes. It was designed for one person to manage large amounts
of code, and so that a line of C++ express more thing than a line of C. The main
functionality C++ adds to C are:
- control of the accessibility to code within the source les (namespace, struct and class).
- mechanisms that make a line of code more expressive (constructors, destructors, operators, ...).
- object programming (class derivation).
- generic programming (templates).
Quick notes to C programmers
Quick notes to C programmers:
- instead of macros use
- - const or enum to de ne constants
- - inline to prevent function call overload
- - template to declare families of type and families of functions
- use new/delete instead of free/malloc (use delete[] for arrays)
- don't use void* and pointer arithmetic
- an explicit type conversion reveals an error of conception.
- avoid to use C style tables, use vectors instead.
- don't recode what is already available in the C++ standard library.
- variables can be declared anywhere: initialization can be done when variable is required.
- whenever a pointer cannot be zero, use a reference.
- when using derived class, destructors should be virtual.
Apr 7, 2013
Convert php array to javascript array
Convert php array to javascript array
Create php array first,
<?php
$arrPhp=array("1","2","3","4","5');
?>
Create javascript array second
<script>
var arrJava=new Array();
</script>
Now assign php array value to javascript array value
<script>
<?php
for($i=0;$i<count($arrPhp);$i++)
echo "arrJava[".$i."]=".$arrPhp[$i].";";
?>
</script>
the php array now assigned to javascript array.
Remove special charecter using javascript
remove special charecter using javascript:
<input type="text" onkeyup="checkspl(this)";>
/*--- this code in script place ---*/
function checkspl(as)
{
var dd=as.value;
if(/^[a-zA-Z0-9- ,]*$/.test(dd) == false)
{
dd=dd.substring(0,(dd.length-1));
as.value = dd;
}
}
Trim spaces from starting and ending of string using javascript
Trim spaces from starting and ending of string using javascript
function removeSpace ()
{
var str=document.getElementById("tex").value;
str[t] = str[t].replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
}
return str;
}
Hide files from Gallery or Music Player in android mobile
Instead of deleting the files or moving them to your computer all of the time, you can hide them in just a few seconds.
All you need to do is create a folder called .nomedia wherever you want on your sdcard (you can do this from your phone by selecting Menu button -> Create Folder), then put the particular files on that folder and the files will not appear in media apps.
You can also press the Menu key on your phone, then choose More -> go to Settings and untick the "Show hidden files" option if it is enabled. That way you will hide the .nomedia folder from the My Files app aswell.
Split operators from string in javascript
1. First Method to remove operators:
'123+456-321'.split(/([-+\/*])/) // gives == ["123", "+", "456", "-", "321"]
2. Second Method to remove operators:
<script type="text/javascript">
var strIn = '123+456-321';
var tarrN = strIn.split(/\D/);
var tarrO = strIn.split(/\+|\-|\*|\//);
alert(tarrN.join('\n')+'\n\n'+tarrO.join('\n'));
</script>
PHP - To get IP address
The following code is used to get ip address by using PHP
get_ip_address()
(Custom function by John)
get_ip_address -- Retrieves Ip adres from user and checks for proxy server.
Description
(string) get_ip_address()
This simple function checkes whether the uses is behind a (transparent) proxy, and returns the found IP adres.
Function
function get_ip_address()
{
if (isset($_SERVER)) {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && ip2long($_SERVER["HTTP_X_FORWARDED_FOR"]) !== false) {
$ipadres = $_SERVER["HTTP_X_FORWARDED_FOR"];
} elseif (isset($_SERVER["HTTP_CLIENT_IP"]) && ip2long($_SERVER["HTTP_CLIENT_IP"]) !== false) {
$ipadres = $_SERVER["HTTP_CLIENT_IP"];
} else {
$ipadres = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR') && ip2long(getenv('HTTP_X_FORWARDED_FOR')) !== false) {
$ipadres = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_CLIENT_IP') && ip2long(getenv('HTTP_CLIENT_IP')) !== false) {
$ipadres = getenv('HTTP_CLIENT_IP');
} else {
$ipadres = getenv('REMOTE_ADDR');
}
}
return $ipadres;
}
?>
PHP simple Memcache implementation
Description:
You can use this class to simply cache your data with Memcache in PHP.
Requirements
- PHP 5.3 or higher (you can change "__construct" to "cache" to make it work in older versions)
- Memcache extention installed (how to install the Memcached extention?)
- Memcached installed on a server (how to install Memcached?)
cache.php
Save the code below to a file named cache.php, and include this file in your php code (include "cache.php";)
# Copyright 2012 John Post - StarfixIT
# NOTE: Requires PHP version 5.3 or later with Memcache module
class cache
{
private $host = '127.0.0.1';
private $port = '11211';
private $memcache;
private $lifetime;
private $name;
function __construct($name = false, $lifetime = 600)
{
//Check if Memcache is installed
if(!class_exists("Memcache")){
exit("You need to install memcache");
}
//Check if allready connected to memcached
if (isset($_GLOBALS["memcache"])) {
//Yes, use old connection
$this->memcache = $_GLOBALS["memcache"];
} else {
//No, make new connection
$this->memcache = new Memcache;
$this->memcache->connect($this->host, $this->port);
$_GLOBALS["memcache"] = $this->memcache;
}
//Set global livetime
$this->lifetime = $lifetime;
//Set global name
if ($name !== false) {
$this->name = $this->makeNameOk($name);
}
}
//Public function to set the name
public function setName($name)
{
$this->name = $this->makeNameOk($name);
}
//Public function to set the cache, livetime optional
public function setCache($data, $lifetime = false)
{
$lifetime = $lifetime === false ? $this->lifetime : $this->lifetime;
$this->memcache->set($this->name, $data, MEMCACHE_COMPRESSED, $lifetime);
}
//Public function to get the cache, optional name
public function getCache($name = false)
{
$name = $name === false ? $this->name : $name;
$result = $this->memcache->get($name);
if ($result !== false) {
return $result;
}
return false;
}
//Public function to remove the data from Memcached
public function remove( $name = false )
{
$name = $name === false ? $this->name : $name;
$this->memcache->delete( $name );
}
//Function to create a clean alias
private function makeNameOk($name)
{
return preg_replace('/[^A-Za-z0-9_]/', "", $name);
}
}
Example
//Include Cache class
include "cache.php";
//Open the class with name "myData" and 900 secs lifetime
$cache = new cache("myData", 900);
//Get the data from cache
$data = $cache->getCache();
if( $data === false ){
//if the cache is expired, run the expensive function
$data = very_expensive_function();
//Save the data to cache
$cache->setCache( $data );
echo "Cache set:
";
}else{
echo "Got data from cache:
";
}
//Output the data
echo $data;
//This is just a silly example of an expensive function (takes about 2 seconds)
function very_expensive_function(){
$count = 19500000;
$time_start = microtime(true);
for($i = 0; $i < $count; ++$i);
$i = 0; while($i < $count) ++$i;
return number_format(microtime(true) - $time_start, 3);
}
Data Cards 2G/ 3G GPRS Settings for all network operators
Reliance
APN: rcomnet
Access Number: *99#
Aircel
APN: aircelgprs
Access Number: *99***1#
Idea
APN: internet
Access Number: *99#
Docomo
APN: TATA.DOCOMO.INTERNET
Access Number: *99#
Airtel
APN: airtelgprs.com
Access Number: *99#
BSNL 3G
APN: bsnlnet
Access Number: *99#
BSNL 2G
APN: bsnlnet
Access Number: *99#
MTNL 3G
Postpaid: APN: mtnl3g
Prepaid APN: pps3g
Access Number: *99#
Connect Samsung galaxy y to PC for Internet
Connect Samsung galaxy y to PC for Internet
The following steps are used to connect your Galaxy mobile with your PC / Laptops
STEP 1 :- Download Samsung KIES and Install it on your PC.
STEP 2 :- Now Open Samsung Kies and Plug the Mobile USB to PC.
STEP 3 :- It will start automatically Installing Samsung galaxy Drivers and Check if "Phone USB driver" has been Successfully Installed.
STEP 4 :- Now Open Control Panel and Go to "Phone and Modems"
STEP 5 :- Click on "Modems" Tab and Here it will be showing your Mobile with your Model Number then Right Click on it and goto Properties.
STEP 6 :- Click on "Change Settings" tab and Click on Advance and Paste the below code.
AT+CGDCONT=1,"IP","airtelgprs.com"
Note :- This Code is only for Airtel Users other customer have to contact their customer care( Dial 198 )
STEP 7 :- Again Open Control Panel and Goto "Network and Sharing Center"
STEP 8 :- Now Click on "Set up a New Connection" and choose Connection option "Set up a dial-up Connection"
Dial Phone Number :- *99#
Username :- Airtel
STEP 9 :- Click on Connect.
Now Internet Connection is ready to use Just open your Browser and Use it.....
If you have any difficulty while connection your Mobile to PC Just leave a comment i will defiantly try to solve it as soon as Possible.
Samsung Galaxy Y Secret Codes
Samsung Galaxy Y Secret Codes are given below.
*#*#4636#*#* - phone information
*2767*3855# - hard reset
*#*#7780#*#* - factory reset
*#*#7594#*#* - change end call/ power option
*#*#197328640#*#* - service mode
*#*#273283*255*663282*#*#* - file copy screen (backup media files)
*#*#526#*#* - wlan test
*#*#232338#*#* - shows wifi mac address
*#*#1472365#*#* - gps test
*#*#1575#*#* - another gps test
*#*#232331#*#* - bluetooth test
*#*#232337#*# - shows bluetooth device address
*#*#8255#*#* - gtalk service monitor
*#*#0283#*#* - packet loopback
*#*#0*#*#* - lcd test
*#*#0673#*#* - melody test
*#*#0842#*#* - device test (vibration test and backlight test)
*#*#2663#*#* - touch screen version
*#*#2664#*#* - touch screen test
*#*#0588#*#* - proximity sensor test
*#*#3264#*#* - ram version codes to get firmware version information:
*#*#4986*2650468#*#* - pda, phone, h/w, rfcalldate
*#*#1234#*#* - pda and phone
*#*#1111#*#* - fta sw version
*#*#2222#*#* - fta hw version
*#*#44336#*#* - pda, phone, csc, build time, change list number
Subscribe to:
Posts (Atom)