BCA, B.Tech_info

BCA, B.Tech_info

Easy way to learn

Breaking

Saturday, August 18, 2018

Polymorphism in C++

August 18, 2018 0
Polymorphism in C++
Definition:
      Polymorphism is another OOP concept. Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors at different instances. The instances depend upon the type of data used in the operation.

The word polymorphism means having many forms. In simple word, we can define polymorphism as the ability of a message to be displayed in more than one form. Real life example of polymorphism, a person at a same time can have same characteristic. 

Polymorphism is mainly divided into two types:
  • Compile time polymorphism 
  • Runtime polymorphism 

Compile time polymorphism

        This type of polymorphism is achieved by function overloading or operator overloading.
Types of Compile time polymorphism -
  1. Function overloading
  2. Operator overloading

1) Function overloading

           When there are multiple function with same name but different parameters then those functions are said to be overloaded. 
Overloading in C++ refers to the use of the same thing for different purposes. This means that we can use the same function name to create functions that perform a variety of different tasks. This is also known as polymorphism. Using the concepts of function overloading, we can design a family of functions with one function name but with different argument list. 

// Declarations                                              

int div(int x, int y);                                                  //prototype 1
int mul(int x, int y, int z);                                        //prototype 2
float sub(float a, float b);                                        //prototype 3

// Function calls

cout << div(3,4);                                                     //uses prototype 1
cout << mul(2, 3, 4);                                               //uses prototype 2
cout << sub(10.2, 12.5);                                          //uses prototype 3

The function would perform different operation depending upon the argument list in the function call. A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique.



Example program



#include<iostream.h>
using namespace std;

int vol(int);
float vol(float, int);

int main()
{
   cout << vol(5) <<"\n";
   cout << vol(2.5, 8) <<"\n";
   return 0;
}
int vol(int s)
{
   return(s*s*s);
}
float vol(float r, int h);
{
   return(3.14*r*r*h);
}

OUTPUT:

125

157.26



2) Operator overloading:

The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.  C++ provide option to overload operators.
For example, we can make the operator ('+') for string to concatenate two strings. We know that, this is the additional operator whose task is to add two operands. So a single operator '+' when placed between two operands, adds them and when placed between strings operand, concatenate them. Basically, operator overloading is used because it makes program more readable as the operator which are used for basic data types can also be used for user-defined data types


C++ program to overload unary minus (-) operator:


#include <iostream>
#include <conio.h>
using namespace std;

class example
{
    int a,b;
    public:
        void input()
        {
            cout<<"Enter a and b: ";
            cin>>a>>b;
        }
        void operator -()   //operator function as a member function
        {
            a=-a;
            b=-b;
        }
        void display()
        {
            cout<<"a="<<a<<endl<<"b="<<b<<endl;
        }
};

int main()
{
    example e;
    e.input();
    cout<<"Before overloading unary minus operator is "<<endl;
    e.display();
    -e;
    cout<<"After overloading unary minus operator is"<<endl;
    e.display();
    getch();
    return 0;
}

OUTPUT:

Enter a and b: 4 -5

Before overloading unary minus operator is
a=4
b=-5
After overloading unary minus operator is
a=-4

b=5


Runtime polymorphism:



This type of polymorphism is achieved by function overriding.

Function overriding on the other hand occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.


Thursday, June 7, 2018

what is procedure oriented programming and object oriented programming?

June 07, 2018 0
what is procedure oriented programming and object oriented programming?

Procedure-Oriented Programming:

                                                                  High level languages such as COBOL, FORTRAN and C, is commonly known as procedure oriented programming(POP). IN the procedure oriented programming , the problem is viewed as a sequence of things. Procedure-oriented programming basically consists of writing a list of instructions for the computer to follow. we normally use a flow chart  to organize these action and represent the flow of control from one action to another. While we concentrate on the development of functions, very little attention is given to the data that are being used by various functions.


Relationship between data and function in POP
Relationship between data and function in POP

Characteristics of Procedure Oriented Programming are:

  • Procedure oriented programming is a function oriented approach.
  • Large functions are divided into small programs known as function.
  • POP follows top-down approach.
  • Data move freely around the system from one function to another function.
  • New data and function cannot be added easily.
  • A good programming approach for scientific application development.

Object-Oriented Programming:

Object oriented approach is to remove some of the flows encountered in the procedure oriented programming. Object oriented programming(OOP), treats data as a critical element in the program and does not allow it to flow freely around the system. It ties data more closely to the functions that operate on it to flow freely around the system. It allows decomposition of a problem into a number of entities called Objects. The data of an object can be accessed only by the functions associated with the object.
relationship of data and functions in OOP
Relationship between data and functions in OOP

Characteristics of Object Oriented Programming:

  • Object oriented is a data oriented approach.
  • Programs are divided into what are known as Objects.
  • OOP follows bottom- up approach.
  • Data is hidden and cannot be accessed by the external functions.
  • New data can be added easily.
  • A good programming approach for real time software design.









Wednesday, June 6, 2018

Basic concepts/characteristics of object oriented programming

June 06, 2018 0
Basic concepts/characteristics of object oriented programming

OOP Characteristics:

  • Objects
  • Classes
  • Data Abstraction 
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Dynamic Binding
  •  Message Passing
concepts of OOP

Objects:

Objects are the basic run-time entities in an object-oriented programming. They may represents a person, a place, a bank account, a table of data or any item that the program has to handle. They may also represents user-defined data such as vector, time and lists. When the program is executed, the object interact by sending message to one another.  

Classes:

Objects contain data, and code to manipulate that data. The entire set of data and code of an object can be made a user-defined data type with the help of class. In fact, objects are variable of the type class. Once a class has been defined, we can create a number of objects belonging to that class. A class is  a collection of objects of similar type. 
Ex. Fruit mango;

Data Abstraction:

Abstractions refer to the act of representing essential features without including background details or explanation. They are commonly known as Abstraction Data Type(ADT).



Encapsulation:

The wrapping up of data and functions into single unit is known as encapsulation. Data encapsulation is a striking feature of a class. The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it. These functions provide the interface between the object's data and the program. 

Inheritance:

Inheritance is the process by which objects of one class acquire the properties of object of another class. The class whose members are inherited is called the Base class and the class that inherits those members is called  Derived class.  It supports class of hierarchical classification.

     The concept of inheritance provides the ideas of reusability. This means we can add essential features to an exciting class without modifying it.

Polymorphism:

Polymorphism is another OOP concept. Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors at different instances. The instances depend upon the type of data used in the operation.  

     Real life example of polymorphism, a person at same time can have different characteristics. 

Dynamic Binding:

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means the code associated with,  given procedure call is not known until the time of the call at the run-time. It is associated with polymorphism and inheritance.

Message Passing:

An object program consist of a set of objects communicate with each other. Object communicate with each other by sending and receiving information much the same way as people messages to one another.

Tuesday, June 5, 2018

What is Data type in C++

June 05, 2018 0
 What is Data type in C++
Definition

Data type means of identifying the type of data and associated operations of handling it. C++ provide various data type is represented differently within the computer's memory.You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.

Types of data type uses in c++
Types of data type uses in C++

User defined data type:

There are some data type that are defined by user, are known as user defined data type. There are four types of use defined data type. 

Structure:

A structure is a collection of different type of data element. It attempts to bring a user-defined types as close as possible to the built-in data type and also provides a facility to hide the data which is one of the main feature of object oriented programming.

      

 In C++ structure can have both variable and function as members. It can also declare some of its members as 'private' so that they cannot be accessed directly by the external functions. Structure names are stand alone and can be used like any other type names. The keyword "struct" can be omitted in declaration of structure variable. By default, members of structure are public.


e.g.,   struct stud

{

int marks;

float members;

char name[15];

};


 Union:

A union is same like that of structure but the little difference  is that the members of union shares common memory space. Union is declared with the help of "union" keyword.

e.g.,   union emp 
{
int emp;
char name[20];
float salary;
};

Class:

A class is a collection of data and functions in a single unit. The functions are called members of class and the data is called data members of a class. A class represents a group of similar objects. Class is a way to bind data and functions in a single unit. Class are usually grouped under two sections, one is named, private and other is named as public. 

The class members that have been declared as private can be accessed only from within the class. On the other hand, public members can be accessed by the outside the class also. The data hiding ( using private declaration ) is the key feature of object oriented programming. By default, members of class are private.

syntax:

class class_name
{
     private:
         variable declaration;
         function declaration;
     public:
         variable declaration;
         function declaration;
};

Enumeration:


Defining own data type and specify what values a variable of this type can take is called enumeration. An enumerated data type can be declared using 'enum' keyword. They providing way for attaching names to numbers, thereby increasing comprehensibility of the code.

Built-in data type:

Basic data type that are not composed of other data type. There are three types of built-in data type.

Integral:

There are two types of integral data type.

Integer data type:

Integer data type refers to that data which are integer such as 1,2,3,0,-1,-2,-3 etc. They have no fractional part. Integer is declared with the help of "int" data type.

e.g., 

      int sum;
      int x, y;

Character data type:

An identifier declared as char becomes a character variable. It is denoted by enclosing the character in single quotes. 
e.g.,   
       char x, y;
       

Void data type:

The data type void actually refers to an object that does not have a value of any type. void data type indicate the function that does not return its value. This is an empty data type which does not return value when it is called by calling function.
e.g.,
  void main()

Floating type:

Floating type consist two types of data.

Float data type:

A variable declared as float becomes a floating point variable and can hold only floating point numbers. A number having function. A number having fractional part is a floating point number. It contain size of 4 bytes.

e.g.,
   float average;
   float x, y;

Double data type:

The data type double is also used for handling floating point numbers. It is treated as distint data type because it occupies twice as much memory as type float and store floating point numbers with much large range. It contains 8 bytes of data.
e.g.,
   double average;
   double x, y;

Derived data type:

The data type are derived from the built-in  data type are known as derived data type. There are four types of derived data type.

Array:

Array is a collection of homogeneous type of data elements. The data type of element may be int, float, or char etc. Array may be one dimensional, two dimensional and multi-dimensional.

e.g.,
   int count[10];
   float matrix[2][3];
   char name[10];

Function

Function is a small program to do some specific task. The function can be called any number of times. Function is also called self contained program. Function eliminates the repeated code in the program. Once a program code is written there is no need to write the code again and again.
For e.g.,
   int square(int a)
   {
   int b;
   b=a*a;
   return(b);
   }

Pointer

A pointer is a variable that contain the address of another variable. The address refers to the location of another variable in the memory. It simply uses the identifier whenever it needs to refer to the variable.

e.g.,
   int i=10;
   int *j;
   j=&i;

References:

A reference is a alternative name for a variable. That is, a reference is an alias for a variable in a program.
Once a reference is created, it cannot be later made to reference another object; it cannot be reset. This is often done with pointers. References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing. A reference must be initialized when declared. There is no such restriction with pointers
Due to the above limitations, references in C++ cannot be used for implementing data structures like Linked List, Tree, etc. 

classes and objects

June 05, 2018 0
classes and objects

Class definition:

A class is a method in which the data and its related functions bind together. It allows the data and function to be hidden from external use, if necessary. The class declaration describe the type and scope of its member. The class declaration is almost similar to the structure declaration.

syntax:

class class_name
{
    private:
        variable declarations;
        function declarations;
    public:
       variable declarations;
       function declarations;
};

The body of class is enclosed within braces and terminated by a semicolon. The class body contain two sections, first one is private, and another one is public. These keywords are followed by a colon. The class body contain variable declaration and function declaration. These functions and variable are known as class members.
The class members that have been declared as private cannot be accessed by external function only can be accessed within class whereas public members can be accessed by outside the class also. External function only can access the public section of the class. 


Objects:

In C++, the class variables are known as objects. The declaration of an object is similar to that of a variable of any basic type.  The necessary memory space is allocated to an object at this stage.
Object can be created after the class definition.

syntax:

class record
{
       int a;
      int  b;
   public:
      int z;
};
   ….
   ….
record  i;
i.a=0;
i.b=10;
…….
…….

Here, objects a, b and c of type record. Objects can also be created when a class is defined by placing their names immediately after the closing brace.

syntax:

class record
{
      ….
      ….
} a, b, c;


Example program:

#include<iostream>

using namespace std;

class print
{
   private:
      int number;
   public:
      void input_value(void);
      void output_value(void);
};
void print::input_value(void)
{
   cout<<"\n Enter an integer \n";
   cin>> number;
}
void print::output_value(void)
{
    cout<<"Number entered is:"<<number;
}
int main()
{
print x;
x.input_value();
x.output_value();
return 0;
}

Output:

Enter an integer
12
Number entered is: 12

Monday, June 4, 2018

Constructor, copy constructor and Destructor

June 04, 2018 0
Constructor, copy constructor and Destructor

Constructor:

Constructor is a special member function whose task is to initialize the objects of its class. Constructor is invoked automatically  when objects are created.  It is known as special because it has same name as the class name to which it belong. It is called constructor because it constructs the values of the data members of the class. 

The following are the properties of the constructor functions:
1.  Constructor should be declared in the public section.
2.  They do not have return type even void.
3.  They are invoked automatically whenever an object of associated class is created.
4.  They can not have default arguments.
5.  No. one can refer to their addresses.
6.  It can not be virtual.

Example program:
#include<iostream>
#include<conio.h>

using namespace std;

class simple
{
   private:
      int a, b;
   public:
      simple();                                                                                //constructor declared
      void print()
       {
           cout<<"A= "<<a<<"\n";
           cout<<"B=  "<<b<<"\n";
       }
};
simple::simple()                                                                       //constructor defined
{
    a=10;
    b=12;
}

int main()
{
    clrscr();
    simple obj;                                                                                     //constructor called
    obj.print();
    getch();
    return 0;
}

Output:

A= 10
B= 12


Copy Constructor:

The copy constructor  is a constructor which creates an object by initializing it with an  another  object of the same class, which has been created previously. It invokes when a new object is initialized with exciting one.  It is an overloaded constructor. A copy constructor takes a reference to an object of the same class as itself as an argument. when no copy constructor defined, the C++ compiler supplies its own copy constructor.

syntax:

class_name(const class_name &obj)
{
       // program statement
}

Example program:
#include<iostream>

using namespace std;

class point
{
    private:
       int m, n;
   public:
      point(int a, int b)
      {
         m=a;
         n=b;
      }
      point(const point &p)                                                                   //copy constructor
      {
         m=p.m;
         n=p.n;
      }
      int getA()
      {
         return m;
      }
         int getB()
      {
        return n;
      }
};
int main()
{
  point obj1(15, 20);                                                                        //simple constructor is  called
  point obj2 = obj1;                                                                         //copy constructor is called
  cout<<"Object value 1a: "<<obj1.getA()<<", Object value 1b: "<<obj1.getB();
  cout<<"\n Object value 2a: "<<obj2.getA()<<", Object value 2b: "<<obj2.getB();
  return 0;
}



Output:

Object value 1a: 15, Object value 1b: 20
Object value 2a: 15, Object value 2b: 20



Destructor:

Destructor is used to destroy the object that have been created by object. Destructor is a member function  whose name is same as the class name like a constructor. But a destuctor is preceded by tilde(~) sign.

Suntax:

~code() { }

The following properties of destructor:
1.  A destructor invoked implicitly by the compiler upon exit from the program.
2.  It is used to clean up the storage that is no longer accessible.
3.  It never takes any argument nor return any value.

It  is a good practice to declare destructor in a program since it releases memory space for future use. Objects are destroyed in the reverse order of creation.

Example program:
#include<iostream>
using namespace std;
int count=0;
class display
{
   public:
       display()
       {
         count++
         cout <<"\n Objects created "<<count;
       }
       ~display()
      {
         cout <<"\n Objects destroyed "<<count;
         count--;
      }
};
int main()
{
    cout<<"\n\nMAIN\n";
    display A1, A2;
     {
           cout << "\n\n BLOCK\n";
           display A3;
     }
     cout<<"\n\nRE-ENTER MAIN\n";
     return 0;
}

Output:

MAIN

Object created 1
Object created 2

BLOCK

Object created 3
Object destroyed 3

RE-ENTER MAIN

Object destroyed 2
Object destroyed 1

Polymorphism in C++