Saturday, November 8, 2014

C++ Beginners - Few Tips

Hi, Everyone

Hope you all are good.
From last year I was trying to return back and finally I'm back. :)
Lot's of people have requested me to return back and appreciated my blog.
I'm glad that so many people liked my blog so much. Thanks to all who spend there precisions time to read it.
Thanks Again!! :)

Today, in this post I want to point out few basic things which we need to know as a Professional Developer. When we are in college and Programming Beginner we never know or didn't get the opportunity to learn or we learn it but it's not a good practice to apply at professional world. This post will help you find your basic questions and tips to write a professional code.

Before we start let me share two important Mantras: 
"Never trust end users"
"You are responsible for your code"

Tips/Suggestions:
Tip 1.
#include <iostream>                                    // 1  
using namespace std;                                    // 2  
                                                                             

int main(int argc, const char * argv[]) {      // 3 
     cout << "I'm a Programmer";                // 4 
    return 0;                                                    // 5 
}                                                                           


Let me explain you the code.

Line 1: #include <iostream> 
Line beginning with a hash sign (#) are preprocessor directives which instructs the preprocessor to include a standard C++ code, known as header. Here, iostream has been included to allows our code to perform standard input and output operations.

Thinking, What's preprocessor? Just go though this image.



Line 2: using namespace std;
A namespace defines a new scope. They provide a way to avoid name collisions (of variables, types, classes or functions) without some of the restrictions imposed by the use of classes, and without the inconvenience of handling nested classes. 

std namespace is declared in iostream.


I shall always recommend to write Line 4 as std:cout << "I'm a Programmer"; in place of declaring namespace at top.

Why? Suppose there are two variables with same variable name in two separate namespace. Now, if you include both of those using "using". At the time of implementation section it's confusing which variable will be included which may lead to un expected results.



"Namespaces are a powerful addition to an already powerful language, giving the programmer more flexibility, provided he knows how to take advantage of it."


Line 3: int main(int argc, const char * argv[])
When our program will start executing OS will call main method to start it's operation. 
We can recevice paramter to our code. Say our program is complilted and output file name is DemoCode.

Now if we run our code like this:
$ DemoCode India USA UK
argc will give 4 as number of arguments and argv array will have 4 arguments, DemoCode, India, USA and UK.

Line 4: cout << "I'm a Programmer";
It's a way to print a message in Console/output stream.

Line 5: return 0; or return EXIT_SUCCESS;
Once a program has a successful execution it return 0 or use macro EXIT_SUCESS to OS. Otherwise, we can return 1 or EXIT_FAILURE macro which denotes execution failed.
Value of EXIT_SUCCESS and EXIT_FAILURE are declared in header <cstdlib>
                                                                                                                                                                    

Tip 2: 
Comma operator ( , )

For example:
int b;
int a = (b=5,b+2);

In this code, second line first the value 5 will be assigned to b after that 2 will be added to b and last it will be assigned to a. So, a will be 7.

Read More: http://en.wikipedia.org/wiki/Comma_operator
                                                                                                                                                                  

Tip 3:

Macros

Format:
#define [identifier name] [value]
Macro is familiar word for Beginners and a common use most of you may have seen:

#define M_PI 3.14159                                // Approx. value of PI used


float radious = 25.0; 

float circumference = 2 * M_PI * radious;
Read More: https://gcc.gnu.org/onlinedocs/cpp/Macros.html
                                                                                                                                                                

More will be updated soon....

Monday, September 16, 2013

Back Again

Hi!
Everyone,

It's been almost two years now since I last posted anything in my blog.
There are so many things to learn in my life. I still need a lot of time and effort for self study.

But, finally I'm back with more experience and topics. I shall try to share few good topics for everyone. Hope you all like it.

Soon, I shall post some interesting topics..... related to Programming, System, Linux and Networking.

Monday, November 14, 2011

How to get Window's minimize and maximize button in fedora 16

This is something which I believe should have been available by default.
Thou, the below steps to get that is not so difficult and also little bit of fun.

Step 1. Download gconf-editor
# yum install gconf-editor

Step 2. Open gconf-editor
# gconf-editor

Step 3. Go to / -> desktop -> gnome -> shell -> windows

Step4. Now click on value of button_layout and change it to:
"menu:minimize,maximize,close"

Step 5. Logout and Login back ...... it's done! :)

Note: if you want some space between menu buttons use "space" for eg:
"menu:minimize,maximize,space,close"

Friday, September 16, 2011

Tip / Trick for web developers

Today I was trying to install JEvent2.0 in Joomla1.5.23 for one of my client.
But, when ever I "upload and install" JEvent I was getting few MySQL error messages.
Something like this:
* Component Install: DB function failed with error number 2006
MySQL server has gone away SQL=INSERT INTO ULJoomla_components VALUES( 0, 'JEvents MVC', 'option=com_jevents', 0, 0, 'option=com_jevents', 'JEvents MVC', 'com_jevents', 0, 'js/ThemeOffice/component.png', 0, '', 1 )
SQL =

INSERT INTO ULJoomla_components VALUES( 0, 'JEvents MVC', 'option=com_jevents', 0, 0, 'option=com_jevents', 'JEvents MVC', 'com_jevents', 0, 'js/ThemeOffice/component.png', 0, '', 1 )

* Error Loading ModulesMySQL server has gone away SQL=SELECT id, title, module, position, content, showtitle, control, params FROM ULJoomla_modules AS m LEFT JOIN ULJoomla_modules_menu AS mm ON mm.moduleid = m.id WHERE m.published = 1 AND m.access <= 2 AND m.client_id = 1 ORDER BY position, ordering

Solution:
Extract the archive file and paste the complete path and press "Install" (not upload and install).
But, in my case I don't have full path since I have access to joomla's root directory using ftp only.
Here, goes the solution, how I made it worked for me:
1. upload the archive file into (joomla'root directory)/tmp/
2. now use "upload and install" method to install file.
Finally installed smoothly without a single issue.

Thursday, September 15, 2011

Alias to find process id of a running process

This is an example for those who believe in saving time.
You can create your own aliases to save your time from typing long commands again and again.
Create alias
# alias p_id='echo -n '\''process name:'\'';read a;ps -e | grep firefox | cut -d '\'' '\'' -f 2'
Execute it
# p_id
process name:[name of the running process]
[output will be the process id]

Monday, August 8, 2011

[Alpha version] Now it is simple to install rpm packages on other pc which don't have internet connection.

Run this script package-script.sh from the system which has internet connection then copy the package.tar.gz file.

Now,
How to install packages on other pc which don't have internet connection.
1. paste package.tar.gz into non-internet pc.
2. Switch to root user using
$ su -
3. Extract tarball
# tar -C packagepkd -xvf package.tar.gz
4.
# cd packagepkd/.package/
5. rpm -ivh ....
check "man rpm" for more details.

Note:

Kindly, report here if you find any bug.

How to remove all *.lnk files from pen drive

Yesterday, I have connected my pen drive on someone else's system. Now, when I return home and connected my pen drive on my system, I have found lnk files for each file and directory in my pendrive. It is a waste of time deleting those files one by one. So, here is a simple command to remove all lnk files.

# find /path/to/pendrive -name '*.lnk' -exec rm -v '{}' ';'