Ticker

6/recent/ticker-posts

BASIC DATA TYPES IN C++

C++ Data Types

C++ Data Types

C++ Data Types

1) Built-in-type 

Integral type ñ The data types in this type are int and char. The modifiers signed, unsigned, long & short may be applied to character & integer basic data type. The size of int is 2 bytes and char is 1 byte. 

i) void - void is used for. 

To specify the return type of a function when it is not returning any value. 

To indicate an empty argument list to a function. Ex- Void function1(void) 

In the declaration of generic pointers. 

        EX- void *gp 

A generic pointer can be assigned a pointer value of any basic data type. 

Ex - int *ip // this is int pointer 

        gp = ip //assign int pointer to void. 

A void pointer cannot be directly assigned to their type pointers in C++ we need to use cast operator. 

Ex - Void * ptr1; 

        Char * ptr2; 

        ptr2 = ptr1;

is allowed in c but not in C++ 

        ptr2 = (char *) ptr1 

is the correct statement

ii) Floating type: 

The data types in this are float & double. The size of the float is 4 byte and double is 8 byte. The modifier long can be applied to double & the size of long double is 10 byte.

2) User-defined type 

i) The user-defined data type structure and union are same as that of C. 

ii) Classes - Class is a user defined data type which can be used just like any other basic data type once declared. The class variables are known as objects.

3) Enumeration 

i) An enumerated data type is another user defined type which provides a way of attaching names to numbers to increase simplicity of the code. 

ii) It uses enum keyword which automatically enumerates a list of words by assigning them values 0, 1, 2,Ö.etc. 

iii) Syntax:- 

    enum shape { 

            circle, 

            square, 

            triangle 

Now shape become a new type name & we can declare new variables of this type. 

        EX - shape oval;

iv) In C++, enumerated data type has its own separate type. Therefore c++ does not permit an int value to be automatically converted to an enum value. 

Ex.   shape shapes1 = triangle, // is allowed 

        shape shape1 = 2; // Error in C++ 

        shape shape1 = (shape)2; //ok 

v) By default, enumerators are assigned integer values starting with 0, but we can over-ride the default value by assigning some other value. 

EX:- enum colour {red, blue, pink = 3}; 

it will assign red to o, blue to 1, & pink to 3 or 

enum colour {red = 5, blue, green}; it will assign red to 5, blue to 6 & green to 7.

4) Derived Data types 

1) Arrays 

An array in C++ is similar to that in c, the only difference is the way character arrays are initialized. In C++, the size should be one larger than the number of character in the string where in c, it is exact same as the length of string constant. 

Ex -  char string1[3] = "ab"; // in C++ 

        char string1[2] = :ab"; // in c. 

2) Functions 

Functions in C++ are different than in c there is lots of modification in functions in C++ due to object orientated concepts in C++. 

3) Pointers 

Pointers are declared & initialized as in c. 

Ex-  int * ip; // int pointer 

        ip = &x; //address of x through indirection 

C++ adds the concept of constant pointer & pointer to a constant pointer. 

char const *p2 = "HELLO"; // constant pointer

ALSO SEARCH:-

"Keyword"

"basic data types in C++"

"basic data types in C++ language"

"basic data types in C++ programming"

"basic data types in C++ with examples"

"basic data types in C plus plus"

"basic data types in C and their size"

"explain basic data types in C++"

"which is not one of the basic data types in C++"

"four basic data types in C++"

"explain basic data types in C++ language"

"size of basic data types in C++"

"explain about the basic data types in C++ language with example"

"describe basic data types in C++"

"write a program to perform input/output of all basic data types in C++"

"basic and derived data types in C++"

"basic or primitive data types in C++ are"

"basic difference between variables and data types in C++"

"basic data types in cpp"