Friday 26 August 2016

questions from IndiaBix

questions on OOP concepts

Questions

Develop a Supermarket Billing System using C++.

Develop a Supermarket Billing System using C++. The key features of this application are listed below :  Bill Report : It shows the bill report of all the items added in supermarket billing system.  Add, Remove or Edit items: With this feature one can add, remove and modify item details. In add items, one can add information or details such as item no., item name, manufacturing date, price, quantity, tax percent, and many more.  Show item details: This feature allows users to see the items and the corresponding details given for the item while adding the item. Use file to store the data.


#include<iostream>
#include<windows.h>
#include<conio.h>
#include<fstream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
using namespace std;
//global variable declaration
int k=7,r=0,flag=0;
COORD coord = {0, 0};
void gotoxy(int x, int y)
{
 COORD coord;
 coord.X = x;
 coord.Y = y;
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
struct date
{
int mm,dd,yy;
};

ofstream fout;
ifstream fin;

class item
{
int itemno;
char name[25];
date d;
public:
void add()
{
cout<<"\n Item No: ";
cin>>itemno;
cout<<"\n\n\t Name of the item: ";
cin>>name;
//gets(name);
cout<<"Manufacturing Date(dd-mm-yy): ";
cin>>d.mm>>d.dd>>d.yy;
}
void show()
{
cout<<"Item No: ";
cout<<itemno;
cout<<"Name of the item: ";
cout<<name;
cout<<"Date : ";
cout<<d.mm<<"-"<<d.dd<<"-"<<d.yy;
}

void report()
{
gotoxy(3,k);
cout<<itemno;
gotoxy(13,k);
puts(name);
}
int retno()
{
    return(itemno);
}
};

class amount:public item
{
float price,qty,tax,gross,dis,netamt;
public:
void add();
void show();
void report();
void calculate();
void pay();
float retnetamt()
{
    return(netamt);
}
}amt;

void amount::add()
{
item::add();
cout<<"Price: ";
cin>>price;
cout<<"Quantity: ";
cin>>qty;
cout<<"Tax percent: ";
cin>>tax;
cout<<"Discount percent: ";
cin>>dis;
calculate();
fout.write((char *)&amt,sizeof(amt));
fout.close();
}
void amount::calculate()
{
gross=price+(price*(tax/100));
netamt=qty*(gross-(gross*(dis/100)));
}
void amount::show()
{
fin.open("itemstore.dat",ios::binary);
fin.read((char*)&amt,sizeof(amt));
item::show();
cout<<"Net amount: ";
cout<<netamt;
fin.close();
}

void amount::report()
{
item::report();
gotoxy(23,k);
cout<<price;
gotoxy(33,k);
cout<<qty;
gotoxy(44,k);
cout<<tax;
gotoxy(52,k);
cout<<dis;
gotoxy(64,k);
cout<<netamt;
k=k+1;

if(k==50)
{
gotoxy(25,50);
cout<<"PRESS ANY KEY TO CONTINUE...";
getch();
k=7;
system("cls");
gotoxy(30,3);
cout<<" ITEM DETAILS ";
gotoxy(3,5);
cout<<"NUMBER";
gotoxy(13,5);
cout<<"NAME";
gotoxy(23,5);
cout<<"PRICE";
gotoxy(33,5);
cout<<"QUANTITY";
gotoxy(44,5);
cout<<"TAX";
gotoxy(52,5);
cout<<"DEDUCTION";
gotoxy(64,5);
cout<<"NET AMOUNT";
}
}

void amount::pay()
{
show();
cout<<"\n\n\n\t\t*********************************************";
cout<<"\n\t\t                 DETAILS                  ";
cout<<"\n\t\t*********************************************";
cout<<"\n\n\t\tPRICE                     :"<<price;
cout<<"\n\n\t\tQUANTITY                  :"<<qty;
cout<<"\n\t\tTAX PERCENTAGE              :"<<tax;
cout<<"\n\t\tDISCOUNT PERCENTAGE         :"<<dis;
cout<<"\n\n\n\t\tNET AMOUNT              :Rs."<<netamt;
cout<<"\n\t\t*********************************************";
}

int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout<<setprecision(2);
fstream tmp("temp.dat",ios::binary|ios::out);
menu:
system("cls");
gotoxy(25,2);
cout<<"Super Market Billing ";
gotoxy(25,3);
cout<<"===========================nn";
cout<<"\n\t\t1.Bill Reportnn";
cout<<"\t\t2.Add/Remove/Edit Itemnn";
cout<<"\t\t3.Show Item Detailsnn";
cout<<"\t\t4.Exitnn";
cout<<"\t\tPlease Enter Required Option: ";
int ch,ff;
float gtotal;
cin>>ch;
switch(ch)
{
case 1:
ss:
system("cls");
gotoxy(25,2);
cout<<"Bill Details";
gotoxy(25,3);
cout<<"================\n\n";
cout<<"\n\t\t1.All Itemsnn";
cout<<"\t\t2.Back to Main menu\n\n";
cout<<"\t\tPlease Enter Required Option: ";
int cho;
cin>>cho;
if(cho==1)
{
system("cls");
gotoxy(30,3);
cout<<" BILL DETAILS ";
gotoxy(3,5);
cout<<"ITEM NO";
gotoxy(13,5);
cout<<"NAME";
gotoxy(23,5);
cout<<"PRICE";
gotoxy(33,5);
cout<<"QUANTITY";
gotoxy(44,5);
cout<<"TAX %";
gotoxy(52,5);
cout<<"DISCOUNT %";
gotoxy(64,5);
cout<<"NET AMOUNT";
fin.open("itemstore.dat",ios::binary);
if(!fin)
{
cout<<"\n\nFile Not Found...";
goto menu;
}
fin.seekg(0);
gtotal=0;
while(!fin.eof())
{
fin.read((char*)&amt,sizeof(amt));
if(!fin.eof())
{
amt.report();
gtotal+=amt.retnetamt();
ff=0;
}
if(ff!=0)
gtotal=0;
}
gotoxy(17,k);
cout<<"\n\n\n\t\t\tGrand Total="<<gtotal;
getch();
fin.close();
}
if(cho==2)
{
    goto menu;
}
goto ss;
case 2:
db:
system("cls");
gotoxy(25,2);
cout<<"Bill Editor";
gotoxy(25,3);
cout<<"=================\n\n";
cout<<"\n\t\t1.Add Item Details\n\n";
cout<<"\t\t2.Edit Item Details\n\n";
cout<<"\t\t3.Delete Item Details\n\n";
cout<<"\t\t4.Back to Main Menu ";
int apc;
cin>>apc;
switch(apc)
{
case 1:fout.open("itemstore.dat",ios::binary|ios::app);
amt.add();
cout<<"\n\t\tItem Added Successfully!";
getch();
goto db;

case 2:
int ino;
flag=0;
cout<<"\n\n\tEnter Item Number to be Edited :";
cin>>ino;
fin.open("itemstore.dat",ios::binary);
fout.open("itemstore.dat",ios::binary|ios::app);
if(!fin)
{
cout<<"\n\nFile Not Found...";
goto menu;
}
fin.seekg(0);
r=0;
while(!fin.eof())
{
    fin.read((char*)&amt,sizeof(amt));
if(!fin.eof())
{
    int x=amt.item::retno();
if(x==ino)
{
flag=1;
fout.seekp(r*sizeof(amt));
system("cls");
cout<<"\n\t\tCurrent Details are\n";
amt.show();
cout<<"\n\n\t\tEnter New Details\n";
amt.add();
cout<<"\n\t\tItem Details editted";
}
}r++;
}
if(flag==0)
{
cout<<"\n\t\tItem No does not exist...Please Retry!";
getch();
goto db;
}
fin.close();
getch();
goto db;

case 3:flag=0;
cout<<"\n\n\tEnter Item Number to be deleted :";
cin>>ino;
fin.open("itemstore.dat",ios::binary);
if(!fin)
{
cout<<"\n\nFile Not Found...";
goto menu;
}
//fstream tmp("temp.dat",ios::binary|ios::out);
fin.seekg(0);
while(fin.read((char*)&amt, sizeof(amt)))
{
int x=amt.item::retno();
if(x!=ino)
tmp.write((char*)&amt,sizeof(amt));
else
{
flag=1;
}
}
fin.close();
tmp.close();
fout.open("itemstore.dat",ios::trunc|ios::binary);
fout.seekp(0);
tmp.open("temp.dat",ios::binary|ios::in);
if(!tmp)
{
cout<<"Error in File";
goto db;
}
while(tmp.read((char*)&amt,sizeof(amt)))
fout.write((char*)&amt,sizeof(amt));
tmp.close();
fout.close();
if(flag==1)
cout<<"\n\t\tItem Succesfully Deleted";
else if (flag==0)
cout<<"\n\t\tItem does not Exist! Please Retry";
getch();
goto db;
case 4:
goto menu;
default: cout<<"\n\n\t\tWrong Choice!!! Retry";
getch();
goto db;
}
case 3:system("cls");
flag=0;
int ino;
cout<<"\n\n\t\tEnter Item Number :";
cin>>ino;
fin.open("itemstore.dat",ios::binary);
if(!fin)
{
cout<<"\n\nFile Not Found...\nProgram Terminated!";
goto menu;
}
fin.seekg(0);
while(fin.read((char*)&amt,sizeof(amt)))
{
int x=amt.item::retno();
if(x==ino)
{
amt.pay();
flag=1;
break;
}
}
if(flag==0)
cout<<"\n\t\tItem does not exist....Please Retry!";
getch();
fin.close();
goto menu;
case 4:
system("cls");
gotoxy(20,20);
cout<<"ARE YOU SURE, YOU WANT TO EXIT (Y/N)?";
char yn;
cin>>yn;
if((yn=='Y')||(yn=='y'))
{
gotoxy(12,20);
system("cls");
cout<<"************************** THANKS **************************************";
getch();
exit(0);
}
else if((yn=='N')||(yn=='n'))
goto menu;
else
{
goto menu;
}
default:
cout<<"\n\n\t\tWrong Choice....Please Retry!";
getch();
goto menu;
}
return 0;
}

Write C++ Program with base class convert declares two variables, val1 and val2, which hold the initial and converted values, respectively. It also defines the functions getinit( ) and getconv( ), which return the initial value and the converted value. These elements of convert are fixed and applicable to all derived classes that will inherit convert. However, the function that will actually perform the conversion, compute( ), is a pure virtual function that must be defined by the classes derived from convert. The specific nature of compute( ) will be determined by what type of conversion is taking place.

/*
Prof.Parchure S.V.
*/
#include <iostream>
using namespace std;
class convert
{
    protected:
double val1;
double val2;
public: convert(double i)
{
    val1 = i;
}
double getconv()
{
    return val2;
}
double getinit()
{
    return val1;
}
virtual void compute() = 0;
};

class l_to_g : public convert
{
public:
l_to_g(double i) : convert(i)
{
}
void compute()
{
    val2 = val1 / 3.7854;
}
};
// Fahrenheit to Celsius
class f_to_c : public convert
{
public:
f_to_c(double i) : convert(i)
{
}
void compute()
{
val2 = (val1-32) / 1.8;
}
};

int main()
{
convert *p;
l_to_g lgob(4);
f_to_c fcob(70);

 p = &lgob;
cout<< p->getinit() << " liters is ";
p->compute();
cout<< p->getconv()<< " gallons\n";
p = &fcob;
cout<< p->getinit() << " in Fahrenheit is ";
p->compute();
cout<< p->getconv()<< " Celsius\n";
return 0;
}

Thursday 25 August 2016

Create employee bio-data using following classes i) Personal record ii))Professional record iii) Academic record Assume appropriate data members and member function to accept required data & print bio-data. Create bio-data using multiple inheritance using C++.

#include<iostream>
using namespace std;
class personal
{
protected:
char name[50];
char address[50];
char birthdate[50];
char gender;
public:
void get_personal();
};
class professional
{
protected:
int noofyearsexp;
char orgname[50];
char projname[50];
char projdetails[50];
public:
void get_professional();
};

class academic
{
protected:
int year;
int marks;
int percentage;
char Class[50];
public:
void get_academic();
};
class biodata: public personal,public academic,public professional
{
public:void display();
};
void personal::get_personal()
{
cout<<"Enter name::";
cin>>name;
cout<<"Enter Address::";
cin>>address;
cout<<"Enter Birthdate(dd/mm/yyyy)::";
cin>>birthdate;
cout<<"Enter gender(M/F)::";
cin>>gender;
}
void professional::get_professional()
{
cout<<"Enter number of years of exp::";
cin>>noofyearsexp;
cout<<"Enter organization name::";
cin>>orgname;
cout<<"Enter project name::";
cin>>projname;
cout<<"Enter project Details::";
cin>>projdetails;
}
void academic::get_academic()
{
cout<<"Enter academic year::";
cin>>year;
cout<<"Enter total marks::";
cin>>marks;
cout<<"Enter percentage::";
cin>>percentage;
cout<<"Enter class::";
cin>>Class;
}
void biodata::display()
{

     cout<<"---------------------Employee Biodata--------------"<<endl;
cout<<"-----------------------------------------------------"<<endl;
cout<<"____________________Personal Details__________________________"<<endl;
cout<<"Name::"<<name<<endl;
cout<<"address::"<<address<<endl;
cout<<"birthdate::"<<birthdate<<endl;
cout<<"Gender::"<<gender<<endl;
cout<<"--------------------------------------------------"<<endl;
cout<<"________________Academic Details________________________"<<endl;
cout<<"Academic Year "<<"marks "<<"percentage "<<"class "<<endl;
cout<<year<<"\t\t0"<<marks<<"\t"<<percentage<<"\t"<<Class<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"_______________Professional Details____________________"<<endl;
cout<<"\nOrganization Name::"<<orgname;
cout<<"\nYears of Experince::"<<noofyearsexp;
cout<<"\nProject Done::"<<projname;
cout<<"\nProject Details::"<<projdetails;
}
int main()
{
biodata b;
b.get_personal();
b.get_academic();
b.get_professional();
b.display();
}

Wednesday 24 August 2016

Create a class Rational Number (fractions) with the following capabilities: a) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators. b) Overload the addition, subtraction, multiplication and division operators for this class. c) Overload the relational and equality operators for this class.

#include<iostream>
using namespace std;
class a
{
    int i,n,d,sn,sd,cond;
public:
    void getdata()
    {
        cout<<"Enter the numerator"<<endl;
        cin>>n;
        cout<<"Enter the denominator"<<endl;
        cin>>d;
    }
    void operator+(a a1)
    {
        if(a1.d!=d)
        {
        sn=(a1.n*d)+(n*a1.d);
        sd=(a1.d)*(d);
        }
        else
        {
            sn=a1.n+n;
            sd=a1.d;
        }
        if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
    }
    void sum()
    {

        for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
            }
        }

        cout<<"Sum is "<<sn<<"/"<<sd<<endl;
    }

    a operator-(a a1)
    {
        a a3;
         if(a1.d!=d)
        {
        a3.sn=(a1.n*d)-(n*a1.d);
        a3.sd=(a1.d)*(d);
        }
        else
        {
            a3.sn=a1.n-n;
            a3.sd=a1.d;
        }
        return a3;


    }
    void sub()
    {
        if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
         for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
          }
        }
        cout<<"Subtraction is "<<sn<<"/"<<sd<<endl;
    }
    a operator /(a a1)
    {
        a a4;
        a4.sn=a1.n*d;
        a4.sd=a1.d*n;
        return a4;
    }
    void div()
    {
         if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
        else if(sn==sd)
        {
            sn=1;
            sd=1;
        }
         for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
            }

    }

    cout<<"Division is "<<sn<<"/"<<sd<<endl;
    }

    a operator*(a a1)
    {
        a a5;
        a5.sn=a1.n*n;
        a5.sd=a1.d*d;
        return a5;

    }

    void mul()
    {
        if(sn>sd)
        {
            cond=sd;
        }
        else if(sd>sn)
        {
            cond=sn;
        }
         for(i=2;i<=cond;i++)
        {

            if(sn%i==0 && sd%i==0)
            {
                sn=sn/i;
                sd=sd/i;
          }
        }
        cout<<"Multiplication - "<<sn<<"/"<<sd<<endl;
    }

};
int main()
{
    a a1,a2,a3,a4,a5,a6;
    a1.getdata();
    a2.getdata();

    a2+(a1);
    a2.sum();

    a3=a2-(a1);
    a3.sub();

    a4=a2/(a1);
    a4.div();

    a5=a2*(a1);
    a5.mul();

    return 0;
}

Create a class template to represent a generic vector. Include following member functions:  To create the vector.  To modify the value of a given element  To multiply by a scalar value  To display the vector in the form (10,20,30,…)

#include<iostream>
#include<vector>
using namespace std;
void display(vector<int> &v)
{
    for(int i=0;i<v.size();i++)
    {
        cout<<v[i]<<" ";
    }
    cout<<"\n";
}

int main()
{
    vector<int> v;
    cout<<"\n initial size="<<v.size()<<" \n ";

    int x;
    cout<<"\n enter values ";
    for(int i=0;i<5;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    cout<<"\n\n size after inserting 5 values  "<<v.size();

    cout<<"\n\n current contents ";
    display(v);

    v.push_back(2);

    cout<<"\n\n size after push_back()= "<<v.size()<<"\n";
    cout<<"\n\n now the contents of vector after push_back()";
    display(v);

    cout<<"\n\n iterator created and inserted element at 3rd position ";
    vector<int>::iterator itr1=v.begin();
    itr1=itr1+2;
    v.insert(itr1,1,6);

    cout<<"\n\n contents of vector after insrting ";
    display(v);
    cout<<"\n\n removing elements 3rd and 4th position";
    //v.erase(v.begin()+2,v.begin()+6);
    v.erase(v.begin()+2,v.begin()+4);
    cout<<"\n\n contents after deletion ";
    display(v);
    cout<<"\n\n";
    return 0;

   }


Develop an object oriented program in C++ to create a database of student information system containing the following information: Name, Roll number, Class, division, Date of Birth, Blood group, Contact address, telephone number, driving licence 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.

#include <iostream>
#include<string.h>
#include<iomanip>

using namespace std;
class db
{
int roll;
char name[20];
char Class[10];
char Div[10];
char dob[10];
  char bg[3],contact[10];
  char phone[10],license[12];

  public:
  static int stdno;
  static void count()
  {
    cout<<"\nNo. of objects created: "<<stdno;
  }
void fin(){cout<<"\nInline Function!";}

  db()
  {
   roll=0;
   strcpy(name,"Sachin");
   strcpy(Class,"I");
   strcpy(Div,"A");
    strcpy(dob,"11/11/1111");
    strcpy(bg,"A");
    strcpy(contact,"city");
    strcpy(phone,"9000000000");
    strcpy(license,"A0101010");
    ++stdno;
  }
  db(db *ob)
  {
            strcpy(name,ob->name);
    strcpy(dob,ob->dob);
    strcpy(Class,ob->Class);
   strcpy(Div,ob->Div);
    strcpy(bg,ob->bg);
    strcpy(contact,ob->contact);
    strcpy(phone,ob->phone);
    strcpy(license,ob->license);
    ++stdno;
  }
void getdata()
  {
     cout<<"\n\nEnter:name,roll,Class,Div,Dob,bg,contact,phone,license \n\n\n";
     cin>>name>>roll>>Class>>Div>>dob>>bg>>contact>>phone>>license;
  }
  friend void display(db d);
  ~db()
  {
    cout<<"\n\n"<<this->name<<"(Object) is destroyed!";
  }
};

void display(db d)
{
cout<<"\n"<<setw(12)<<d.name<<setw(5)<<d.roll<<setw(4)<<d.Class<<setw(3)<<d.Div<<setw(12)<<d.dob<<setw(4)<<d.bg<<setw(12)<<d.contact<<" "<<setw(12)<<d.phone<<" "<<setw(12)<<" "<<d.license;
}
int db::stdno;

int main()
{
    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(12)<<"name"<<setw(5)<<"roll"<<setw(4)<<"Class"<<setw(4)<<"Div"<<setw(12)<<"dob"<<setw(4)<<"bg"<<setw(12)<<"contact"<<setw(12)<<"phone"<<setw(12)<<"license";
   for(i=0;i<n;i++)
     display(*ptr[i]);
   db::count();
   for(i=0;i<n;i++)
   {
     delete(ptr[i]);
   }
   cout<<"\nObjects deleted!" ;
   return 0;
}

Write a C++ program create a calculator for an arithmetic operator (+, -, *, /). The program should take two operands from user and performs the operation on those two operands depending upon the operator entered by user. Use a switch statement to select the operation. Finally, display the result.

#include <iostream>
using namespace std;
int main()
{
    char o,ch;
    float num1,num2;
    do
    {
    cout << "Enter operator either + or - or * or /: ";
    cin >> o;
    cout << "Enter two operands: ";
    cin >> num1 >> num2;
    switch(o)
    {
        case '+':
            cout << num1+num2;
            break;
        case '-':
            cout << num1-num2;
            break;
        case '*':
            cout << num1*num2;
            break;
        case '/':
            cout << num1/num2;
            break;
        default:
            /* If operator is other than +, -, * or /, error message is shown */
            cout << "Error! operator is not correct";
            break;
    }
    cout<<"\n\n do you want to Continue?";
    cin>>ch;
    }while(ch!='n' && ch!='N');
    return 0;
}

Write a function template selection Sort. Write a program that inputs, sorts and outputs an int array and a float array.

#include<iostream>
using namespace std;
template<class T>
void selection(T a[], int n)
{
int i, j;
for (j = 0; j < n-1; j++)
{
    int iMin = j;
    for ( i = j+1; i < n; i++)
{
     if (a[i] < a[iMin])
     {
              iMin = i;
          }
    }
    if(iMin != j) {
        swap(a[j], a[iMin]);
    }
}
}
int main()
{
int a[6]={1,2,6,9,4,7};
char b[4]={'s','b','d','e'};
float c[6]={10.2,5.0,6.3,5.2,1.5,1.56};
selection(a,6);
cout<<"\nSorted Order Integers: ";
for(int i=0;i<6;i++)
cout<<"\n"<<a[i]<<"\t";
selection(b,4);
cout<<"\nSorted Order Characters: ";
for(int j=0;j<4;j++)
    cout<<"\n"<<b[j]<<"\t";
cout<<"\nSorted Order float: ";
selection(c,6);
for(int j=0;j<6;j++)
cout<<"\n"<<c[j]<<"\t";
return 0;
}

Using standard template library (STL) list container implement following member functions of list class: empty, insert, merge, reverse, sort, Unique, using iterator

#include<iostream>
#include<list>
#include<cstdlib>
using namespace std;
void display(list <int> &lst)
{
list<int> :: iterator p;
for(p=lst.begin();p!=lst.end();++p)
cout<<*p<<",";
cout<<"\n\n";
}
int main()
{
    list<int> list1;
list<int> list2(5);
for(int i=0;i<3;i++)
list1.push_back(rand()/100);
list<int>::iterator p;
    for(p=list2.begin();p!=list2.end();++p)
*p=rand()/100;
cout<<"list1\n";
display(list1);

cout<<"list2\n";
display(list2);

list1.push_front(100);
list1.push_back(200);

list2.pop_front();

cout<<" Modified list1\n";
display(list1);

cout<<"Modified list2\n";
display(list2);

list<int>listA,listB;
listA=list1;
listB=list2;

list1.merge(list2);

cout<<"merged unsorted list";
display(list1);


listA.sort();
listB.sort();
listA.merge(listB);
cout<<"Merged sorted list\n";
display(listA);


listA.reverse();
cout<<"Reversed sorted list\n";
display(listA);
return 0;
}

Crete User defined exception to check the following conditions and throw the exception if the criterion does not met.

Crete User defined exception to check the following conditions and throw the exception if the criterion does not met. a. User has age between 18 and 55 b. User stays has income between Rs. 50,000 – Rs. 1,00,000 per month c. User stays in Pune/ Mumbai/ Bangalore / Chennai d. User has 4-wheeler Accept age, Income, City, Vehicle from the user and check for the conditions mentioned above. If any of the condition not met then throw the exception.

/*
#include<iostream>
#include<string.h>
using namespace std;
class exception_handling
{
    int age,income;
    char city[20];
    char Isvehicle;
    public:
    friend istream &operator >>(istream &din,exception_handling &e)
    {
        cout<<"\n\n Enter age of person::";
        cin>>e.age;
        cout<<"\n\n Enter Income::";
        cin>>e.income;
        cout<<"\n\n Enter City ::";
        cin>>e.city;
        cout<<"\n is Vehicle(Y/N)";
        cin>>e.Isvehicle;
        return(din);
    }
    friend ostream &operator <<(ostream &dout,exception_handling &e)
    {
        try
        {
            if(e.age<18 || e.age>55)
            throw(1);
            if(e.income<50000 || e.income>100000)
            throw(2);
            if((e.city!="PUNE" || e.city!="Pune" || e.city!="pune") && (e.city!="MUMBAI" || e.city!="Mumbai" || e.city!="mumbai") && (e.city!="CHENNAI" || e.city!="Chennai" || e.city!="chennai")&& (e.city!="Bangelore" || e.city!="BANGELORE" || e.city!="bangelore"))
            throw(3);
            if(e.Isvehicle=='n' || e.Isvehicle=='N')
            throw(4);

        }
            catch(int ex)
            {
                switch(ex)
                {
                    case 1:
                        cout<<"\n Exception Caught";
                        cout<<"\n User's age should be between 18 and 55!!!";
                    break;
                    case 2:
                        cout<<"\n Exception Caught";
                        cout<<"\n User should have income between Rs.50,000-Rs.1,00,000 per month!!!";
                    break;
                    case 3:
                        cout<<"\n Exception Caught";
                        cout<<"\n User should be from Pune/ Mumbai/ Bangalore / Chennai!!!";
                    break;
                    case 4:
                        cout<<"\n Exception Cauht";
                        cout<<"\n User should have 4-wheeler!!!";
                    break;
                }
            }
            return(dout);
        }

};
int main()
{
    int ch;
    exception_handling e1;
    cout<<"\n\t============Exception Handling===============\n";
    do
    {
        cout<<"\n\n\t 1.Insert data\n\t Display Data \n\t 3.Exit\n";
        cout<<"Enter your choice";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cin>>e1;
            break;
            case 2:
                cout<<e1;
            break;
            case 3:
            break;
        default:
            cout<<"Enter correct choice";
        }
    }while(ch!=3);
    return 0;
}
*/

Write a C++ program that creates an output file, writes information to it, closes the file and open it again as an input file

Write a C++ program that creates an output file, writes information to it, closes the file and open it again as an input file and read the information from the file.

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
  char str[10];
  //Creates an instance of ofstream, and opens example.txt
  ofstream a_file("c:\\one.txt");
  // Outputs to example.txt through a_file
  a_file<<"This text will now be inside of example.txt";
  // Close the file stream explicitly
  a_file.close();
  //Opens for reading the file
  ifstream b_file("c:\\one.txt");
   //Reads one string from the file
  b_file>>str;
  //Should output 'this'
  cout<<str<<"\n";
  cin.get();    // wait for a keypress
  // b_file is closed implicitly here
}