Friday, April 2, 2010

Lesson 03 – Variables

So far you have learned how to write a simple program using basic C++ concepts. Here going to discuss about the variables in C++. Variables used to store data and their existing various types of variables (Eg: char, int, float etc) to store different types of information. Each of these variable types is each a keyword that you use when you declare a variable. Using the right variable type can be important for making your code readable and for efficiency.

The syntax used to declare a variable is "type ;".

Eg: char letter;

float the_float;

int a, b, c, d;

A variable in C++ must be declared (the type of variable) and defined (values assigned to a variable) before it can be used in a program. In C++, all language keywords, all functions and all variables are case sensitive. So word ball and Ball are different keywords.

Scope of variables:

The scope of a variable can be classified in to two categories naming global and local variables.

Local variables - Local variables can be only accessed within the code itself and not outside of the function. So the scope of a local variable is limited to the function in which the variables are declared.

Global variables – Global variables can be used within and outside of any function in the program. To cater this requirement the variables are declaring outside of a function block.

For Eg:

#include

using namespace std;

int iValue1 = 1; // Global variable

int iValue2 = 2; // Global variable

int main()

{

char zCh('B'); // Local variable

cout << "Value of global variable iValue1:" << iValue1 << endl;

cout << "Value of global variable iValue2:" << iValue2 << endl;

cout << "Character in local variable zCh: " << zCh << endl;

int sum, number = 3; // Local variables with and without initialization

sum = number + 5;

cout << "Value of sum: " << sum << endl;

getchar();

return 0;

}

Friday, January 29, 2010

Lesson 02 : An example C++ Program

The most efficient way to start learning a programming language is to write programs. C++ program consists with collection of commands (commands can be either functions or keywords) and those are called C++ source code. C++ gives common functions and keywords which can be used throughout the programming.

Let us start our tour of C++ with a simple program. Here is a simple program which outputs a line of text.

// My first C++ program

#include

using namespace std;

int main()
{
cout << "Hello World!" << endl;
return 0;
}

// My first C++ program - This is a comment in C++ language. Comments can also be written starting with a slash followed by a star, and ending with a star followed by a slash. Those don’t have any effect on the program and used to enhance the readability of the program.

#include - This statement call for the inclusion of a header file to the program. A header file consists with pre defined functions and variables. Those can be used more than one program. So this is a preprocessor directive. This is for the declarations of the basic standard input-output library in C++ and tells the preprocessor to include the iostream standard file.

using namespace std; - By using this compiler can use group of functions in the standard library (std). Here you are including a namesscape to your program where all the elements of the standard C++ library are declared. The semicolon is a part of the program and it is a part of the syntax of C++. By using a semicolon you are telling the compiler that you came to the end of a particular command.

int main() – C++ programs are collection of functions and this is the main function in a particular program. Each and every program in C++ has only one main function and many other functions. This is the starting point of an execution of a program, independently of its location within the source code.
The word main is followed by a pair of parentheses and it is the function declaration in C++. Right after the parentheses finds the body of the main function followed by braces. The body of the function consists with what the function does when it is executed. Key word int is used to specify the return type of the function. This returns an integer type.

cout << "Hello World!" << endl; - This is a statement in a C++ program. This gives a visible effect in the program by outputting “Hello World!"”. As Described earlier this should also finished using a semicolon to inform the end of the statement. cout is used to declare the standard output stream in C++. This insert a sequence of characters into the standard output stream.

Operator << is the insertion stream operator. It sends contents of variable on its right to the object on its left. In our case, right operand is the string “My first C++ program” and left operand is cout object. So it sends the string to the cout object and cout object then displays it on the output screen.

endl is the line feed operator in C++. It acts as a stream manipulator whose purpose is to feed the whole line and then point the cursor to the beginning of the next line. We can use \n (\n is an escape sequence) instead of endl for the same purpose.

return 0; - This is the function return statement. This causes the main function to finish. This statement exits the function main indicating its completion and returns the value 0 to the operating system indicating that the program successfully ran to completion. It returns a variable or value to the process that calls this function.

Wednesday, January 13, 2010

Lesson 01 : Introduction to C++

C++ is an "object oriented" programming language based on the C language created by Bjarne Stroustrup of AT&T Bell Laboratories in the early 1980's and released in 1985. C++ is rich with many important features such as reusability and extensibility.

C++ is a superset of C and most carefully written ANSI C will compile as C++. The American National Standards Institution (ANSI) provides "official" and generally accepted standard definitions for C and C++. A program written only in ANSI C++ is guaranteed to run on any computer whose supporting software conforms to the ANSI standard.

It is must to have a compiler for each and every programming language. Compiler is a communication medium and it is capable of filling the gap between humans and the computer languages. So compiles take the source code written using high-level languages (Eg: C, C++ etc) and convert into machine language. Most of the compilers embedded in frameworks of software development tools and libraries. Some compiles can be downloaded free while some required payments. Here listed some of the compiles can be used for C++.

Downloaded for free:
• Apple C++
• Bloodshed Dev-C++
• Borland C++
• Cygwin (GNU C++)
• Digital Mars C++
• DJ Delorie's C++ development system for DOS/Windows (GNU C++)
• GNU CC source
• IBM C++
• Intel C++ for linux
• The LLVM Compiler Infrastructure
• Microsoft Visual C++ 2008 Express edition.
• Sun Studio

Require payment:
• Borland C++
• CodeWarrior C++
• Comeau C++
• Edison Design Group C++ Front End
• Green Hills C++
• HP C++ for Unix and HP C++ for OpenVMS
• Intel C++
• Mentor Graphics/Microtec Research C++
• Microsoft C++
• Paradigm C++
• The Portland Group C++
• SGI C++
• Sun C++
• WindRiver's Diab C++

For the front end developments it requires a frontend environment. Most common environment used for the C++ language is Dev-C++ around GNU's G++ compiler.