Sunday, August 12, 2012

Introduction to C++ Functions and Program Structure

Executing statements using C++ Functions, otherwise called methods or procedures in other programming languages makes it easy to structure a program by separating its tasks into self-contained units. One example of a C++ function is main function int main (). Because functions are written by the programmers, they are sometimes referred to as user-defined functions or programmer-defined functions. In definition terms, a C++ function is a block of statements that execute when called from some point of the program. Executed statements in C++ function are written once and are reused from several locations in a program, thus enabling its structuring. Statements for execution under one C++ Function  are hidden from other functions.

Executing statements using  C++ function helps to perform an assignment or a task that is required to complement full execution of the program. C++ functions enable us to structure the program but maintain its  flow and coordination.There are two kinds of C++ functions: built-in and user defined functions. The built-in C++ functions are usually in three categories: those which are part of the operating system, and others which are part of the C++ programming environment. The functionality or execution of the built-in C++ functions is normally the same irrespective of the source and the environment.

This tutorial will help you understand better the execution of statement using various forms of C++ functions, how to create one, how and when to use a function to structure a program. To declare a C++ function, you specify first its return type, i.e. the data type to be returned by the function. The return type is followed by the name or the identifier by which the function will be called. The C++ function name is followed by a pair of parentheses. Inside the the pair of parentheses also know as the parameter list area, can be left empty implying that the function will not receive any arguments or can include as many parameters as needed. To understand more on executing statements using C++ Functions, read more on introduction to C++ functions including details on, Declaration of functions, Function Prototype and Function Signatures, Argument Coercion, Argument Promotion Rules, and Void Function Or Function with no Return Type.


Friday, December 9, 2011

Using C++ Control Statements to Transfer Control of Execution

Introduction to C++ Control Statements: Statements that specify the order in which actions are executed are called control statements. C++ control statements in a program execute one after the other in the order they are written (also known as sequential execution) unless there is a direct instruction to act otherwise. All the programs we have used so far included sequential execution which is one of types of C++ control statements. There are however other ways in which a program can execute a statement other than the sequence of next in the line . Enabling this to happen is known as transfer of control. Transfer of control includes other two forms of control statements. These types of C++ control statements  include selection statements and repetition statements.

Selection Statements

Selection Statements which are forms of C++ control statements are sub divided in three types of other types namely, if, if….else and switch control statements.

If Selection Statement

One form of a C++ control statement is the if selection statement which performs an action if a set condition is correct but if the condition is false the program skips the action and execution continues with the rest of the statements. An if statement is written with a key if followed by a condition enclosed in braces and then a statement to be executed when the condition is correct. Where the if form of control statement is to be used, it is advisable to enclose statements in curly brackets, where more than one are to be executed, . 

For more information on C++ control statements, please refer to the detailed chapter on C++ Selection Statements, Repetition C++ control statements and Other transfer of control statements .

Tuesday, November 29, 2011

Understanding Pointer Variables in C++

Understanding Pointers Variables


A pointer in C++ is an address of a memory location where data is stored. Understanding pointer variables in in C++ provides an opportunity to advance knowledge on how programs access data in memory. A pointer in in C++ provides an indirect way of accessing data in memory. When a variable is declared, the compiler reserves and assigns it a portion of space in memory. This portion of memory is known to the computer by the address associated to it. Knowing the memory address of the variable helps us in understanding pointers in in C++. Once we know this address, we can utilize it using the a reference or a pointer. A pointer variable is defined to point (to store the address of) to the data stored at a specific memory.


Understanding how to Declare and Initialize a Pointer in C++


To understand how to initialize and declare a pointer variable in c++, start with the data type followed by a dereference operator (*) and the name of the variable. Once a C++ pointer has been declared, we assign it with the address of the variable to store. By default, it does not matter how you append the *, as long as it is between the data type and the pointer variable. The name of a pointer variable, must comply with the rules that govern every variable. The value of a declared pointer variable in c++ is the address to which it points. Follow the example below.
      int    *ptr1;                    // pointer to an int
      float   *ptr2;                    // pointer to a float 

Assume we have two variables age and height,we can now store the address of each in our pointer variables  ptr1 and ptr2 respectively as follows:

                ptr1 = &age;       //ptr1 points to age;

                ptr2=&height     //ptr2 points to height;

Read the whole chapter on understanding pointer variables in C++

Wednesday, November 2, 2011

Introduction to C++ Arrays

Understanding C++ Arrays


C++ Arrays help us understand how to store large data. C++ arrays consists of set of objects (called its elements), all of which are of the same type and are arranged contiguously in memory. understanding C++ Arrays provide you with a convenient way to manipulate storage of large amounts of data from within programs. C++ Arrays allow for setting aside a group of memory locations which we can then manipulate as a single entity, at the same time enabling us direct access to any individual component.

C++ Array elements are grouped under one symbolic name or identifier. All C++ array elements are positioned next to one another in the memory and are indexed in the order they are arranged beginning from position zero. The index of each C++ array element is used to access that element. The number of elements in a C++ array is called its dimension. The dimension of a C++ array is fixed and predetermined, it cannot be changed during program execution.

A C++ array is declared as follows:
 
DataType ArrayIdentifier [NumberOfElements]

Suppose we want to store the grades of 10 students in a class. We can declare and initialize C++ array elements of grades array as follows;

int grades[10]={35,60,55,50,65,80,70,75,45,85}

Read the whole Chapter on C++ Arrays

Thursday, September 15, 2011

C++ Classes incliding Structures-Classes-Unions

Introduction to C++ Classes

Classes (also known as composite data types) are a collection of different data types grouped together to form one new self-defined data type which can be categorized in form of a structure, class or union. The variable types we have used so far have been primitive types because they are declared in a very simplified manner. Each variable represented an object in its whole ( i.e int car) but at times we want a group of values to be considered as one entity to represent an object (car).

For example, we may need to use one or more data types to represent the car object. In such a situation we can group the different data types into one data type of our liking and then use it the same way we have used other data types. In C++, there are three techniques of defining a new self-defined data type: a structure, a class, and a union.

Structures

A structure is an aggregate of data types that together form new self-defined data type. It is built using several types including the structs. We create a structure using a the struct keyword, followed by a name for the object and the body of the structure with a semicolon at the end. The naming of the struct must follow conventional rules of names in C++. To create a composite type at least one known data type should be used to declare a variable in the body of the structure. Below is an example of a car structure.Read more on C++ Classes

Understanding C++ Operator Overloading and User Defined Class Objects

In understanding c++ operator overloading we begin by defining the Concept. In C++ operator overloading means ‘providing different functionality to an operator under a different environment. Majority of the C++ built-in operators are already overloaded. Like functions, operators take operands (arguments) and return a value. operator overloading can be done on many operators for them to act in a specific manner on the argument or arguments and return a value.

To have a better understanding of c++ operator oveloading, let us look at this example. The plus (+) operator is overloaded to add two integers, increment an integer by one and concatenate a string. Likewise a minus (-) operator is overloaded to subtract two integers, and decrement an integer by one. Like the + and –, C++ provides built-in overloaded definitions for most operators to perform various functionalities directly but only on built-in types. However, C++ provides abilities for a programmer to overload built-in operators to act on user-defined types in a user-defined manner. Read the details onunderstanding C++ Operator Overloading

Introduction to C++ class inheritance

In C++, inheritance is a technique in which a C++ class known as the derived class absorbs, utilizes and enhances the data and behavior of another class known the base class . C++ inheritance enhances object-oriented programming in a way that it saves program development time, and supports reuse of debugged high quality software. In c++ inheritance allows an object oriented programmer to focus on the larger picture rather than specific cases. Because different classes sometimes require similar data members, inheritance enables avoidance of duplication of codes in the same project environment. The base class captures the most common members and then supplies them to the derived classes with the help of inheritance technique. Details