Friday, 23 January 2015

Pointer Is Fun

What Are Pointers?

Pointers are basically the same as any other variable. However, what is different about them is that instead of containing actual data, they contain a pointer to the memory location where information can be found. This is a very important concept. Many programs and ideas rely on pointers as the basis of their design, linked lists for example.

Getting Started

How do I define a pointer? Well, the same as any other variable, except you add an asterisk before its name. So, for example, the following code creates two pointers, both of which point to an integer:
int* pNumberOne;
int* pNumberTwo;
Notice the "p" prefix in front of the two variable names? This is a convention used to indicate that the variable is a pointer. Now, let's make these pointers actually point to something:
pNumberOne = &some_number;
pNumberTwo = &some_other_number;
The & (ampersand) sign should be read as "the address of" and causes the address in memory of a variable to be returned, instead of the variable itself. So, in this example, pNumberOne is set to equal the address of some_number, so pNumberOne now points to some_number.
Now if we want to refer to the address of some_number, we can use pNumberOne. If we want to refer to the value of some_number from pNumberOne, we would have to say *pNumberOne. The * dereferences the pointer and should be read as "the memory location pointed to by," unless in a declaration, as in the line int *pNumber.

What We've Learned So Far: An Example

Phew! That's a lot to take in. I'd recommend that if you don't understand any of those concepts, to give it another read through. Pointers are a complex subject and it can take a while to master them. Here is an example that demonstrates the ideas discussed above. It is written in C, without the C++ extensions.
#include <stdio.h>

void main()
{
    // declare the variables:
    int nNumber;
    int *pPointer;

    // now, give a value to them:
    nNumber = 15;
    pPointer = &nNumber;

    // print out the value of nNumber:
    printf("nNumber is equal to : %d\n", nNumber);

    // now, alter nNumber through pPointer:
    *pPointer = 25;

    // prove that nNumber has changed as a result of the above by 
    // printing its value again:
    printf("nNumber is equal to : %d\n", nNumber);
}

Wednesday, 14 January 2015

Personnel Information System (PR: 03)


Practical No:03


Aim:
/*
Develop an object oriented program in C++ to create a database of the personnel information system containing the following information: Name, Date of Birth, Blood group, Height, Weight, Insurance Policy, number, Contact address, telephone number, driving license no. etc Construct the database with suitable member functions for initializing and destroying the data viz. constructor, default constructor, copy, constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete.*/
 
 Theory:

1)constructor
2) default constructor
3) copy constructor
4) Parametrized constructor 
5) Destructor, 
6)static member functions
7)friend class, 
8)this pointer, 
9)inline code 
10)Dynamic memory allocation operators-new and delete.

 Conclusion  

Program for Reference:

/* Personnel.cpp*/
#include<iostream>
#include<string.h>
#include<iomanip.h>

class db
{
    char nm[20];
      char dob[10];
      char bg[3];
      float wt;
      float ht;
     int ins;
      long no;
      public:
      static int stdno;
      static void count()
      {
            cout<<"\nNo. of objects created: "<<stdno;
      }
    void fin() { cout<<"\nInline Function!";}

      db()
      {
         strcpy(nm,"Name");
            strcpy(dob,"DOB");
            strcpy(bg,"BG");
            wt=ins=ht=no=0;
            ++stdno;
      }
      db (db *ob)
      {
            strcpy(nm,ob->nm);
            strcpy(dob,ob->dob);
            strcpy(bg,ob->bg);
            wt=ob->wt;
            ht=ob->ht;
            ins=ob->ins;
            no=ob->no;
      }

      void getdata()
      {
            cout<<"\n\nEnter: name, dob, blood grp, weight, height, insurance policy no,
        contact no-\n";
            cin>>nm>>dob>>bg>>wt>>ht>>ins>>no;
      }
      friend void display(db d);

      ~db()
      {
            cout<<"\n\n"<<this->nm<<"(Object) is destroyed!";
      }
};


void display(db d)
{
    cout<<"\n"<<setw(10)<<d.nm<<setw(15)<<d.dob<<setw(8)<<d.bg<<setw(5)<<d.wt
    <<setw(5)<<d.ht<<setw(10)<<d.ins<<setw(15)<<d.no;
}

int db::stdno;

void main()
{
       clrscr();
       int n,i;
       db d1,*ptr[5];
       cout<<"\nDefault values:";
       display(d1);
       d1.getdata();
       display(d1);
       db d2(&d1);
       cout<<"\n\nUse of copy constructor :\n";
       display(d2);
       cout<<"\nHow many objects u want to create?:";
       cin>>n;
       for(i=0;i<n;i++)
       {
           ptr[i]=new db();
            ptr[i]->getdata();
       }
       cout<<"\n"<<setw(10)<<"Name"<<setw(15)<<"Date of Birth"<<setw(8)
    <<"Blood grp"<<setw(5)<<"Weight"<<setw(5)<<"Height"<<setw(10)<<"Ins
    no"<<setw(15)<<"Contact no.";
   for(i=0;i<n;i++)
     display(*ptr[i]);
   db::count();
   for(i=0;i<n;i++)
   {
     delete(ptr[i]);
   }
   cout<<"\nObjects deleted!" ;
   getch();

Thursday, 8 January 2015

Library Managamnet (PR:02)

/* Problem Statement:

A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not. If it is not, an appropriate message is displayed. If it is, then the system displays the book details and requests for the number of copies required. If the requested copies book details and requests for the number of copies required. If the requested copies are available, the total cost of the requested copies is displayed; otherwise the message Required copies not in stockis displayed.
Design a system using a class called books with suitable member functions and Constructors. Use new operator in constructors to allocate memory space required. Implement C++ program for the system

*/
 /* This is an Reference Program */

/* Bookshop.cppp*/
#include<iostream>
#include<string.h>
#inlcude<iomanip.h>
Using namespace std
class book
{
    private:
    char *author;
    char title[50];
    int price;
    char publisher[50];
    int stock_position;
    public:
    book(int x)//constructor defined
    {
        author=new char[x];
    }
};//class ends
void book :: getdata()
{
    cout<<"Enter title of book";
    cin>>title;
    cout<<"Enter author of book";
    cin>>author;
    cout<<"Enter price of book";
    cin>>price;
    cout<<"Enter publisher of book";
    cin>>publisher;
    cout<<"Enter number of copies";
    cin>>stock_position;
}
void book :: display()
{
    cout<<setw(20)<<title<<setw(20)<<author<<setw(20)<<Publisher<<setw(20)<<price
    <<setw(10)<<stock position;
}
main()
{
    int size;
    cout<<"How many character you want in author name\n";
    cin>>size;
    book ob[50]=new book(size);
    char bname[50];
    int choice,nbook;
    while(1)
    {
        cout<<"Menu:1.Input Data 2.Display 3.search book 4.Purchase book 5.Exit";
        Cout<<"\n Enter your choice";
        cin>> choice;
        switch(choice)
        {
            case 1:
                Cout<<"How many books data you want to enter";
                Cin>>nbook;
                For(i=0;i<nbook;i++)
                ob[i].getdata();
            break;
            case 2:
                if (nbook<=0)
                cout<<"No data available";
            else
            {
                cout<<setw(20)<<"title"<<setw(20)<<"author"<<setw(20)<<"Publisher"
                <<setw(20)<<"prices"<<setw(10)<<"stock position";
                for(i=0;i<nbook;i++)
                {
                    ob[i].putdata();
                }
            }
            break;
            case 3:
                if (nbook<=0)
                cout<<"No data available";
            else
            {
                cout<<setw(20)<<"title"<<setw(20)<<"author"<<setw(20)<<"Publisher"
                <<setw(20)<<"prices"<<setw(10)<<"stock position";
                for(i=0;i<nbook;i++)
                {
                    cout<<"Enter book name you want to search";
                    cin>>bname;
                    if (strcmp(bname,ob[i].title)==0)
                    {
                        ob[i].putdata();
                    }
                }
            }
            break;
            case 4:
                if (nbook<=0)
                cout<<"No data available";
            else
            {
                for(i=0;i<nbook;i++)
                {
                    cout<<"Enter book name you want to purchase";
                    cin>>bname;
                    if (strcmp(bname,ob[i].title)==0)
                    {
                        cout<<"book details are given below";
                        cout<<setw(20)<<"title"<<setw(20)<<"author"<<setw(20)<<"Publisher"
                        <<setw(20)<<"prices"<<setw(10)<<"stock position";
                        ob[i].putdata();
                        cout<<"How many copies you want to puchase";
                        cin>>copies;
                        If (ob[i].stock_position>=copies)
                        {
                            cout<<"Requested copies are available\n";
                            cout<<"Total cost is:\t"<<copies*ob[i].price;
                        }
                        else
                        {
                            cout<<"Sorry!!!!!Requested copies arenot available\n";
                        }
                    }
                }
                break;
                case 5:
                    exit(0);
            }//while ends
        }//main ends
}