Technical Point.

The Best Platform of Diploma CSE Students.

Subscribe Us

Breaking

Monday, May 11, 2020

ELEMENTS OF C++ LANGUAGE:,

C++ Data Types

The data type is a category of data. In other words, a data type is a collection of data values with similar characteristics. In the C++ programming language, every variable needs to be created with a data type.
The data type of a variable specifies the following.
  • The type of value can be stored in a variable.
  • The amount of memory in bytes has to be allocated for a variable.
  • The range of values has to be stored in a variable.
The C++ programming language supports the following data types.

  • Integer Data type
  • Floating-point Data type
  • Double Data type
  • Character Data type
  • Boolean Data type
  • Void Data type
  • Wide Character Data type

Let's look at each data type in detail.

Integer Data type

An integer is a number without a decimal part. For example, the value 10 is an integer. In c++ integer data type has the following characteristics.
  • Integer data type is represented using the keyword “int”.
  • The integer value is represented using the type specifier “%d” or “%i".
  • The integer data type allows storing any value in the range from -32768 to 32767. However, it may change using type modifier.

Floating-point Data type

A floating-point value is a number with a decimal part. For example, the value 10.65 is a floating-point. In c++ floating-point data type has the following characteristics.
  • Floating-point data type is represented using the keyword “float”.
  • The floating-point value is represented using the type specifier “%f”.
  • The floating-point data type allows storing any value in the range from 2-31 to 231.
  • The floating-point data type does not allows type modifier.
  • The floating-point data type allows upto 6 decimal points.

Double Data type

The double data type is similar to the floating-point data type but it allows a wide range of values. The double data type has the following characteristics.
  • The double data type is represented using the keyword “double”.
  • The double value is represented using the type specifier “%ld”.
  • The double data type allows storing any value in the range from 2-63 to 263.
  • The double data type does not allows type modifier except long.
  • The double data type allows double precision upto 12 decimal points.

Character Data type

A character is a symbol enclosed in a single quotation. Here, the symbol may be anything like an alphabet, a digit, or a special symbol. In C++, the character data type has the following characteristics.
  • The character data type is represented using the keyword “char”.
  • The character value is represented using the type specifier “%c” or "%s".
  • The character data type allows storing any value in the range from -128 to 127.
  • The character data type allows only signed and unsigned type modifiers.

Boolean Data type

The boolean data type contains only two values 0 and 1. Here the value 0 represents false, and 1 represents true. The boolean data type has the following characteristics.
  • The boolean data type is represented using the keyword “bool”.
  • The boolean value is represented using the type specifier “%d”.
  • The boolean data type allows storing only 0 and 1.
  • The boolean data type does not allows type modifier.

Void Data type

The void data type is a value less data type, and it represents nothing. The void data type has the following characteristics.
  • The void data type is represented using the keyword “void”.
  • The void value does not have any type specifier.
  • The void data type does not allows storing any value.
  • The void data type does not allows type modifier.

Wide character Data type

The wide-character data type is similar to character data type, but it takes 2 bytes of memory. It allows a wide range of character values compare to the character data type. This data type has supported by the latest compilers only. The keyword wchar_t is used to represent wide character data type.
The following table describes the characteristics of each data type briefly.
Data type                         Keyword                                Memory size                        
Memory sizeRange
Short Integer                  short int                                     2 bytes
2 bytes-32768 to +32767
Long Integer                   long int or int                            4 bytes

4 bytes2-31 to 231
Signed Integer               int or short int or long int           2 or 4 bytes
2 or 4 bytes-32768 to +32767 or 2-31 to 231
Unsigned Integer          unsigned int                                2 or 4 bytes  

2 or 4 bytes0 to +65535 or 0 to 232-1
Floating-point                float                                            4 bytes

4 bytes+/- 3.4e +/- 38 (~7 digits)
Double                           double or long double                8 bytes
8 bytes+/- 1.7e +/- 308 (~15 digits)
Signed Character            char or signed char                1 bytes
1 bytes-128 to +127
Unsigned Character         unsigned char                        1 bytes

1 bytes0 to +255
Boolean                            bool                                        1 bit
1 bit0 or 1
Void                                 void                                       No memory 

No memoryNo value
Wide Character             wchar_t                                  2 bytes

2 bytes2-15 to 215


Tokens in C++

Tokens is the smallest individual unit in C++ language. In fact, every unit that makes a sentence in C++ is a Token. C++ has six types of Tokens as given below.
  • Keywords
  • Identifiers
  • Constants
  • Strings
  • Special Symbols
  • Operators

  • Variable In c++
  • Variables are used to store values. variable name is the name of memory location where value is stored. It must be alphanumeric, olny underscore is allowed in a variable name. It is composed of letters, digits and only underscore. It must begin with alphabet or underscore. It can not be begin with numeric.

Declaration of Variable

  • Declaration will allocate memory for specified variable with garbage value.

Syntax :

  •   Data-Type Variable-name;  

Examples :

  •   int a;
      float b;
      char c;

Initialization of Variable

  • Initialization means assigning value to declared variable. Every value will overwrite the previous value.

Examples :

  •    a = 10;
       b = 4.5;
       c = 'a';
    Character value must be enclosed with single quotes.
      a = 4.5; 
    if we assign decimal value to integer variable, it will accept only integer portion of value. In the above example variable a will accept 4 only.



C++ Program Structure

The C++ programming language supports both procedure-oriented and object-oriented programming paradigms. So, we can write a C++ program using the POP structure and OOP structure too.

POP structure of C++ program

/*documentation*/
pre-processing statements 
global declaration;
void main()
{
    Local declaration;
    Executable statements;
    .
    .
    .
}
User defined functions 
{
    function body
    .
    .
}

Let's create a C++ program using the POP structure.
Example
/*Program to perform addition of two numbers*/
#include 
int a, b;
void main()
{
    int c;
    void get_data();
    int sum(int, int);
    
    get_data();    
    c = sum(a, b);    
    cout << "Sum = " << endl;
}
void get_data() 
{
    cout << "Enter any two numbers: ";
    cin >> a >> b;
}

int sum(int a, int b)
{
    return a + b;
}

OOP structure of C++ program

/*documentation*/
pre processing statements 
class ClassName
{
    member variable declaration;
    .
    .
    .
    member functions(){
        function body
        .
        .
    }
};
void main ()
{
    ClassName object;
    object.member;
}


Let's create a C++ program using the OOP structure.
Example
/*Program to perform addition of two numbers*/
#include 

class Addition{
    int a, b;
    public:
        get_data(){            
            cout << "Enter any two numbers: ";
            cin >> a >> b;
        }
        int sum(){
            get_data();
            return a + b;
        }
};

void main(){
    Addition obj;
    cout << "Sum = " << sum() << endl;
}


C++ Variables

A variable is named memory location, where the user can store different values of the specified data type. In simple words, a variable is a value holder. Every variable in the program has the following properties.
  • Every variable has a name (user-specified) and an address.
  • Every variable must be created with a data type.
  • Every variable is allocated with a memory based on the data type of it.
  • Every variable has a value.

Creating Variables

Creating a variable is nothing but declaring a variable. In C++, all the variables must be declared before they use in the program. The general syntax for declaring a variable is as follows.
data_type variable_name;

The above syntax is used to declare a variable without initial value. But, C++ allows us to declare a variable with initial value too. And it can be done using the following syntax.
data_type variable_name = value;

In C++, multiple variables of the same data type may be declared using a single statement. In this case, all the variables must be separated with a comma symbol. Let's look at the following declaration statements.
int a, b, c;
float x = 10.5, y;
char p = 'A', q = 'B', r = 'C';

Every declaration statement with multiple variables may contain declaration without initial value or with an initial value or mix-up of both.

Types of Variables

Based on the location of variable declaration, variables are classified into five types. They are as follows.
  • Local Variables
  • Global Variables
  • Formal Variables
  • Member Variables
  • Instance Variables

Local Variables

The variables that are declared inside a function or a block are called local variables. The local variable is visible only inside the function or block in which it is declared. That means the local variables can be accessed by the statements that are inside the function or block in which the variable has declared. Outside the function or block, the local variable can't be accessible.
The local variables are created upon execution control enters into the function or block and destroyed upon exit.
Consider the following example of C++ program.
#include 

using namespace std;

int sum(){
   int a = 10, b = 20;
   //cout<< "Result = " << result; // Error - result can't be accessed
   return a + b;
}

int main()
{
    int result;

    result = sum();
    cout << "Sum = " << result << endl;
    //cout << a << "+" << b << "=" << sum() << endl; // Error -  a & b are not accessible

    return 0;
}

Global Variables

The variables that are declared outside a function are called global variables. The global variable is visible inside all the functions that are defined after its declaration. That means the global variables can be accessed by all the functions that are created after the global variable declaration. The functions that have created before the global variable declaration can't access it.
The local variables are created upon execution starts and destroyed upon exit.
Consider the following example of C++ program.
#include 

using namespace std;

int a = 10; // Global variable

int main()
{
    int b = 20; // Local variable
    void show();
    cout << "Inside the main!!!" << endl;
    cout << "a = " << a << endl;
    show();

    return 0;
}

void show(){
   int a = 30;  // Local variable with same name as global
   cout << "Inside the show!!" << endl;
   cout << "a = " << a << endl;    // refer to local variable
   cout << "a = " << : :a << endl;   // refer to global variable
}

🔔 When both global and local variables has same name, only local variable is refered inside the function. To refer the global variable we need to use : : (scope resolution) operator.

Formal Variables

The variables that are created in the function definition as receivers to the parameter values are called formal variables. The formal variables are also known as formal parameters, and they act as local variables inside the function.
Consider the following example of C++ program.
#include 

using namespace std;

void add(int x, int y){
    cout << "sum = " << x+y << endl;
}

int main()
{
    int a = 10, b = 20; // Local variable
    
    add(a, b);
    
    return 0;
}

In the above example program, the variables x and y are called formal variables.
🔔 The formal variables are allowed only when the function has parameters.

Member Variables

The variables that are created in a class are known as member variables. The member variables are accessible to all the methods of that class. The member variables are also accessible to the methods of other classes using inheritance mechanism.
Consider the following example of C++ program.
#include 

using namespace std;

class Sample{
    public:
        int a, b;
        void setData(){
            cout << "Enter a and b values: ";
            cin >> a >> b;
        }
        void getData(){
            cout << "a = " << a << endl << "b = " << b << endl;
        }
};

int main()
{
    Sample s;

    s.setData();
    s.getData();

    return 0;
}

In the above example program, the variables a and b are called member variables of class Sample.

Instance Variables

The instance variable is a special type of variable of a user-defined data type called class. That means an instance variable is a variable of class type. The instance variables are also known as objects. The instance variables are used to access the class members from outside the class.
Consider the following example of C++ program.
#include 

using namespace std;

class Sample{
    public:
        int a, b;
        void setData(){
            cout << "Enter a and b values: ";
            cin >> a >> b;
        }
        void getData(){
            cout << "a = " << a << endl << "b = " << b << endl;
        }
};

int main()
{
    Sample s;

    s.setData();
    s.getData();

    return 0;
}

In the above example program, the variable s in the main method is called instance variable of class Sample, and it also is known as the object of Sample class.

C++ Expressions

An expression is collection of operators and operands which produce a unique value as result. The expressions are used to perform mathematical and logical operations in a program. In an expression operator is a symbol with pre-defined task and operands are the data values on which the operation is performed.
There are three types of expressions and they are as follows.
  • Infix Expression
  • Prefix Expression
  • Postfix Expression
The most commanly used expression type is infix expression.

Infix Expression

In this type of expression the operator is between the operands.

Prefix Expression

In this type of expression the operator is in-front of the operands.

Postfix Expression

In this type of expression the operator is after the operands.

No comments:

Post a Comment

Recently

All C program

Write a C program to print ‘Hello World’ on screen . #include < stdio.h > #include < conio.h > void  m...