Thursday 24 July 2014

C++ Theroy.& Programme

Difference Between Procedure Oriented Programming (POL) & Object Oriented Programming (OOP): -


Basic Concept of Object Oriented Programming (Features): -

Objects: -
Objects are the basic run time entities. This represents a person, a place, a bank A/C or any item that the program has to handle. They can represent user define data type. Programming problem is analysis term of object and the nature of communication between them. Objects represents space in the memory and have an associate address like one record or structure in �C�. When a program is executed the objects interacts by sending massages to one another each object contains data & code to manipulate the data.



Classes: -
The entire set of data & code of an object can be made a user define data type with the help of a class. Objects are variables of the class. Once the class has been define we can create any number of objects belong to that class. A class is a collection of objects of similar type.

Data Abstraction & Encapsulation: -
The wrapping of data & functions into a single unique class is known as encapsulation. The data is hidden from outside functions, only those functions which are included in the class can access it. This feature is called data hiding or information hiding.
Abstraction refers to the act of representing essential features without including the background details. Classes use the concept of abstraction & are defined as a list of abstract attribute. The attribute are called data members because they hold information & function that operate on this data are called methods as member functions. Classes are known as abstract data type.

Inheritance: -
Inheritance is the process by which objects of one class can have properties of objects of another class. It supports the concept of hierarchical classification. The concept of inheritance provides the idea of reusability this means that we can add additional features to an existing class without modifying it. We can drive one class from the existing class the new class will have combine feature of both the classes.

Polymorphism: -
Polymorphism is Greek term mean the ability to take more than one from.
Ex- For one operator operation can take different form if operator is working on number �+� than it gives sum of numbers & if this operation is working on string than it will give concatenation string. This type of behavior is known as operator overloading.
A single function name can used to handle different number & different types of arguments. Using single function name to perform different type of task is known as function overloading. In Object Oriented programming language like C++ polymorphism achieve by operator overloading & function overloading.

Dynamic Binding: -
Binding refers to the linking of a processor call to the code to be executed response to the call dynamic binding is known as late binding means that the code associated with a given procedure call is not known until the time of the call at run time. This is associated with polymorphism & inheritance.

Message Passing: -
Object oriented program consist of the set of object that communicate with each other object communicate by sending & receiving information in the form of message.

INLINE FUNCTION: -
The object of using function is save memory because each function has its own memory lock having consecutive instruction set traditionally when function is called with some parameters then execution will be followed by that function instruction set. So control is shifted when function is called also when function get executed the return statement will again shift the control to the calling function. If one small function called many times in the program, saves memory but increases the load of execution. Inline functions are design & implement in different way.
An inline function is a function that is expanded in line when it is invoked. That is, the compiler replaces the function call with the corresponding function code. The inline functions are defined as follows:

1.There are some situations where inline expression may not work are :
2.For function returning values, if a loop, a switch, or a go to exists.
3.For functions not returning values, if a return statement exists.
4.If functions contain static variables.
5.If inline function are recursive.
Inline expansion makes a program run faster because the overhead of a function call and return is eliminated. However, it makes the program to take up more memory because the statements that define the inline function are reproduced at each point where the function is called. So, a trade-of becomes necessary. The inline keyword merely sends a request, not a command, to the compiler. The compiler may ignore this request if the function definition is too long or too complicated and compile the function as a normal function.

CONSTRUCTOR: -
A Constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called because it constructs the values of data members of the class. The Constructor can have arguments to pass when it is called but it is not having return value. A constructor that accepts no parameters is called the default constructor. The default constructor for class A is A:: A(). If no such constructor is defined, than the compiler supplies a default constructor. When a constructor is declare for a class, initialization of the class objects becomes mandatory. We can construct two dimensional arrays.
Constructors that can take arguments are called parameterized constructors. The constructor functions can also be defined as inline functions. The parameter of a constructor can be of any type except that of the class to which it belongs.
Ex: -
class A

  Public:  
        A (A); 
};


It is possible to define constructor with default arguments.
Ex: -
class A
{   
Public:
    A (int I=0);
  };


Allocation of memory to object at the time of their construction is known as dynamic construction of object. The memory is allocated with the help of the new operator.
We must pass the initial value as arguments to the constructor function when the object is created. This can be done in two ways.
By calling the constructor explicitly.
Ex: - item A1 = item (0,100);
By calling the constructor implicitly.
Ex: - item A1 (0,100);
Ex: - item A() {       };
It takes no arguments, is used to create objects which are not initialized.

CHARACTERISTICS OF CONSTRUCTOR: -

1.They should be declared in the public section.
2.They are invoked automatically when the object are created.
3.They do not have return types, not even void and therefore, and they cannot return values.
5.They cannot be inherited, though a derived class can call the base class constructor.
6.Like other C++ functions, they can have default arguments.
7.Constructor cannot be virtual.
8.We cannot refer to their addresses.
9.An object with a constructor (or destructor) cannot be used as a member of a union.
10.They make implicit calls to the operators new and delete when memory allocation is required.

COPY CONSTRUCTOR: -
A reference variable has been used as an argument to the copy constructor. We cannot pass the argument by value to a copy constructor. When no copy constructor is defined, the compiler supplies its own copy constructor. A constructor can accept a reference to its own class as a parameter. Thus the statement is valid. In such cases, the constructor is called the copy constructor.
Ex: -
class item
{  
   Public:     
      Item (A&); 
      item (A &x); 
  };
      main ()
{   
     Item X (100); 
    item B(X);  
   item C = X;  
  }

If class is having multiple constructor definition then this concept is called constructor overloading. 

DESTRUCTOR: -
A destructor, as the name implies, is used to destroy the objects that have been created by a constructor. Like a constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde (~) symbol. A destructor 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 not longer accessible. Whenever new operator is used to allocate memory in the constructors, we should use delete to free that memory. A destructor is not called implicitly. It must be declare in public section.

CHARACTERISTICS OF DESTRUCTOR: -
1.They should be declared in the public section.
2.They are invoked automatically when the object are created.
3.They do not have return types, not even void and therefore, and they cannot return values.
4.They cannot be inherited, though a derived class can call the base class constructor.
5.Like other C++ functions, they can have default arguments.
6.Destructor cannot be virtual.
7.We cannot refer to their addresses.
8.An object with a destructor (or constructor) cannot be used as a member of a union.
9.They make implicit calls to the operators new and delete when memory allocation or de-allocation is required.

New Operator: -

C language is uses malloc () and calloc () functions to allocate memory dynamically at run time. Similarly, it uses the function free () to free dynamically allocated memory. We use the dynamic allocation dynamic techniques when it is not known in advance how much of memory space is needed. New is a memory allocation operator. The new operator can be used to create objects of any type. The general form is as under.

Syntax: -Pointer-Variable = NEW Data-Type;

Here pointer-variable is a pointer of type data-type. The new operator allocates sufficient memory to hold a data object of type data-type and return the address of the object. The data-type may be any valid data type. The pointer-variable holds the address of the memory space allocation. Alternatively, we can combine the declaration of pointers and their assignments.

Ex: - int *p = new int;

We can also initialize the memory using the new operator. This is done as under.

Syntax: -Pointer-Variable = New Data-Type (value);

Here value specifies the initial value. New can be used to create a memory space for any data type including user-defined types such as arrays, structures, and classes. This is done as under.

Syntax: -Pointer-Variable = New Data-Type [size];

Here size specifies the number of element in the array. When creating multi-dimensional array with new, all the array sizes must be supplied.

Ex: -     Array_ptr = new int [3] [6] [7];    // legal

Array_ptr = new int [m] [7] [9];   // legal
Array_ptr = new int [] [7] [9];   // illegal

Delete Operator: -
When a data object is no longer needed, it is destroyed to release the memory space for reuse. The general form is as follows.

Syntax: -Delete pointer-variable;

Here the pointer-variable is the pointer that points to a data object created with new. If we want to free a dynamically allocated array, we must use the following form of delete.

Syntax: -Delete [size] pointer-variable;

Here the size specifies the number of elements in the array to be freed. The problem with this form is that the programmer should remember the size of the array. Recent version of C++ does not require the size to be specified.

Dynamic Constructor: -
The constructor can also be used to allocate memory while creating an object. This will enable the system to allocate the right amount of memory each object when the objects are not of the same size, thus resulting in the saving of memory. Allocation of memory to object at the time of their constructor is known as dynamic construction of objects. The memory is allocated with the help of the new operator.

Friend Function: -
C++ introduces two new types of functions, namely, friend function. It is basically introduced to handle some tasks related to class objects.  The private members cannot be accessed from outside the class. That is non-member function cannot have an access to the private data of class. However, there could be a situation where we would like two classes to share a particular function. To make an outside function �friendly� to a class, we have to simply declare this function as a friend of the class.

        The keyword friend should precede the function declaration. The function is defined else where in the program like a normal C++ function. The function definition does not use either the keyword friend or the scope operator (::). The functions that are declared with the keyword friend are known as friend functions. A friend function, although not a member function, has full access rights to the private member of the class. The general form of declaration of friend function is as under.
Syntax: -
Class ABC
{
Public:
Friend void XYZ (void);
}
Here ABC is a class name and XYZ is a name if one function which is a friend function above is the declaration of the friend function in the class.

Characteristic of Friend Function: -
1.It is not in the scope of the class to which it has been declared as friend.
2.Since it is not in the class, it cannot be called using the object of that class.
3.It can be invoked like a normal function without the help of any object.
4.Unlike member functions, it cannot access the member names directly and has to use an object and dot membership operator with each member name (e.g. A. x).
5.It can be declare either in the public or the private part of the class without affecting its meaning.
6.Usually it has the object as the arguments.
7.A friend functions are often used in operator overloading.
8.A friend function will have only one argument for unary operators and two for binary operators.
9.Member function of one class can be friend functions of another class. In such cases, they are defined using the scope resolution operator.
Ex: -
    class X
{      ����
Int fun1 ();       // member function of X
};
Class Y
{      �����
Friend int X :: fun1 ()     // fun1 () of X is friend of Y
};
We can also declare all the member functions of one class as the friend function of another class. In such cases, the class is called a friend class.
Ex: -
         class X
{      ����
Friend class Z  // all the member function of Z are friends to X   };
A friend function can be called by reference. This method can be used to alter the values of the private members of a class. Remember, altering the values of private members is against the basic principles of data hiding. It should be used only when absolutely necessary.

Limitation of Friend Function: -
When we overloading unary operator using friend function than argument to this function is pass by reference because unary operator are normally self-modifying operations for object that is passed. When we overloading binary operator it is not necessary to pass reference but for some operator like �+-�compound assignment �-=� Shorthand operators reference can be pass using friend function. We cannot overload assignment �=�,�[]�,�()� using friend function. If we want to overload this of one assignment we have to use member function.

Advantage of Friend Function: -
If an application have statement like any number + object where first argument is any number of primitive data type & second is object of the class. This operator + function can�t be overloaded by member function because left hand side argument of the operator is not object which can call member function. This type of application requires friend function.

THIS Pointer: -
C++ uses a unique keyword called THIS to represent an object that invokes a member function. THIS is a pointer that points to the objects for which this function was called. This unique pointer is automatically passed to a member function when it is called. The pointer THIS acts as an implicit argument to all the member functions.
We have been implicitly using the pointer THIS when overloading the operator using the member function. When a binary operator is overloaded using a member function, we pass only one argument to the function. The other argument is implicitly passed using the pointer THIS. One important application of the pointer THIS is to return the object it points to. For example return *this; This statement assume importance when we want to compare two or more objects inside a member function and return the invoking object as a result.

Static Data Members: -
A data member of a class can be qualified as static. The properties of a static member variable are similar to that of a C static variable. Static variables are normally used to maintain values common to the entire class. Main points to remember static data members are as follows.
The type and scope of each static member variable must be defined outside the class definition. This necessary because the static data members are stored separately rather than as a part of an object.
Since they are associated with the class it self rather than with any class object, they are also known as class variables.
While defining a static variable, some initial value can also be assigned to the variable.

Characteristic of Static Data Member: -
It is initialized to zero when the first object of its class is created. No other initialization is permitted.
Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
It is visible only within the class but its lifetime is the entire program.
The static variable count is initialized to zero when the objects are created.
The count is incremented whenever the data is read into an object.
Static variable are like non-inline member function as they are declare in a class declaration and defined in the source file.

Static Member Function: -
Like static member variable we can also have static member functions. Properties of static function are a static function can have access to only other static members declared in the same class. A static member function can be called using the class name (instead of its objects). Class-name:: function-name;.

Operator Overloading: -
The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.
C++ also allows us to provide new definitions to some of the built-in operators. That is, we can give several meaning to an operator, depending upon the types of arguments used. This process is known as operator overloading.
Operator functions must be either member functions (non-static) or friend function. A base difference between them is that friend function will have only one argument for unary operator and two for binary operators, while a member function has no argument for unary operator and only one for binary operator. Arguments may be passed either by value or by reference. Operator overloading is one of the many exciting feature of C++ language. It is an important technique that has enhanced the power of extensibility of C++. Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators. We can overload the entire C++ operator except the following.

Class member access operator (. , .*).
Scope resolution operator ( :: ).
Size operator ( sizeof ).
Conditional operator ( ?: ).



Object Oriented Programming



C++ theory  :- j.b.vaghasiya





C++ Theroy.& Programme

DS Programme

/*
Translate Infix to postfix notation using stack.
s= (a*b+d)-(e*f^g^h)^i
--> ab*d+efg^h^*i^-
*/
#include<stdio.h>
#include<conio.h>
void main()
{
   char in[50],stk[50];
   int i,top=-1;
   clrscr();
   printf ("\n \t  Enter a Infix Notation:");
   gets(in);
   stk[++top]='(';
   printf("\n \t Postfix :");
   for(i=0;in[i]!='\0';i++)
   {
   switch(in[i])
   {
     case '(' :  stk[++top]='(';
  break;
     case ')' :   while(stk[top]!='(')
  {
      printf("%c",stk[top--]);
  }
  top--;
  break;
     case '^' :   while(stk[top]=='^')
  {
      printf("%c",stk[top--]);
  }
  stk[++top]=in[i];
  break;
     case '*' :
     case '/' :
while(stk[top]=='^' || stk[top]=='*' || stk[top]=='/')
  {
      printf("%c",stk[top--]);
  }
  stk[++top]=in[i];
  break;
     case '+' :
     case '-' :
  while(stk[top]=='^' || stk[top]=='*' || stk[top]=='/'  || stk[top]=='+' || stk[top]=='-'  )
  {
      printf("%c",stk[top--]);
  }
  stk[++top]=in[i];
  break;
     default :
       printf("%c",in[i]);
break;
    }
  }
  while(stk[top]!='(')
  {
      printf("%c",stk[top--]);
  }
  getch();
}
###########=jbvaghasiya====================================================

/*
Translate Infix to prefix notation using stack.
s= a+b+c
--> ++abc
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char in[50],stk[50],pre[50];
   int i,top=-1,k=0;
   clrscr();
   printf ("\n \t  Enter a Infix Notation:");
   gets(in);
   stk[++top]=')';

   for(i=strlen(in)-1;i>=0;i--)
   {
   switch(in[i])
   {
     case '(' :
while(stk[top]!=')')
  {
   pre[k++]=stk[top--];
  }
  top--;
  break;
     case ')' :   stk[++top]=')';
  break;
     case '^' :
  stk[++top]=in[i];
  break;
     case '*' :
     case '/' :
while(stk[top]=='^')
  {
   pre[k++]=stk[top--];
  }
  stk[++top]=in[i];
  break;
     case '+' :
     case '-' :
  while(stk[top]=='^' || stk[top]=='*' || stk[top]=='/')
  {
     pre[k++]=stk[top--];
  }
  stk[++top]=in[i];
  break;
     default :
pre[k++]=in[i];
break;
    }
  }
  while(stk[top]!=')')
  {
      pre[k++]=stk[top--];
  }
//       strrev(pre);
printf("\n \t Prefix :");
       for(i=strlen(pre)-1;i>=0;i--)
   printf("%c",pre[i]);
  getch();
}

J.b.vaghasiya