Topics:    Namespace     Keywords    Identifiers    Constants    Data types Namespace: • It defines a scope for the identif...

Basic of C++ Keywords,Identifiers,Constants and Data types

/
0 Comments
Topics:
  1.   Namespace
  2.   Keywords
  3.   Identifiers
  4.   Constants
  5.   Data types

Namespace:

• It defines a scope for the identifiers that are used in a program.
• For using identifiers defined in namespace using directive is used as follows:
using namespace std;
• std is the namespace where ANSI C++ standard class libraries are defined.
• All ANSI C++ programs must include this directive.
• This will bring all the identifiers defined in std to the current global scope.
This keyword allows the programmer to better divide up the program into different namespaces or sorts of modules

Keywords
• They are explicitly reserved identifiers and cannot be used as names for the program variables or other.
• It is used to declare, define any function variable like to make we use class keyword.
user-defined program elements. 
Ex: int, class, void etc. 

Identifiers
• They refer to the names Of variables, functions, arrays, classes etc., created by the programmer. 
• Each language has its own rules for naming these identifiers. 
• Following are common rules for both C and C++: 
Only alphabetic characters, digits and underscores are permitted. 
The name cannot start with a digit. 
Uppercase and lowercase letters are distinct. 
A declared keyword cannot be used as a variable name. 

Constants:
• Like variables, constants are data storage locations. But variables can vary, constants do not change. • You must initialize a constant when you create it, and you can not assign new value later, after constant is initialized. 
Defining constant using #define: 
#define is a preprocessor directive that declares the symbolic constant. 
Example syntax: 
#define Pl 3.14 
Every time the preprocessor sees the word PI, it puts 3.14 in the text.


Explain various Data types used in C++. 
• C++ provides following data types.
• We can divide data types into three parts
1. Primary data type
2. Derived data type
3. User-defined data type




Primary/ Inbuilt Data types: 

• The primary data type of C++ is as follow. 
                            Size (bytes)                  Range 
char                          1                              -128 to 127   
unsigned char           1                              O to 255 
short or int                2                             -32,768 to 32,767 
unsigned int              2                             O to 65535 
long                          4                              -2147483648 to 2147483647 
unsigned long           4                             O to 4294967295 
float                          4                             3 .4e-38 to 3.4e+308 
double                       8                             1.7e-308 to 1.7e+308 
long double               10                           3 .4e-4932 to 1.1e+4932 

• Primary or inbuilt data types means the data type which is already declared and defined by the system that data type is the primary or inbuilt data type.
• Primary or Inbuilt data types are also known as predefine data type.
• int, char, float are the example of the primary data type.

< Derived data type:
• Derived data means the data type declared by the by the system and defined by the user.
• Data type like function, array and pointer are the derived data type.
• The array is declared by the system but it not defined by the system. In the array, we define the length of the array and its values.
1. Arrays 
2. Function 
3. Pointers 

  • We cannot use the derived data type without the use of primary data type. 
  • Array: An array is a fixed-size sequenced collection of elements of the same data type. 
  • Pointer: Pointer is a special variable which contains the address of another variable. 
  • Function: A Group of statements combined in one block for some special purpose. 

User Defined Data types: 

• We have the following type of user-defined data type in C++ language. 
  1. Structure
  2. Union
  3. Class
  4. enum
• The user-defined data type is defined by the programmer as per his/her requirement. 
• The user defines data types means the data type which is declared and defines by the user.
A user-defined data type (UDT) is a data type that derived from an existing data type. You can use UDTs to extend the built-in types already available and create your own customized data types.
• C++ attacks this problem by allowing a user to directly define types that behave in (nearly) the same way as built-in types. Such a type is often called an abstract data type. I prefer the term user-defined type. A more reasonable definition of abstract data type would require a mathematical ‘‘abstract’’ specification
Structure: Structure is a collection of logically related data items of different data types grouped together and known by a single name. 
Union: Union is like a structure, except that each element shares the common memory. 
Class: A class is a template that specifies the fields and methods of things or objects. A class is a prototype from which objects are created. 
enum: Enum is a user-defined type consisting of a set of named constants called enumerator. 
In other words enum is also used to assign numeric constants to strings 
Syntax of enumeration: enum enum_tag {list of variables}; 
Example of enumeration: enum day—of—week {mon=l, Cue, wed, thu, fri, sat, sun} ; 




You may also like

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts