Monday, October 10, 2016

new and delete Operators :)

C++ Program to show use of new and delete Operators.

#include <iostream>
using namespace std;
int main()
{
    int i,*p; //pointer variable
    p=&i; //pointer now points to i
    p=new int [3]; //new operator
    for(i=0; i<3; i++)
    {
        cout<<"Enter Value "<<(i+1)<<": ";
        cin>>(*(p+i)); //incrementing pointer address
    }

    for(i=0; i<3; i++)
    {
        cout<<"\nValue: "<<(*(p+i));
        cout<<"\nAddress: "<<(unsigned)p+i;//address of value
    }

    cout<<"\n\tdelete operator:";
    delete []p; //delete operator
    cout<<"\nMemory Released";
    return 0;
}



Saturday, October 8, 2016

Operations on CIRCULAR QUEUE!! :)

/* Before understanding the concept of circular queue,you must visit my previous
program on "OPERATIONS ON QUEUE",which discusses how operations are carried out on linear queue.
This program shows how even after the queue is FULL, insertions can be made after deletion.
This is the advantage of circular queue over linear queue!! */

#include<stdio.h>
#include<conio.h>
#define MAX 2 //defining size of queue
void insert(int);
int del();
void display();
int queue[MAX];
int front=-1;
int rear=-1;
main()
{
      int choice,num;
      while(1) //keep continuing the program until user wants to exit
      {
                    printf("Enter your choice \n");
                    printf("1.INSERTION \n");
                    printf("2.DELETION \n");
                    printf("3.DISPLAY \n");
                    printf("4. EXIT \n");
                    scanf("%d",&choice);
         switch(choice)
                    {
                    case 1: //executes insertion
                    insert(num);
                    break;
                    case 2: //executes deletion
                    num=del();
                    break;
                    case 3: //executes display
                    display();
                    break;
                    case 4: //stop further processing
                    exit(1);
                    break;
                    default:
                    printf("Invalid choice");
                    }
                    }
                    }
                 
                    // Function definition //
                 
                    void insert(int x)
                    {
if((front==0 && rear==MAX-1) || (front==rear+1))/* if either no deletion has occured and we have
reached the end of the queue  OR  after all deletions,we have inserted new elements that have reached
the queue's capacity */
            {
            printf("\n Queue is FULL \n ");
            return;
            }
            else
            {
                 printf("Enter number to be inserted: ");
                    scanf("%d",&x);
                    }

            if (front==-1) //for inserting 1st element we set front=0
            {
                front=0;
                }
                if(rear==MAX-1) //if we have reached the end of the queue
                {
                            rear=0; //we reset rear=0,as it is a circular queue
                            }
                            else
                {
                        rear=rear+1;
                                }
                         queue[rear]=x; //insertion at end of queue
                                }
                                int del()
                                {
                                     int x;

                 if(front==-1)
                 {
                       printf("\n Queue is EMPTY \n ");
                 }
            x=queue[front]; /* since queue is  FIFO and deletion occurs from front end,so we set
                      element at front of the queue as a value to the variable x */
                      if(front==rear) //if there is only 1 element in the queue
            {
                front=-1;//deleting that element
                rear=-1;
                                     }
                      else if(front==MAX-1) /* one last element at the "end of the queue" is left
                      which is soon going to be deleted by making front +1,we further reset the value
                      of front=0 after the last deletion as it is a circular queue and now front is
                      once again ready to delete newly entered elements in the queue */
                      {
                          front=0;
                          }
                          else
                          {
                              front=front+1;
                              }
                              printf("%d has been deleted \n",x);
                              return x;
                              }
                 void display()
                 {
                      if(front==-1)
                      {
                                   printf("Queue is Empty");
                                   return;
                                   }
                     int i;
                     i=front;
                     if(front<=rear)
                     {
                                    while(i<=rear) //setting a condition for printing entire queue
                                    {
                           printf("%d\n",queue[i++]);
                           }
                           }
                      else //touches the concept of circular queue
                      {
                          while(i<=MAX-1) //if we are somewhere in between the queue
                          {
                               printf("%d\n",queue[i++]);
                               } //this will go on till last element
                               i=0; //we reset front =0
                      while(i<=rear)
                               {
                                 printf("%d\n",queue[i++]);
                                 }
                                 }
                                 }                                                
                                                                                                       
                                                         
                       
                 
                       

Friday, October 7, 2016

Display Area(Solution To E Balagurusamy Problem 9.1)

Solution:

#include<iostream>
using namespace std;
class shape
{
protected:
double a,b;
public:
void getdata()
{
cout<<"\nEnter The Parameters \n";
cin>>a>>b;
}
virtual void display()
{

}
};
class triangle:public shape
{
public:
void display()
{
double area;
area=0.5*a*b;
cout<<"\nArea of Triangle is "<<area;
}
};
class rectangle:public shape
{
public:
void display()
{
double area;
area=a*b;
cout<<"\nArea of Rectangle is "<<area;
}
};
main()
{  int choice;
shape *p;
cout<<"\nPress 1 For Triangle\nPress 2 For Rectangle\n ";
cin>>choice;
if(choice==1)
{ triangle t;
p=&t;
}
else if(choice==2)
{
rectangle r;
p=&r;
}
p->getdata();
p->display();
}


Output:


Object Slicing :)

Object Slicing:

When a derived class object is assigned to base class then the base class contents in derived object  are copied to base class leaving behind the derived class specific contents.

Following Program Illustrates This Concept:
#include<iostream>
using namespace std;
class base
{
public:
int i=10,j=70;
void show()
{   cout<<"\nIn Base Class";
cout<<"\nI ="<<i;
cout<<"\nJ ="<<j;
}
};
class derived:public base
{
public:
int k=150;
void show()
{  i=50;
  j=75;
cout<<"\nIn Derived Class";
cout<<"\nI ="<<i;
cout<<"\nJ ="<<j;
cout<<"\nK ="<<k;
}
};
main()
{
base b;
derived d;
b.show();
d.show();
b=d;
b.show();
d.show();
}

Output:


Thursday, October 6, 2016

Virtual Destructor :)

When We use Pointers In Inheritance then the destructor of derived class is not called.
To follow the normal rule we can use Virtual Destructor

Following is a simple program to Illustrate the Above Concept

#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"Base Constructor Called\n";
}
virtual ~A()
{
cout<<"Base Destructor Called\n";
}
};
class B:public A
{
public:
B()
{
cout<<"Derived Constructor Called\n";
}
virtual~B()
{
cout<<"Derived Destructor Called\n";
}
};
main()
{   A *p=new B();
delete(p);
}

Output:













And if we donot use virtual destructor then the above program gives the following output


Virtual Functions :)

As we  Know that the Polymorphism is the feature of OOP,which means we use the same name but multiple forms.
So in case of Run time Polymorphism the Virtual Functions Are used which tell about the binding of function procedure with its call at run time.
Virtual Functions done this by checking the object to which the pointer is pointing rather than the type of pointer.
Following is a Simple program to illustrate the above concept

#include<iostream>
using namespace std;
class Base
{
public:
void display()
{
cout<<"Display base\n";
}
virtual void show()
{
cout<<"Base Virtual\n";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"Display Derived\n"; }
    void show()
{
cout<<"Derived Virtual\n";
}
};
main()
{
Base b;
Derived d;
    Base *p=&b;
p->display();
p->show();
p=&d;
p->display();
p->show();
}

Output:



















So as we can see in the output the Display function of base class is called two times because it is not virtual
On the other hand the show function is called for both derived and base class because it is made virtual.

Wednesday, October 5, 2016

MULTIPATH INHERITANCE AND VIRTUAL CLASSES :)

Multipath Inheritance in C++ implements the use of Virtual Classes.


We make the base class 'virtual' by adding the keyword 'virtual' during declaration of Derived class.

#include <iostream>
using namespace std;
class A
{
public:
    int a1;
    A()
    {
        cout<<"Constructor of Base Class A";
    }
};

class B:public virtual A //virtual public A is also OK
{
public:
    int a2;
    B()
    {
        cout<<"\nConstructor of Base Class B";
    }
};

class C:public virtual A
{
public:
    int a3;
    C()
    {
        cout<<"\nConstructor of Class C";
    }
};

class D:public B,public C
{
public:
    int a4;
    D()
    {
        cout<<"\nConstructor of Derived Class D\n\n";
    }
};
int main()
{
    D obj;
    obj.a1=10;
    obj.a2=20;
    obj.a3=30;
    obj.a4=40;
    cout<<"a="<<obj.a1;
    cout<<"\nb="<<obj.a2;
    cout<<"\nc="<<obj.a3;
    cout<<"\nd="<<obj.a4;
    return 0;
}