About Me

My photo
Posting here few basic programs. I prefer using C++. Trying to keep it error free. Feel free to point any of them. Thanks for visiting. You can copy paste code directly from here.

Thursday, 22 September 2011

/* Struct vs Class

Program to show there aren't many differences between a structure and a class

COMMENTS are shown in bold and italics*/


#include<iostream.h>
#include<conio.h>



class Demo2                                      //class
{
    public:                                              //public label
        void print()
        {
            cout<<"\n\n For the class\n\n Enter a, b and c : ";
            cin>>a>>b>>c;
            cout<<"\n a= "<<a<<" b = "<<b<<" c= "<<c;
        }
       

    private:
        int a;
        int b;

    protected:
        int c;
};
Demo2 a2;                              // object created for the class



struct Demo1                        
                                       

{
    public:                                             //see this has labels as a class
        void print()                                  // has functions
        {
            cout<<"\n\nFor the structure\nEnter x, y and p : ";
            cin>>x>>y>>p;
            cout<<"\nx= "<<x<<" y = "<<y<<" p= "<<p;
        }
        Demo1();

                             /* even has constructors & destructors although i have shown a constructor only . A constructor is a special function having same name of the class */

    private:
        int x;
        int y;

    protected:
        int p;
};
Demo1 a1;                   

//object of structure defined in the same way as for a class                




Demo1::Demo1()             /*constructor declaration outside structure as can be done for a class*/
{
    x = y = p = 0;
    cout<<"\nThis shows constructor working for struct\n"<<"\n x= "<< x <<" y = "<< y <<" p= "<< p;
}



int main()
{
    a1.print();                                //calling structure object
    a2.print();                                //calling class object         
    getch();
    return 0;
}

Sample Output:





// Stack through array


Program to implement Pop and Push operations in Stack (stack is implemented using Array)

#include<iostream.h>
#include<conio.h>
#include<process.h>                          // to use exit() function

# define size 20                                 //for array size

int stack[size], top=-1, data;

void push()
{
     if(top < size-1)
     {
        cout<<"\n Enter data for push operation... : ";
        cin>>data;
        top+=1;                                   //same as top = top + 1
        stack[top] = data;
     }
     else
     cout<<"\n\tStack full....\n\tTry emptying values from stack...";
}

void pop()
{
    if(top = = -1)
    cout<<"\n\t Stack underflow...\n\t Try pushing elements...";
    else
    {
    data = stack[top];
    cout<<"\n\t Popped data : "<<data;
    top-=1;
    }
}

void display()
{
     for(int i=0; i<= top; ++i)
     cout<<stack[i]<<" --> ";
    
     cout<<"\n\n   Press enter to continue";
     getch();
}

int main()
{

    int choice;

    do
    {
    clrscr();
    cout<<"\n\n\n\t Welcome to Stack menu";
    cout<<"\n  Press 1 to push elements in stack";
    cout<<"\n  Press 2 to pop elements from stack";
    cout<<"\n  Press 3 to exit";
    cout<<"\n    Stack current status...: "<<top+1<<" elements present\n\n";
    cout<<"\n\n  Enter your choice (1-3)  : ";
    cin>>choice;

    switch(choice)
    {
                       case 1:
                            push();
                            display();
                          break;
                         
                       case 2:
                            pop();
                            display();
                          break;
                          
                       case 3:
                             cout<<"\n   Preparing to exit...";
                             cout<<"\n   Press enter...";
                             getch();
                             exit(0);
                          break; 
                         
                       default:
                             cout<<"\n\t Wrong selection!!! Remember (1-3)";
                          break;   
        }
     }while(choice != 3);
     getch();
    
     return 0;

}





Sample Output of the program: