MY mENU


Monday, 27 February 2012

Structures in C++


Structure's in C++:
A structure is a collection of variables under a single name. Variables can be of any type: int, float, char etc. The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name.
Declaring a Structure:
The structure is declared by using the keyword struct followed by structure name, also called a tag. Then the structure members (variables) are defined with their type and variable names inside the open and close braces "{"and "}". Finally, the closed braces end with a semicolon denoted as ";" following the statement. The above structure declaration is also called a Structure Specifier.

Example:

Three variables: custnum of type int, salary of type int, commission of type float are structure members and the structure name is Customer. This structure is declared as follows:
In the above example, it is seen that variables of different types such as int and float are grouped in a single structure name Customer.
Arrays behave in the same way, declaring structures does not mean that memory is allocated. Structure declaration gives a skeleton or template for the structure.
After declaring the structure, the next step is to define a structure variable.
to declare Structure Variable:
This is similar to variable declaration. For variable declaration, data type is defined followed by variable name. For structure variable declaration, the data type is the name of the structure followed by the structure variable name.
In the above example, structure variable cust1 is defined as:
What happens when this is defined? When a structure is defined, it allocates or reserves space in memory. The memory space allocated will be cumulative of all defined structure members. In the above example, there are 3 structure members: custnum, salary and commission. Of these, two are of type int and one is of type float. If integer space allocated by a system is 2 bytes and float four bytes the above would allocate 2bytes for custnum, 2 bytes for salary and 4 bytes for commission.

 access structure members:

To access structure members, the operator used is the dot operator denoted by (.). The dot operator for accessing structure members is used thusly:
structure_variable_name.member_name

For example:

A programmer wants to assign 2000 for the structure member salary in the above example of structure Customer with structure variable cust1 this is written as:

Friday, 24 February 2012

Basic concepts of C++


Before starting to learn C++ it is essential to have a basic knowledge of the concepts of Object oriented programming. Some of the important object oriented features are namely:
          Objects
  • Classes
  • Inheritance
  • Data Abstraction
  • Data Encapsulation
  • Polymorphism
  • Overloading
  • Reusability
Objects:
Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular instance of a class. There can be more than one instance of a class. Each instance of a class can hold its own relevant data.
An Object is a collection of data members and associated member functions also known as methods.

Classes:

Classes are data types based on which objects are created. Objects with similar properties and methods are grouped together to form a Class. Thus a Class represents a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects become functions of the class and are referred to as Methods.
For example consider we have a Class of Cars under which Santro Xing, Alto and WaganR represents individual Objects. In this context each Car Object will have its own, Model, Year of Manufacture, Color, Top Speed, Engine Power etc., which form Properties of the Car class and the associated actions i.e., object functions like Start, Move, and Stop form the Methods of Car Class.
No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created.

Inheritance:

Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class. The new class that is formed is called derived class. Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming.

Data Abstraction:

Data Abstraction increases the power of programming language by creating user defined data types. Data Abstraction also represents the needed information in the program without presenting the details.

Data Encapsulation:

Data Encapsulation combines data and functions into a single unit called Class. When using Data Encapsulation, data is not accessed directly; it is only accessible through the functions present inside the class. Data Encapsulation enables the important concept of data hiding possible.

Polymorphism:

Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways.

Overloading:

Overloading is one type of Polymorphism. It allows an object to have different meanings, depending on its context. When an existing operator or function begins to operate on new data type, or class, it is understood to be overloaded.

Reusability:

This term refers to the ability for multiple programmers to use the same written and debugged existing class of data. This is a time saving device and adds code efficiency to the language. Additionally, the programmer can incorporate new features to the existing class, further developing the application and allowing users to achieve increased performance. This time saving feature optimizes code, helps in gaining secured applications and facilitates easier maintenance on the application.

Thursday, 23 February 2012

Enumeration Vs Iterator in Collections

What is the difference between Enumeration And Iterator?



Enumeration

Iterator
1. It is legacy interface and introduced in 1.0 version

1 It is non-legacy and introduced in 1.2 version
2.Applicable only for legacy classes and it is not universal cursor

2. Applicable for any Collection implemented class object.
3.While iterating the elements we are not allowed to remove the objects just we can perform only read operation

3. While iterating we can perform removal also in addition to read operation.
4.By using elements() method we can get Enumeration object

4.   By using iterator() method we can get Iterator   
    object


Iterator
Enumerator

Iterator supports remove() method


Enumerator doesn’t supports remove() method.

It is nor synchronized


It is synchronized


It supports ArrayList, vector, hashmap, hashtable


It doesn’t supports Arraylist, and hashmap.

It doesn’t supports legacy methods


It supports legacy methods like hasMoreElements(),nextElements();