Pages

Friday, March 29, 2013

C++ File Handling Program

Write a program that shows the use of read() and write() to handle file I/O involving objects.

#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
class INVENTORY
{
  char name[10];
  int code;
  float cost;
  public:
    void readdata(void);
    void writedata(void);
};
void INVENTORY :: readdata(void)
{
  cout<<"\n Enter name:";cin>>name;
  cout<<"\n Enter code:";cin>>code;
  cout<<"\n Enter cost:";cin>>cost;
}
void INVENTORY :: writedata(void)
{
  cout<<setiosflags(ios::left)
  <<setw(10)<<name
  <<setiosflags(ios::right)
  <<setw(10<<code)
  <<setprecision(2)
  <<setw(10)<<cost<<endl;
}

int main()
{
  INVENTORY item[3];
  fstream file;
  file.open("Stock.dat",ios::in|ios::out);
  cout<<"\n Enter details for three items \n";
  for(int i=0;i<3;i++)
  {
    item[i].readdata();
    file.write((char *) & item[i],sizeof(item[i]));
  }
  file.seekg(0);
  cout<<"\nOutput\n";
  for(i=0;i<3;i++)
  {
    file.readdata((char *) &item[i],sizeof(item[i]));
    item[i].writedata();
  }
  file.close();
  return 0;
}


C++ PROGRAM TO DO ELECTRICITY BILL CALCULATION.

C++ PROGRAM TO READ AND DISPLAY STUDENT DETAILS USING INHERITANCE.

C++ program to overload the unary minus operator using friend function.

Write a program to overload the unary minus operator using friend function.

#include<iostream>
using namespace std;
class space{
  int x;
  int y;
  int z;
  public:
    void getdata(int a,int b,int c);
    void display(void);
    friend void operator-(space &s);
};
void space :: getdata(int a,int b,int c){
  x=a;
  y=b;
  z=c;
}
void space :: display(void){
  cout<<x<<" ";
  cout<<y<<" ";
  cout<<z<<"\n";
}
void operator-( space &s){
  s.x=-s.x;
  s.y=-s.y;
  s.z=-s.z;
}
int main(){
  space s;
  s.getdata(10,-20,30);
  cout<<"S : ";
  s.display();
  -s;
  cout<<"S :";
  s.display();
  return 0;
}

You might also like:

C++ PROGRAM TO DO ELECTRICITY BILL CALCULATION.

C++ PROGRAM TO READ AND DISPLAY STUDENT DETAILS USING INHERITANCE.

OUTPUT

 


Template Function

Write a template function that swaps the values of two arguments passed to it. In main( ), use the function with integers and characters.

#include<iostream.h>
using namespace std;
template <class T>
void swap(T &a,T &b)
{
    T temp=a;
    a=b;
    b=temp;
}
void func(int x,int y,char w,char z)
{
    cout<<"x and y before swap: "<<x<<y<<"\n";
    swap(x,y)
    cout<<"x and y after swap: "<<x<<y<<"\n";
    cout<<"w and z before swap: "<<w<<z<<"\n";
    swap(w,z)
    cout<<"w and z after swap: "<<w<<z<<"\n";

}
int main()
{
    fun(10,20,’s’,’S’);
    return 0;
}



You might also like:-

Saturday, December 8, 2012

C++ PROGRAM - FACTORIAL USING RECURSION.

Write a C++ program which calculates the factorial of a given number using recursion.

#include<conio.h>
#include<iostream.h>
long factorial (long a)
{
    if (a > 1)
        return (a * factorial (a-1));
    else
        return (1);
}
int main ()
{
    long ln;
    clrscr();
    cout << "Enter a number: ";
    cin >> ln;
    cout << ln << "!" << " = " << factorial (ln);
    getch();
    return 0;
}


OUTPUT








You might also like:-

Tuesday, October 30, 2012

PATIENT DATABASE USING INHERITANCE

Develop a program in C++ to create a database of the following items of the derived class.
Name of the patient, sex, age, ward number, bed number, nature of illness, date of admission.
Design a base class consisting of data members : name of the patient, sex and age ; and another base class consisting of the data members : bed number and nature of the illness. The derived class consists of the data member ,date of admission.
Program should carry out the following methods
(i) Add a new entry.
(ii) List the complete record.

#include<conio.h>
#include<iostream.h>
class A
{
  public:
  char name[20];
  char sex[10];
  int age;
  void get_data();
  void disp_data();
};
void A::get_data()
{
  cout<<"enter name:";
  cin>>name;
  cout<<"enter age:";
  cin>>age;
  cout<<"enter sex:";
  cin>>sex;
}
void A::disp_data()
{
  cout<<"name:"<<name<<'\n';
  cout<<"age:"<<age<<'\n';
  cout<<"sex:"<<sex<<'\n';
}
class B
{
  public:
  int bed_number;
  char nature_illness[40];
  void get_data();
  void disp_data();
};
void B::get_data()
{
  cout<<"enter bed number:";
  cin>>bed_number;
  cout<<"enter nature of illness:";
  cin>>nature_illness;
}
void B::disp_data()
{
  cout<<"bed number:"<<bed_number<<'\n';
  cout<<"nature of illness:"<<nature_illness<<'\n';
}
class C:public A,public B
{
  int date_admission;
  public:
  void get_data();
  void disp_data();
  void record();
};
void C::get_data()
{
  A::get_data();
  B::get_data();
  cout<<"Enter Date of Admission:-> ";
  cin>>date_admission;
}
void C::disp_data()
{
  A::disp_data();
  B::disp_data();
  cout<<"Date of Admission :"<<date_admission;
}
void main()
{
  clrscr();
  C c1;
  cout<<endl<<"Enter details to add a new record\n";
  getch();
  c1.get_data();
  cout<<endl<<"Displaying the added record to database\n";
  c1.disp_data();
  getch();
}


You might also like:

C++ PROGRAM TO DO ELECTRICITY BILL CALCULATION.

C++ PROGRAM TO READ AND DISPLAY STUDENT DETAILS USING INHERITANCE.


OUTPUT

Tuesday, September 18, 2012

C++ PROGRAM TO IMPLEMENT BINARY SEARCH TREE.


Write a C++ program to create a class called BIN_TREE ( Binary tree) with member functions to perform inorder, preorder and postorder traversals. Create a BIN_TREE object and demonstrate the traversals.


#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
struct node
{
  int info;
  struct node *left;
  struct node *right;
};
typedef struct node tree ;
tree *root=NULL;
class BIN
{
  int num;
  tree *p,*prev,*temp;
  public:
  void insert();
  void inorder(tree *);
  void postorder(tree *);
  void preorder(tree *);
  void display();
};
void BIN:: insert()
{
  p=new(tree);
  cout<<"\n Enter  number:";
  cin>>num;
  p->info=num;
  p->left=p->right=NULL;
  if(root==NULL)
  {
    root=p;
    return;
  }
  temp=root;
  while(temp!=NULL)
  {
    if(num>=temp->info)
    {
      prev=temp;
      temp=temp->right;
    }
    else
    {
      prev=temp;
      temp=temp->left;
    }
  }
  if(num>=prev->info)
    prev->right=p;
  else
    prev->left=p;
}
void BIN::preorder(tree *temp)
{
  if(temp!=NULL)
  {
    cout<<" "<<temp->info;
    preorder(temp->left);
    preorder(temp->right);
  }
}
void BIN:: inorder(tree *temp)
{
  if(temp!=NULL)
  {
    inorder(temp->left);
    cout<<" "<<temp->info;
    inorder(temp->right);
  }
}
void  BIN::postorder(tree *temp)
{
  if(temp!=NULL)
  {
    postorder(temp->left);
    postorder(temp->right);
    cout<<" "<<temp->info;
  }
}
void BIN:: display()
{
  if(root==NULL)
  {
    cout<<"\n ***EMPTY TREE**** \n";
    return;
  }
  cout<<"\n\n THE PREORDER DISPLAY IS:   ";
  preorder(root);
  cout<<"\n\n THE INORDER DISPLAY IS:   ";
  inorder(root);
  cout<<"\n\n THE POSTORDER DISPLAY IS:   ";
  postorder(root);
}
void main()
{
  BIN o;
  int ch=1;
  int count=0;
  clrscr();
  while(ch)
  {
    cout<<"\n***********MENU***********";
    cout<<"\n1:INSERT-IN-TREE\n2:DISPLAY\n3.QUIT\n";
    cout<<"\nEnter your choice:\n";
    cin>>ch;
    switch(ch)
    {
      case 1:clrscr();
         count++;
         o.insert();
         break;
      case 2:clrscr();
      cout<<"\n\n THE NUMBER OF NODES IN THE BST is "<< count;
      o.display();
      break;
      case 3:exit(0);
    }
  }
  getch();
}


You might also like:


OUTPUT




C++ PROGRAM TO CREATE A CLASS CALLED STRING.

Write a C++ program to create a class called STRING and implement the following operations. Display the results after every operation by overloading the operator.
 
STRING s1 = “HELLO”

STRING s2 = “WORLD”

STIRNG s3 = s1 + s2 ; (Use copy constructor).

 
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
  char name[23];
  public :string()
  {
    name[23]='\0';
  }
  string(char s[])
  {
    strcpy(name,s);
  }
  string(string &s)
  {
    strcpy(name,s.name);
  }
friend string operator +(string s1, string s2);
friend ostream &operator <<(ostream  &out, string &s);
};
ostream &operator <<(ostream &out , string &s)
{
  out <<"\t"<<s.name<<endl;
  return(out);
}
string operator +(string s1, string s2)
{
  string temp(s1);
//strcat(temp.name,"");
  strcat(temp.name, s2.name);
  return(temp);
}
void main()
{
  clrscr();
  string s1("hello ");
  string s2("world");
  string s3=s1+s2;
  cout<<"\nFIRST STRING ="<<s1
      <<"\nSECOND STRING ="<<s2;
  cout<<"\nCONCATENATED THIRD STRING ="<<s3;
  getch();
}


OUTPUT