Friday, 19 January 2018

CPP-Slip19



Slip19
Q.1)     Write a C++ program to create a class Integer. Write necessary member functions to overload the operator unary pre and post decrement ‘--’  for an integer number.

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class integer
{
int no;
public:
integer(){}
integer(int num)
{
no=num;
}
integer operator--()
{
--no;
return *this;
}
integer operator--(int) //post increment
{
integer temp=*this;
no--;
return temp;
}
void display()
{
cout<<"\nNo = "<<no;
}
};
void main()
{
integer i(10),i1(10),i3;
clrscr();
cout<<"\nAfter post increment = ";
i3=i--;
i3.display();
cout<<"\nAfter pre increment = ";
i3=--i1;
i3.display();
getch();
}

Another way



#include<iostream.h>
#include<conio.h>
 class Integer
   {
    int a;
    public:
    void accept();
    void show();
    void operator--();
   };
 void Integer::accept()
   {
    cout<<"\nEnter a Number:";
    cin>>a;
   }
 void Integer::show()
   {
    cout<<"\nEntered number is:"<<a;
   }
 void Integer::operator--()
   {
    int temp;
    temp=a;
    cout<<"\nPre decrement of the given number is:"<<--temp;
    cout<<"\nPost  decrement of the given number is:"<<a--;
   }
 int main()
   {
    clrscr();
    Integer i;
    i.accept();
    i.show();
    i.operator--();
    getch();
    return 0;
   }
          

 

Q.2)Write a C++ program to read the contents of a “Sample.txt” file. Store all the uppercase characters in ”Upper.txt”, lowercase characters in ”Lower.txt” and digits in “Digit.txt” files. Change the case of each character from “Sample.txt” and store it in “Convert.txt” file.            

 #include<conio.h>

#include<fstream.h>

#include<stdlib.h>

#include<ctype.h>

 void main()

{

char fname[20];

ifstream fin; ofstream fc,fu,fl,fd;

cout<<"\n Enter file name : ";

cin>>fname;

fin.open(fname,ios::in);

fc.open("convert.txt",ios::out);

fu.open("upper.txt",ios::out);

fl.open("lower.txt",ios::out);

fd.open("digit.txt",ios::out);

if(fin.fail())

{

cout<<"file not exists ";

exit(1);

}

 char ch;

while(!fin.eof())

{

ch=fin.get();

if(islower(ch))

{

fl.put(ch);

ch=toupper(ch);

fc.put(ch);

}

else if(isupper(ch))

{

fu.put(ch);

ch=tolower(ch);

fc.put(ch);

}

else if(isdigit(ch))

{

fd.put(ch);

}

else if(isspace(ch))

{fc.put(ch);

}

}

cout<<"file coped....";

getch();

}