Data Types cont .....3

Pointer

A pointer is a variable which holds the address of some other variable. Each variable is stored at a particular location inmemory, which is known as the address of the variable. This address can be stored in a pointer.

The syntax of a pointer is :

<type> *<pointer name>;

For example

int *ptr; // ptr is a user defiend name for the pointer

Pointers are used in C++ program for - Dyna,ic memory allocation, Passing variables by reference to functions, Accessing class data members and member functions.

Reference Variable

A reference is an alias or another name for a variable. The syntax for declaring a reference variable is:

Type &Ref = Var;

For example : int &y = x;

This statement means hat y can be used in place of x.

Constant

The keyword const can be added to the declaration of an object to make that object a constant, rather than a variable. The value of a named constant cannot be altered during the program execution. The syntax for a constant declaration is const type name=value;

For example: const int adult_age=18;

This declares a constant named as adult_age of type integer that holds value 18.

User-Defined Data Types

Structure

A structure is a collection of variables(of different data types) referenced under one name. It is a way of keeping all the related information together. The variables defiend within a structure are called structure elements. The keyword struct is used to declare a structure.

Union

a union is a memory location shared by two or more variables of the same or different types at different times. The keyword union is used for declaring and creating a unoin of these variables.

Enumeration

It is a method for naming integer constants and is more convenient than using a constants. By default, the enumerator values start from zero and increase by one towards the right-hand side.

Class

In C++ a class defines a data type, much like a structure does. A class provides a set of (usually public)operations (member functions)and a set of (usually non-public) data members representing the abstract values that instances (objects) of the type can have. A class may be defiened as a group of objects with the same operations and attributes.

HOME LEARN C++ PREVIOUS NEXT