Tuesday, 3 February 2015

Function template

Function template

A function template defines a family of functions.

Syntax

template < parameter-list > function-declaration (1)
export template < parameter-list > function-declaration (2)  



Explanation

function-declaration - a function declaration. The function name declared become a template name.
function-declaration-with-placeholders - a function declaration where the type of at least one parameter uses the placeholder auto or a constrained type specifier: the template parameter list will have one invented parameter for each placeholder.
parameter-list - a non-empty comma-separated list of the template parameters, each of which is either non-type parameter, a type parameter, a template parameter, or a parameter pack of any of those.


Example :

void f(auto (auto::*)(auto)); // #1
template<typename T, typename U, typename V> void f(T (U::*)(V)); // same as #1
 
template<int N> void f(Array<auto, N>*); // #2 (assuming Array is a class template)
template<int N, typename T> void f(Array<T, N>*); // same as #2
 
void g1(const C1*, C2&); // #3 (assuming C1 and C2 are concepts
template<C1 T, C2 U> void g1(const T*, U&); // same as #3

C++ program to perform String operations (PR: 04)

Practical No: 04

Aim:
Write a C++ program to perform String operations :
theory :
i.     = Equality
ii.     == String Copy
iii.     + Concatenation
iv.     << To display a string
v.     >> To reverse a string
vi.     Function to determine whether a string is a palindrome

vii.   Operator Overloading

Conclusion:




Code for reference :
#include<iostream>
Using namespace std;
class string
{
  char str[20];
  int len;
  public:
  string() { len=0; }

  void read()
  {
    cout<<"\nEnter the string : ";
    gets(str);
  }

  int  operator ~()
  {
    int i;
    len=0;
    for(i=0;str[i]!='\0';i++)
    {
     len++;
    }
    return(len);
  }

  friend istream &operator >>(istream &in,string &s)
  {
    int i;
    ~s;
    cout<<"\nReverse string: ";
    for(i=s.len-1;i>=0;i--)
    {
      cout<<s.str[i];
    }
  }

  void operator ==(string s)
  {
     read();
     char str2[20];
     for(int i=0;str[i]!='\0';i++)
       str2[i]=str[i];
     str2[i]='\0';
    cout<<"\nCopy : ";
    puts(str2);
  }

  void operator +(string s2)
  {
    ~*this;
    int j=len;
    for(int i=0;s2.str[i]!='\0';i++)
    {
       str[j]=s2.str[i];
       j++;
    }
    str[j]='\0';
    puts(str);
  }

  void operator =(string s2)
  {
    int i,f=0;
    for(i=0;str[i]!='\0';i++)
    {
       if(str[i]==s2.str[i])
       {
     f=1;
       }
      else
     f=0;
     break;
    }
    if(f==1)
     cout<<"\nStings are equal!";
    else
     cout<<"\nStrings are not equal";
  }
  void operator ||(string s2)
  {
    int i,j=0,f=0,count=0;
    cout<<"\nPositions of occurence: ";
    for(i=0;str[i]!='\0';i++)
    {
       if(str[i]==s2.str[j])
       {
    j++;
       }
       else
       {
    j=0;
       }
       if(s2.str[j]=='\0')
       {
     f=1;
     count++;
     cout<<"  "<<(i-j+2);
     j=0;
       }
    }
    cout<<"\nNo of occurence of substring: "<<count;
  }

  void operator !()
  {
    int f=0;
    ~*this;
    for(int i=0;i<len;i++)
    {
      if(str[i]==str[len-i-1])
     f=1;
      else
       {
     f=0; break;
       }
    }
    if(f==1)
      cout<<"\nIt is a pallindrome!";
  }


friend ostream &operator <<(ostream &out,string &s)
  {
     out<<s.str;
     return(out);
  }
};

void main()
{
int c,l;
 char m;
 string s1,s2;
 do
 {
 cout<<"\nMENU:\n1.Length of the string\n2.Copy\n3.Reverse\n4.Concatination\n5.Compare two strings\n6.Find Substring\n7.Pallindrome\nChoice= ";
 cin>>c;
 switch(c)
 {
  case 1: s1.read();
      l=~s1;
      cout<<"\nLength of the string is: "<<l;
      break;
  case 2: s1==s2;
      break;
  case 3: s1.read();
      cin>>s1;
      break;
  case 4: s1.read(); s2.read();
      s1+s2;
      break;
  case 5: s1.read();
      s2.read();
      s1=s2;
      break;
  case 6: s1.read(); s2.read();
      s1||s2;
      break;
  case 7: s1.read();
      !s1;
      break;
  case 8: s1.read();
      cout<<s1;
      break;
  default:cout<<"Invalid";
      break;
  }
  cout<<"\nDo you want to continue?-y/n: ";
  cin>>m;
  }while(m=='y');
}

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
}