C++ Programming
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.
[Marks 15]
Solution
#include<iostream.h> //include iostream.h header file
#include<conio.h> //include conio.h header file
class integer
{
int a;
public:
integer(int b)
{
a = b;
}
void display()
{
cout<<a;
}
void operator--()
{
--a;
}
void operator--(int)
{
a--;
}
};
void main()
{
integer obj(4);
clrscr();
cout<<"\n\n\tBefore decreament value of a = ";
obj.display();
--obj;
cout<<"\n\n\tAfter decreament value of a = ";
obj.display();
cout<<"\n\n\tBefore decreament value of a = ";
obj.display();
obj--;
cout<<"\n\n\tAfter decreament value of a = ";
obj.display();
getch();
}
Output
Before decreament value of a = 4
After decreament value of a = 3
Before decreament value of a = 3
After decreament value of a = 2
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.
[Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
#include<fstream.h>
int main()
{
clrscr();
ofstream fp1,fp2,fp3,fp4;
fstream fp;
char ch;
fp.open("d:/sy2014/cpp/Sample.txt",ios :: in);
if(!fp.good())
{
cout<<"\n\tError in openning Sample.txt";
getch();
exit(0);
}
fp1.open("d:/sy2014/cpp/upper.txt");
fp2.open("d:/sy2014/cpp/lower.txt");
fp3.open("d:/sy2014/cpp/digit.txt");
fp4.open("d:/sy2014/cpp/convert.txt");
while(fp)
{
fp.get(ch);
if(isdigit(ch))
{
fp3.put(ch);
fp4.put(ch);
}
else if(isupper(ch))
{
fp1.put(ch);
fp4.put(tolower(ch));
}
else if(islower(ch))
{
fp2.put(ch);
fp4.put(toupper(ch));
}
else
{
fp4.put(ch);
}
}
cout<<"\n\t created successfully";
fp.close();
fp1.close(); fp2.close();
fp3.close(); fp4.close();
getch();
return 0;
}
Tags:
c++