Basic Derived User Defined: Data Types
Basic Derived User Defined: Data Types
Data type defines size and type of values that a variable can store along with the
set of operations that can be performed on that variable.
Every variable used in program hold data, and every data must have their own
type.
C++ provides built-in data types that correspond to integers, characters, floating-
point values, and Boolean values.
DATA TYPES
Array,
Structure, Class,
Function,
Union, Enum
Pointer,
int char float double reference
The basic data types are integral, float and void. C++ language supports both
signed and unsigned literals.
Typical Bit
Type Typical Range Sample Applications
Width
-2147483648 to
signed int 4bytes Large numbers, populations
2147483647
-2,147,483,648 to
long int 4bytes Large numbers, populations
2,147,483,647
signed long int 4bytes same as long int Large numbers, populations
unsigned long
4bytes 0 to 4,294,967,295 Astronomical distances
int
Float 4bytes +/- 3.4e +/- 38 (~7 digits) Scientific (7-digit) precision)
Double 8bytes
+/- 1.7e +/- 308 (~15 Scientific (15-digit precision)
Example Program
#include <iostream.h>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}
For example:
In this case we have defined four new data types : C, NUM, word, and name
as char, unsigned int, char* and char[50] respectively.
C char_A;
Num mark;
word table;
name bca;
2. Enumerated Types
Enumerations serve to create data types to contain something different
that is not limited neither to either numerical or character constants nor to
constants true and false.
Syntax:
enum model_name {
value1,
value2,
value3,
.
.
} object_name;
Example:
3. Structures.
A structure is a set of different types of data that may have different
lengths grouped together under a unique name.
where structure_name is a name for the model of the structure type and
the optional parameter var_name is a valid identifier (or identifiers) for
structure variables.
struct product {
char name [30];
float price;
};
product guava;
product apple,orange, melon;
Once declared, product has become a new valid data type name like the
fundamental ones int, char or short and variables can be declared of that
type.
It is possible to declare the structure variables apple, orange and melon this
way:
struct products {
char name [30];
float price;
} guava,apple, orange, melon;
The members of the structure are accesses with the help of dot (.)
operator.
4. Unions
Unions allow a portion of memory to be accessed as different data types,
since all of them are in fact the same location in memory.
Its declaration and use is similar to the one of structures but its
functionality is totally different
Syntax:
union model_name {
type1 element1;
type2 element2;
type3 element3;
.
.
} object_name;
All the elements of the union declaration occupy the same space of
memory. Its size is the one of the greatest element of the declaration. For
example:
union u1 {
char c;
int i;
float f;
} mytypes;
mytypes.c
mytypes.i
mytypes.f