Monday, August 12, 2019

Streams in c++ programming language

:Managing Console Input Output Operations in C++ :– Every program takes some data as input and generates processed data as output from the familiar input process output cycle. Like this way c++ programming language provide this type facility. We have already use these operator these are the cin  (<<) and cout (<< )operator. C++ support rich set of input and output operators and function since these function use the advanced features of c++ as like classes derived classes and virtual function.
C++ uses the concept of stream and stream classes to implementation i/o  operation with the console and disk files.
 Streams in c++ programming language -: The i/o system design to work with a wide variety of device including terminals disks and tape drives. Although each device is very different the i/o system supply and interface to the programmer that is independent to the actual device being accessed. this process called the stream .
A stream is sequence of bytes. It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent. This source stream that provide data to the program is called the input stream and the destination stream that receive output from the program is called the output stream.
In other words we can say that the a program extract the bytes from an input stream and inserts bytes into an output stream.
The data in the input stream can come from the keyword or any other storage devices. The data in the output stream can go to screen or any other storage devices .
A stream acts as an interface between the program and the input / output  device. So a c++ program handles data independent of the device used. C++ several pre define stream some are already using as like
cin >> 
cout <<
C++ Streams classes -: The c++ i/o system contains hierarchical of classes that are used to define various stream to deal with both the console and disk files. these classes are called the stream classes.

Polymorphism in c++ programming language


Polymorphism in c++ programming language-: Polymorphism in c++-: Polymorphism is one of the crucial features of OOP. It simply means ‘one name, multiple form’. Polymorphism is a Greek word it means ” ability to take more than one ” An operation may exhibit difference behavior in different instance. The behavior depends upon the types of data used in the operation. For example adding two number the operation will generate a sum. if the operands are string then the operation would produce a third string by concatenation. the process making an operator to exhibit difference behaviors in different instance is known as operator overloading.
Types of polymorphism in c++ -: In the c++ programming language the polymorphism has two parts
  1. Compile time polymorphism 
  2. Run time Polymorphism



Compile time polymorphism -: The overloaded member function are selected for invoking by matching arguments both type and number. this information is known to the compile at the compile time.
compiler is able to select the appropriate compile is able to select the appropriate function for a particular call at the compile time itself. This is called early binding or static binding or static linking. Also known as compile time polymorphism.
For example function overloading
#include <iostream>

#include <stdc++.h> 

using namespace std; 
class netnic 
{ 
 public: 
 

 void func(int x) 
 { 
  cout << "value of x is " << x << endl; 
 } 
 
 
 { 
  cout << "value of x is " << x << endl; 
 } 
 
 
 void func(int x, int y) 
 { 
  cout << "value of x and y is " << x << ", " << y << endl; 
 } 
}; 

int main() { 
 
 netnic vik1; 
 
 
 vik1.func(6); 
 
 
 vik1.func(14.124); 
 
 
 vik1.func(70,45); 
 return 0; 
} 


Output of this program
value of x is 6
value of x is 14.124
value of x and y is 70, 45

Run time Polymorphism -: It is known what object are under consideration the appropriate version of the function is invoked since the function is linked with a particular class much later after the compilation, this process is termed as late binding. It is also known as dynamic binding  because this section of the appropriate function is done dynamically at run time
#include <iostream> 

#include <stdc++.h>

using namespace std;

class netnic 
{
public:
virtual void print ()
{ 
    cout<< "print netnic class" <<endl; 
}

void show ()

{
    cout<< "show netnic class" <<endl; }
};

class derived:public netnic
{
public:
void print ()  
{ 
      cout<< "print derived class" <<endl;
 }

void show ()
{ 
     cout<< "show derived class" <<endl; }
};

//main function
int main()
{
   netnic *xptr;
   derived d;
   xptr = &d;
   xptr->print();
   xptr->show();

return 0;
}
Output of this program
Print derived class
Show netnic class
Dynamic binding is a powerful feathers of c++ programming language. This requires to object.

Pointers in c++ programming language

Pointers in c++ programming language

Pointers in c++-:  Pointer is one of the key aspects of c++ language similar to that of  c programming. Pointer offers a unique approach to handle data in c and c++ language.
We know that a pointer is a derived data type that refers to another data variable by storing the variable memory address rather than data. a pointer variable define where to get the value of a specific data variable instead of defining actual data .
Like the c programming language variable can also refer another pointer in c++. However it often point to a data variable . Pointer also provide an alternative approach to access other data object.
Defining a pointer in c++ -: Like any other programming language before we use the pointer in c++ program it is necessary to defining. We can declare  a pointer variable similar to the variable in c++. Like c language variable . The declaration is based on the data type of the variable it point to the declaration of a pointer variable takes like this
data-type * pointer-variable;
In  this example the pointer variable is the name of the pointer  and data type refer to the one of the valid c++ data type such as int,char,float and so many more. The data type is followed by an asterisk (*) which distinguishes a pointer variable from other variable to the computer.
As we know a pointer variable can point to any type of data available in c++. It is necessary to understand that a pointer is able to point to only one data type at the specific time. Let us declare a pointer variable which points to an integer variable as like
int *ptr ;
here ptr is a pointer variable and point to an integer data type. the pointer variable ptr should contain memory location of any integer variable. in the same manner we can declare pointer variable for other data type also.
Initializing  pointer in c++ -: Like other programming language a variable must be initializing before using it in a c++ program. We can initializing a  variable as like
int *ptr, a;   //declaration

ptr = &a;     //initializing
in this example the pointer variable ptr contain the address of the variable a .
For better understanding we take a example of pointer
#include <iostream>

#include <conio.h>

void main()

{

  int a, *ptr1, **ptr2;

  clrscr();

  ptr1= &a;

  ptr2=&ptr1;

  cout << " the address of a :" << ptr1 << "\n";

   cout << " the address of ptr1 :" << ptr2 ;

   cout << "\n\n";

   cout << "after increment the address value :\n\n"

   ptr1+=2;

   cout << " the address of a :" << ptr1 <<"\n";

   ptr2+=2;

  cout << " the address of ptr1 :" << ptr2 <<"\n";
}
   


Output of this program
The address of a : 0x8fb6ff4

The address of ptr1 : 0x8fb6ff2

After increment the address valu :

The address of a : 0x8fb6ff8

The address of a :0x8fb6ff6

The pointer which are not initializing in a program called Null pointer.
Manipulation of pointer in c++ programming -: We can manipulate with the indirection operator as like “*” which is also known ad dereference operator. We can indirectly access the data variable content. For like this way
"pointer_variable"
We know that the dereference a pointer allows us to get the content of the memory location that the pointer points to after assigning address of the variable to a pointer we may want to change the content of the variable. Using the dereference operator   we can change the content of memory location.

Pointer Expression and pointer arithmetic in c++ -:  C++ allows pointer to perform the following arithmetic operator
  • A pointer can be increment(++) and decrements (–)
  • One pointer can be subtracted from another
  • Any integer can be added to or subtracted from a pointer.

int a[5]; 

int *aptr; 

aptr=&a[0];


Inheritance in C++ programming language

Inheritance in C++ programming language

Inheritance in C++ -: Inheritance is the process by which object one class acquire the properties of object of another class. Inheritance support the concept of hierarchical classification. For example the bird ‘robin’ is the part of the class ” flying bird” which is again a part of the class “bird”. The principle behind this sort of division is divided class shares common characteristic with the class from which is derived as illustrated.
Reuse ability is the another important function of object oriented programming system. C++ also support the concept of reuse ability . The c++  classes can be reuse in several way.  Once a class has been written and tested . it can be used by other programmers to suite there  requirement. This is basically done by creating a new classes and reusing the properties of the existing one . This mechanism of deriving a new class from an old one is called inheritance.
In this process the old class referred the base class  and new one is called the derived class or subclass.
c++ inheritance

Types of Inheritance in c++ -: In the c++ programming language support many type inheritance these are the
  1. Single inheritance
  2. multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

Single Inheritance in c++ -:  For understanding a single inheritance we take a example in this a base class B and a derived class D. The class B contain a private data member one public data member and three public member function. The class D contain one private data and two public member function.
Single Inheritance in C++  public class example:
#include <iostream>

Using namespace std;

class B

{

    int a;

public:


    int b;

    void get_ab();


    int get_a(void);


    void show_a(void);

};


class D : class B         //public derivation

{


   int c;

public:

   void mul(void);

   void display(void);

};

//--------------------------------------------

void B:: get_ab(void)
{
  a=5;
  b=10;
}

int B:: get_a()
{
  return a;
}
void B :: show_a()
{

  cout << "a = <<a <<"\n";

}
void D :: mul()
{

   c=b * get_a();

}

void D :: display()

{

   cout << "a =" << get_a() << "\n"; 
    cout << "b =" << b << "\n";
    cout << "c =" << c << "\n";

}

//-----------------------------------

int main()
{
 D d;
 d.get_ab();
 d.mul();
 d.show_();
 d.display();

 d.b=20;
 d.mul();
 d.display();
 
return 0;
}
output of this program
a=5
a=5
b=10
c=50

a=5
b=20
c=100


Multilevel inheritance in c++ : – It is not uncommon that a class is derived from another derived class. Multilevel inheritance represents a type of inheritance when a Derived class is a base class for another class
For example  we take a example of  family base class is a grandfather (A) and intermediate base class is a father(B) and derived class is a child (C). this type inheritance called the multilevel inheritance.


Example of multilevel inheritance 
#Include <iostream>

using namespace std;

class student

{ 
   protected:
  
     int rollnumebr;

  public:

     void getnumber(int);
    
     void putnumber(void);

};

void student :: getnumber(int a)

{

      rollnumber = a;

}

void student :: putnumber()

{

   cout <<"roll number: " << roll number <<"\n";
  
}

class test : public student    // first level derivation

{

 protected:

  float sub1;
  float sub2;

public:

     void getmarks (float, float);
     void putmarks (void);
};


void test :: getmarks(float x,float y)
{
  sub1= x;
  sub2=y;
}

void test :: putmarks()

{
  cout << "marks in SUB1=" << sub1 <<"\n";
  cout << "marks in SUB2=" << sub2 <<"\n";
}

class result : public test
{
 float total ;
public:
 void display(void);
};

void result :: display(void)
{
 total = sub1 + sub2 ;
 putnumber();
 putmarks();
cout << " total = " << total <<"\n" ;
}

int main ()
{
   result student1;
   student1 getnumber(78);
   student1 getmarks(75.0,85.5);
   student1 dispay();

 return();
}


The output of this program 
Roll number 78

marks in sub1=75

marks in sub2= 85.4

total = 160.4


Destructors In C++ programming language

Destructors In C++ programming language

Destructors In C++ -: C++ also provide another member function called the destructors that destroyed object when they are no longer required.
It is the name implies . It is used to destroy the object that have been created by a constructors. Like the  constructors is a member function whose name is the same as the class name but is preceded by a tilde. For example the Destructors for the class integer can be defined as like
    -Integer (){}
A destructors never takes any argument nor does it return any value.It will be invoked implicitly by the compiler upon exit from the program to clean up storage that is no longer accessible. It is a good practice to declare destructors in a program since it release memory space for future use.
When New us used to allocated memory in the constructors we should use delete to free that memory. For example
Implementation of Destructors program –
#include<iostream>

using namespace std;

int count =0;

class netnic

{  

  public :
    
    netnic()

  {   

    count ++;

    cout << "\n no of object created " << count;

  }

  -netnic()

  {
      count --;

     cout << "\n no of object destroyed" << count;

  }

};

int main()

{

 cout << "\n\n enter main \n;

 netnic B1, B2, B3, B4;
  
    {
       
     cout << "\n\n Enter block1 \n";
   
     netnic B5;

    } 

   {

      cout << "\n\n Enter block2 \n";

      netnic B6;

     } 

 cout << "\n\n reenter main \n";

 return 0;

}
The output of this program
enter main

no. of object created 1

no. of object created 2

no. of object created 3

no. of object created 4

enter block1

no. of object created 5

no. of object destroyed 5

enter block2

no. of object created 5

no. of object destroyed 5

reenter main

no. of object destroyed 4
no. of object destroyed 3
no. of object destroyed 2
no. of object destroyed 1




Then we can say destruction is the opposite to the constructors 

function in c++ programming

Function in c++ programming

Introduction of function  -: We know that function play an important role in C program development. Dividing a program into function is one of the major principle of top down structured programming. Other advantage of function is that it is possible to reduce the size of a program by calling and using them at different place in the program.
Recall that we have used a syntax similar to the following in developing c programming…
void show();  /* function declaration */

main()
{

..........
show                /* function call*/
.......

}

void show ()            /* function definition */

{ 
 .......
 .......              /* function body */
 .......
}

when the function is called control is transferred to the first statement  in the function body. The other statement in the function body are then execute and control return the main program  when the closing braces is encountered. C++ us no exception. functions continue to be the building blocks of c++ programs.
In the c++ programming has added many new features to function to make them more reliable and flexible.

TYPES OF FUNCTION IN C++ PROGRAMMING

  • The main function 
  • Function prototyping 
  • Call by reference
  • Return by reference 
  • Inline function 
  • Default arguments
  • Const arguments 
  • Friend and virtual function

The Main Function -:  C does not specify any return type for the main function which is the starting point for the execution program.  Its looks like..
main()

{
     // main program statement

}
In c++ the main() return a value of type int to the operating system.
Function Prototyping -: It is one of the major improvement added in c++ programming. It’s describing the function interface to the compiler by giving details such as the number and types arguments and the type of return value. With function prototyping a template is always used when declaring and defining function. when a function is called the compiler used the template to ensure that proper arguments are passed and the return value is treated correctly.
Function prototyping is a declaration statement in the calling program and is of the following like …
type function-name( argument-list)
the arguments list contains the types and name of arguments that must be passed to the function. For example
float volume(int x,float y,float z)

Call by Reference-: C++ permit us to pass parameters to the function by reference. When we pass arguments by reference the formal arguments in the called function become aliases to the actual arguments in the calling function. This means that when the function is working with its own arguments it is actually working on the original data like this …
void swap(int &a, int &b)
{
      int t=a;
      a=b;
      b=t;
}
If the p and q are two integer variable then the function call
swap (p,q)
will exchange the value of p and q using there alias a and b.
void swap(int *a, int *b)
{
    int t;
    t=*a;
    *a=*b;
    *b=t;
}

Return by reference -:  A function can also return a reference. for like
int & max (int &a, int &b)
{
  if (a>b)

       return a;

  else
 
       return b;
}
In the return type of max() is int &. the function return reference to a and b . Then a function call such as max (c,d)will yield a reference to either c or d on depending their value.this means that function call can appear on the left hand side of an assignment statement.

Operator In C++ programming Language

Operator In C++ programming Language-: C++ also  support a rich set of built in operator we have already used several of them such as equal to add(+)Minus(-)  multiplication(*) and division(/) operator is a symbol that tells the computer to perform certain mathematics or logical manipulation. operators are used in program to manipulate data and variables. they usually form a part of mathematical or logical expression.
ANSCI C operators can be classified into in number of category  these are here:
  1. arithmetic operators-
  2. relation operators
  3.  logical operators
  4. assignment operators
  5. increment and decrements operators
  6. condition operators
  7. Bitwise operators
  8. . Special operators
All the C programming language operator are use in c++ also . Others new operator are here
: :             Scope Resolution Operator
:  : *          pointer to member declarator
->*            Point to member operator
.*               Point to member operator
Delete     memory release operator
End1       line feed operator
New         Memory allocation operator
Setw         Field with operator


Scope Resolution  Operators in C++ -: We know that the C++ has a block structure language. Block and constant can be used in constructing program. We know that the same variable name can be used to have different meaning in different block.The scope of the variable extends from the point of its declaration til of the end of the block containing the declaration. A variable declare inside a block is said to be local to that block. For example
...........
...........
 {
  int x=10;
  ........
  ........
}
........
........
{
   int x=1;
  .......
   .......
}
For this example here we see the two value are for X it can not accessed from with the inner block. For resolving the problem c++ introduce a new operator : : scope resolution operator.
example of scope resolution operator with a program
# include <iostream> 

using namespace std;

int m=10;   //global m

{
     int m=20;

       {

           int k=m;

           int m=30;
 
          cout <<"we are in inner block \n";

          cout <<"k= " << k <<"\n";

           cout <<"m= " << m <<"\n";

          cout <<" :: m= " << ::m <<"/n";
        }
  
 cout <<"/n we are in outer block \n";

 cout <<"m= "<< m << "\n";

 cout <<":: m=" << :: m <<"\n";

 return 0;

}




The output of this program.
We are inner block

k=20;

m=30

:: m=10

we are in outer block

m=20

::m=10

In the above program the variable m is declared at three place namely outside the main () function , inside the main () function and the inside the inner block.
Member dereferencing operator in c++ -:  C++ provide a set of three pointer to member operator. these are the
:  : *          pointer to member declarator—-:To declare a pointer to a member of class.
->*            Point to member operator—-:To access a member using object name and a pointer to that member.
.*               Point to member operator—: To access a member using a pointer to the object and a pointer to a member.
Memory management operator -: c use malloc() and callock() function  to allocate memory dynamically at run time. In the c++ language two unary operator New and delete that perform the task of allocating and freeing the memory in better and easier way. Since these operators manipulated memory on the free store so  these called free store operators.
An object can be created by new and the destroyed by delete .
pointer variable = new datatype;  // for creating a object
for a example
int * p = new int(25);

float * q= new float (8.2);


Delete pointer example
delete pointer variable ;  // for release memory or delete 

Manipulators operators in c++-: These operators that are used to format the data display. The most commonly used manipulators are end1 and setw. for example
.....
.....

cout <<"m= " << m << end1;

     <<"n =" << n << end1;

.....
.....

Type cast operators in c++-:  This is permits explicit conversion of variable or expression using the type cast operator.  For example
type name (expression)
for example
average = sum/(float)i;     // c notation 

average =sum/float(i);      // c++ notation
The function call notation usually leads to simplest expression. c++ adds the cast operators.