Posts

Showing posts from December, 2013

C++ Data Structures

C/C++ arrays allow you to define variables that combine several data items of the same kind but structure is another user defined data type which allows you to combine data items of different kinds. Structures are used to represent a record, suppose you want to keep track of your books in a library. You might want to track the following attributes about each book: Title Author Subject Book ID Defining a Structure: To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member, for your program. The format of the struct statement is this: struct [ structure tag ] { member definition ; member definition ; ... member definition ; } [ one or more structure variables ]; The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before t...

C++ Basic Input/Output

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called input operation and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc, this is called output operation . I/O Library Header Files: There are following header files important to C++ programs: Header File Function and Description <iostream> This file defines the cin, cout, cerr and clog objects, which correspond to the standard input stream, the standard output stream, the un-buffered standard error stream and the buffered standard error stream, respectively. <iomanip> This file declares ser...

C++ How to use Date and Time

The C++ standard library does not provide a proper date type. C++ inherits the structs and functions for date and time manipulation from C. To access date and time related functions and structures, you would need to include <ctime> header file in your C++ program. There are four time-related types: clock_t, time_t, size_t , and tm . The types clock_t, size_t and time_t are capable of representing the system time and date as some sort of integer. The structure type tm holds the date and time in the form of a C structure having the following elements: struct tm { int tm_sec ; // seconds of minutes from 0 to 61 int tm_min ; // minutes of hour from 0 to 59 int tm_hour ; // hours of day from 0 to 24 int tm_mday ; // day of month from 1 to 31 int tm_mon ; // month of year from 0 to 11 int tm_year ; // year since 1900 int tm_wday ; // days since sunday int tm_yday ; // days since January 1st int tm_isdst ; // hours of da...

C++ References

A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable. C++ References vs Pointers: References are often confused with pointers but three major differences between references and pointers are: You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage. Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another object at any time. A reference must be initialized when it is created. Pointers can be initialized at any time. Creating References in C++: Think of a variable name as a label attached to the variable's location in memory. You can then think of a reference as a second label attached to that memory location. Therefore, you can access the contents of ...

C++ How to use pointers

C++ pointers are easy and fun to learn. Some C++ tasks are performed more easily with pointers, and other C++ tasks, such as dynamic memory allocation, cannot be performed without them. As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory. Consider the following which will print the address of the variables defined: #include <iostream> using namespace std ; int main () { int var1 ; char var2 [ 10 ]; cout << "Address of var1 variable: " ; cout << & var1 << endl ; cout << "Address of var2 variable: " ; cout << & var2 << endl ; return 0 ; } When the above code is compiled and executed, it produces result something as follows: Address of var1 variable : 0xbfebd5c0 Address of var2 variable : 0xbfebd5b6 What Are Pointers?...

C++ How to use Strings

C++ provides following two types of string representations: The C-style character string. The string class type introduced with Standard C++. The C-Style Character String: The C-style character string originated within the C language and continues to be supported within C++. This string is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null . The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello." char greeting [ 6 ] = { 'H' , 'e' , 'l' , 'l' , 'o' , '\0' }; If you follow the rule of array initialization, then you can write the above statement ...

C++ HOw to use arrays

C++ provides a data structure, the array , which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Declaring Arrays: To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows: type arrayName [ arraySize ]; This is called a single-dimension array. The arraySize must be an integer constant greater than zero...

C++ How to use numbers

Normally, when we work with Numbers, we use primitive data types such as int, short, long, float and double, etc. The number data types, their possible values and number ranges have been explained while discussing C++ Data Types. Defining Numbers in C++: You have already defined numbers in various examples given in previous chapters. Here is another consolidated example to define various types of numbers in C++: #include <iostream> using namespace std ; int main () { // number definition: short s ; int i ; long l ; float f ; double d ; // number assignments; s = 10 ; i = 1000 ; l = 1000000 ; f = 230.47 ; d = 30949.374 ; // number printing; cout << "short s :" << s << endl ; cout << "int i :" << i << endl ; cout << "long l :" << l << endl ; cout << ...