OOPS Practical File
OOPS Practical File
ETCS-258
AIM
Write a program for multiplication of two matrices using OOP.
SOURCE CODE
#include <iostream>
class Matrix {
int x[100][100];
int row;
int column;
public :
void display() {
cout<<"The matrix is : \n";
for(int i = 0;i<row;i++)
{
for(int j = 0;j<column;j++)
{
cout<<x[i][j]<<" ";
}
cout<<"\n";
}
}
};
int main() {
int l,m,n,o;
cout<<"Enter the number of rows and columns for matrix 1 :";
cin>>l>>m;
Matrix m1;
m1.input(l,m);
Matrix m2;
m2.input(n,o);
cout<<"\n\nMatrix 1\n";
m1.display();
cout<<"\n\nMatrix 2\n";
m2.display();
Matrix mult;
mult.multiplication(m1,m2);
mult.display();
return 0;
}
OUTPUT
EXPERIMENT – 2
AIM
Write a program to find whether a number is prime or not.
SOURCE CODE
#include<iostream>
using namespace std;
class Prime{
private:
int x;
public :
void input(int a)
{
x = a;
}
void isPrime()
{
int count = 0;
for(int i=2;i<=x/2;i++)
{
if(x%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<x<<" is a prime number\n";
else
cout<<x<<" is not a prime number\n";
}
};
int main()
{
int n;
Prime num;
num.input(n);
num.isPrime();
return 0;
}
OUTPUT
EXPERIMENT – 3
AIM
Write a program to take name, address as character string, age as int, salary as float and contains inline
function to set the values and display it.
SOURCE CODE
#include<iostream>
using namespace std;
class details {
private:
int age;
char name[20];
char address[40];
float salary;
public:
inline void inputData();
inline void displayData();
};
void details::inputData(){
void details::displayData(){
int main()
{
details a,b;
cout<<"\nEnter the details of a:";
a.inputData();
cout<<"\nEnter the details of b:";
b.inputData();
return 0;
}
OUTPUT
EXPERIMENT – 4
AIM
Using the concept of function overloading, write a function for calculating area of triangle, circle
and rectangle.
SOURCE CODE
#include<iostream>
using namespace std;
int main()
{
int length,breadth;
float radius,base,height;
int choice;
cout<<"Enter choice(1 for rectangle 2 for circle 3 for triangle) : ";
cin>>choice;
switch(choice)
{
AIM
Write a program to create a class Student which have data members as name, branch, roll no, age,
sex and five subjects. Display the name of student and his percentage who has more than 70%.
SOURCE CODE
#include<iostream>
using namespace std;
class Student {
private :
int subjects[5];
char name[20];
char branch[10];
float rollno;
int age;
char sex;
public :
void getData(){
void showData(){
int main()
{
Student s1,s2;
cout<<"\nEnter the details of Student1 :";
s1.getData();
cout<<"\nEnter the details of Student2 :";
s2.getData();
AIM
Write a program to create a TIME class with members hours, minutes and seconds. Take input, add
two-time objects and passing objects to function and display the result.
SOURCE CODE
#include <iostream>
class time
{
private:
int hours;
int minutes;
int seconds;
public:
void inputTime()
{
cout << "\n Enter time :";
cout << " \nEnter Hours :";
cin>>hours;
cout << "\nEnter Minutes :";
cin>>minutes;
cout << "\nEnter Seconds :";
cin>>seconds;
}
void showTime()
{
cout << "\nTime after addition :";
cout << hours << ":" << minutes << ":" << seconds << endl;
}
AIM
Write a program to create a function power to raise a number m to a power n. The function takes
double value for m and integer value for n. use default value for n to make the function. Calculate
the squares when this argument is omitted.
SOURCE CODE
#include <iostream>
double power = 1;
return power;
}
int main()
{
double m;
int n,choice;
switch(choice)
{
case 1 : cout<<"Enter the number:";
cin>>m;
cout<<"Enter the power :";
cin>>n;
cout<<"\n"<<m<<"^"<<n<<"="<<Power(m,n);
break;
return 0;
}
}
OUTPUT
EXPERIMENT – 8
AIM
Write a program to find the greatest of two given numbers in two different classes using friend
function.
SOURCE CODE
#include <iostream>
class number1;
class number2;
class number1
{
int a;
public :
class number2
{
int b;
public :
int main()
{
number1 x;
number2 y;
int num1,num2;
cout<<"Enter the first number : ";
cin>>num1;
x.input(num1);
cout<<"Enter the second number : ";
cin>>num2;
y.input(num2);
cout<<"The largest number is :"<<Largest(x,y);
return 0;
}
OUTPUT
EXPERIMENT – 9
AIM
Write a program to find the biggest of three number using Friend Function.
SOURCE CODE
#include <iostream>
class num1;
class num2;
class num3;
class num1
{
int a;
public :
void input(int x)
{
a=x;
}
class num2
{
int b;
public :
void input(int y)
{
b=y;
}
class num3
{
int c;
public :
void input(int z)
{
c=z;
}
int main()
{
num1 a;
num2 b;
num3 c;
int x,y,z;
cout<<"Enter the three numbers : ";
cin>>x>>y>>z;
a.input(x);
b.input(y);
c.input(z);
cout<<"The largest number is : "<<Cal(a,b,c);
return 0;
}
OUTPUT
EXPERIMENT – 10
AIM
Write a program to demonstrate the use of friend function with Inline assignment.
SOURCE CODE
#include <iostream>
class number
{
int a;
public :
void input(int x)
{
a=x;
}
int main()
{
number a;
int x;
cout<<"Enter the number : ";
cin>>x;
a.input(x);
cout<<"The private number using friend function is : "<<Cal(a);
return 0;
}
OUTPUT
EXPERIMENT – 11
AIM
Write a program to find the sum of two numbers declared in a class and display the numbers and sum
using friend class.
SOURCE CODE
#include <iostream>
class number1;
class number2;
class number1
{
int a;
public :
class number2
{
int b;
public :
int main()
{
number1 x;
number2 y;
int num1,num2;
cout<<"Enter the first number : ";
cin>>num1;
x.input(num1);
cout<<"Enter the second number : ";
cin>>num2;
y.input(num2);
cout<<"The sum is :"<<Sum(x,y);
return 0;
}
OUTPUT
EXPERIMENT – 12
AIM
Write a program to perform addition of two complex numbers using constructor overloading. The
first constructor which takes no argument is used to create objects which are not initialized, second
which takes argument is used to initialize real and imaginary parts to equal values and third which
takes two arguments is used to initialized real and imaginary to two different values.
SOURCE CODE
#include <iostream>
class Complex
{
int real;
int imaginary;
public:
Complex()
{
real = 0;
imaginary =0;
Complex(int x)
{
real = x;
imaginary = x;
}
Complex(int x,int y)
{
real = x;
imaginary = y;
}
int getReal()
{
return real;
}
int getImaginary()
{
return imaginary;
}
};
int main()
{
int e,f,g,h;
cout<<"Enter values for complex number a : ";
cin>>e>>f;
Complex a(e,f);
cout<<"Enter values for complex number b : ";
cin>>g>>h;
Complex b(g,h);
Complex add;
add.addNumbers(a,b);
AIM
Write a program to enter any number and find its factorial using constructor.
SOURCE CODE
#include <iostream>
factorial()
{
x = 0;
fact = 1;
}
factorial(int a)
{
if(a<=0)
{
fact = 1;
}
else
fact = 1 ;
for(int i=1;i<=a;i++)
{
fact = fact*i;
}
}
int getFact()
{
return fact;
}
};
int main()
{
int a;
cout << "Enter number : ";
cin>>a;
factorial fact(a);
return 0;
}
OUTPUT
EXPERIMENT – 14
AIM
Write a program to generate a Fibonacci series using Copy Constructor.
SOURCE CODE
#include <iostream>
using namespace std;
class fibonacci
{
private:
int f,f1,f2,m;
public:
fibonacci(int n)
{
m=n;
}
for(int i=2;i<=x.m;i++)
{
x.f=x.f1+x.f2;
cout<<" "<<x.f;
x.f1=x.f2;
x.f2=x.f;
}
}
};
int main()
{
int n;
cout<<"\n Enter the number of elements in series:";
cin>>n;
fibonacci A(n);
fibonacci B(A);
return 0;
}
OUTPUT
EXPERIMENT – 15
AIM
Implement a class string containing the following functions:
• Overload + operator to carry out the concatenation of strings.
• Overload = operator to carry out string copy.
• Overload <= operator to carry out the comparison of strings.
• Function to display the length of string.
• Function tolower() to convert upper case to lower case.
• Function toupper() to convert lower case letters to upper case.
SOURCE CODE
• Overload + operator to carry out the concatenation of strings.
#include <iostream>
class String
{
private:
string Word;
public :
String()
{
Word = "";
}
String(string a )
{
Word = a;
}
void display()
{
cout<<"The string is : "<<Word;
}
};
int main()
{
string x,y;
cout<<"Enter first string : ";
getline(cin,x);
cout<<"Enter second string : ";
getline(cin,y);
String a(x);
String b(y);
String c = a + b;
c.display();
}
• Overload = operator to carry out string copy.
#include <iostream>
class String
{
private:
string Word;
public :
String()
{
Word = "";
}
String(string a )
{
Word = a;
}
void display()
{
cout<<"The string is : "<<Word;
}
};
int main()
{
string x;
cout<<"Enter first string : ";
getline(cin,x);
String a(x);
String c = a ;
c.display();
}
• Overload <= operator to carry out the comparison of strings.
#include <iostream>
class String
{
private:
string Word;
public :
String()
{
Word = "";
}
String(string a )
{
Word = a;
}
void display()
{
cout<<"The string is : "<<Word;
}
};
int main()
{
string x,y;
cout<<"Enter first string : ";
getline(cin,x);
cout<<"Enter second string : ";
getline(cin,y);
String a(x);
String b(y);
a<=b;
}
• Function to display the length of string.
include <iostream>
class String
{
private:
string Word;
public :
String()
{
Word = "";
}
String(string a)
{
Word = a;
}
void Length()
{
int count;
for (int i = 0;Word[i];i++)
{
count++;
}
cout<<"The length is :"<<count;
}
void display()
{
cout<<"The string is : "<<Word<<"\n";
}
};
int main()
{
string x,y;
cout<<"Enter first string : ";
getline(cin,x);
String a(x);
a.display();
a.Length();
}
• Function tolower() to convert upper case to lower case.
#include <iostream>
#include<cstring>
class String
{
private:
char Word[100];
int Size;
public :
String(int len)
{
Size = len;
cout<<"Enter string : ";
cin.getline(Word,Size);
}
void display()
{
cout<<"The string is : "<<Word<<"\n";
}
};
int main()
{
char x[30];
int Size;
cout<<"Enter size of String :";
cin>>Size;
cin.ignore();
String a(Size);
a.tolower(a);
a.display();
}
• Function toupper() to convert lower case letters to upper case.
#include <iostream>
#include<cstring>
class String
{
private:
char Word[100];
int Size;
public :
String(int len)
{
Size = len;
cout<<"Enter string : ";
cin.getline(Word,Size);
}
void display()
{
cout<<"The string is : "<<Word<<"\n";
}
};
int main()
{
char x[30];
int Size;
cout<<"Enter size of String :";
cin>>Size;
cin.ignore();
String a(Size);
a.toupper(a);
a.display();
}
OUTPUT
• Overload + operator to carry out the concatenation of strings.
• F
u
n
ct
io
n
to
upper() to convert lower case letters to upper case.
EXPERIMENT – 16
AIM
Write a program to overload new and delete operators.
SOURCE CODE
#include <iostream>
class Data
{
private :
int number;
public :
Data()
{
cout<<"\nConstructor is called.";
}
Data(int n)
{
number = n;
}
void display()
{
cout<<"\nThe data is : "<<number<<"\n";
}
int main()
{
Data * p = new Data(10);
p->display();
delete p;
}
OUTPUT
EXPERIMENT – 17
AIM
Write a program to overload unary increment(++) and decrement(--) operators
SOURCE CODE
#include <iostream>
class Data
{
public :
int number;
Data(int a)
{
number = a;
}
void display()
{
cout<<"The number is : "<<number;
}
};
int main()
{
int x;
cout<<"\nEnter number : ";
cin>>x;
Data D(x);
D.number++;
D.display();
D.number--;
D.display();
return 0;
}
OUTPUT
EXPERIMENT – 18
AIM
Create a base class basic_info with data members name, roll no, sex and two member
functions getdata and display. Derive a class physical_fit from basic_info which has data members height
and weight and member functions getdata and display. Display all the information using object of derived
class.
SOURCE CODE
#include <iostream>
class basic_info
{
private :
string name;
int rollno;
char sex;
public :
void getdata()
{
cout<<"\nEnter name : ";
getline(cin,name);
cout<<"\nEnter roll No. : ";
cin>>rollno;
cout<<"\nEnter gender(M/F) :";
cin>>sex;
}
void display()
{
cout<<"\nThe name is : "<<name;
cout<<"\nThe roll no. is : "<<rollno;
cout<<"\nThe gender is : "<<sex;
}
};
void display()
{
basic_info::display();
cout<<"\nThe height is : "<<height;
cout<<"\nThe weight is : "<<weight;
}
};
int main()
{
physical_fit p;
p.getdata();
p.display();
}
OUTPUT
EXPERIMENT – 19
AIM
Create class first with data members book no, book name and member function getdata and putdata.
Create a class second with data members author name, publisher and members getdata and showdata.
Derive a class third from first and second with data member no of pages and year of publication. Display
all this information using array of objects of third class.
SOURCE CODE
class first
{
private:
char bname[50]; int bno;
public:
void getdata ()
{
cout << "Enter Book No. : "; cin >> bno;
cout << "Enter Book name : "; cin >> bname;
}
void putdata()
{
cout << "Book No. : " << bno << endl; cout << "Book
Name : " << bname << endl;
}
};
class second
{
private:
char author[20]; char
publisher[20];
public:
void indata()
{
cout << "Enter Author : "; cin >> author;
cout << "Enter Publisher : "; cin >> publisher;
}
void showdata()
{
cout << "Author : "; cout <<
author;
cout << "Publisher : "; cout <<
publisher;
}
};
class third : public first, public second
{
private:
int year; int
page;
public:
void input()
{
getdata(); indata();
cout << "Enter number of pages : "; cin >> page;
cout << "Enter Year of Publication : "; cin >> year;
}
void output()
{
putdata();
showdata();
cout << "Number of pages : " << page << endl; cout << "Year of
Publication : " << year;
}
};
main()
{
int i, n;
cout << "Enter number of Books : "; cin >> n;
third obj[5];
for (i = 0; i < n; i++)
{
obj[i].input();
}
for (i = 0; i < n; i++)
{
cout << "About Book no. " << i + 1 << " : " << endl; obj[i].output();
}
}
OUTPUT
EXPERIMENT – 20
AIM
Design three classes STUDENT, EXAM, and RESULT. The STUDENT class has data members such as
roll no, name. Create a class EXAM by inheriting the STUDENT class. The EXAM class adds data
members representing the marks scored in six subjects. Derive the RESULT from the EXAM class and
has its own data members such as total marks. WAP to model this relationship.
SOURCE CODE
#include <iostream>
class Student
{
public :
int rollno;
string name;
void getinfo()
{
cout<<"\nEnter Roll No. :";
cin>>rollno;
cin.ignore();
cout<<"\nEnter name :";
getline(cin,name);
}
};
{
public :
float marks[6];
void getmarks()
{
cout<<"\nEnter marks in 6 subjects : ";
for(int i = 0;i<6;i++)
{
cin>>marks[i];
}
}
};
{
public :
float totalmarks;
void getdata()
{
getinfo();
getmarks();
}
void showResult()
{
cout<<"\nThe roll no is :"<<rollno;
cout<<"\nThe name is : "<<name;
float sum = 0;
for(int i = 0;i<6;i++)
{
sum+=marks[i];
}
totalmarks = sum;
cout<<"\nThe total marks are(out of 600) :"<<totalmarks;
}
};
int main()
{
Result R;
R.getdata();
R.showResult();
return 0;
}
OUTPUT
EXPERIMENT – 21
AIM
Create a class called LIST with two pure virtual function store() and retrieve().To store a value call store
and to retrieve call retrieve function. Derive two classes stack and queue from it and override store and
retrieve.
SOURCE CODE
#include <iostream>
class List
{
public :
int List[30];
int size;
virtual void store() = 0;
virtual void retrieve() = 0;
};
{
public :
void store()
{
cout<<"Enter number of elements : ";
cin>>size;
cout<<"\nEnter stack :";
for(int i = 0;i<size;i++)
{
cin>>List[i];
}
}
void retrieve()
{
cout<<"\nThe elements in stack are(from the top): ";
for(int i = 0;i<size;i++)
{
cout<<"\n"<<List[size-i-1];
}
}
};
{
public :
void store()
{
cout<<"\nEnter number of elements : ";
cin>>size;
cout<<"\nEnter queue :";
for(int i = 0;i<size;i++)
{
cin>>List[i];
}
}
void retrieve()
{
cout<<"\nThe elements in queue are(from the start): ";
for(int i = 0;i<size;i++)
{
cout<<"\n"<<List[i];
}
}
};
int main()
{
List *L;
Stack S;
L = &S;
L->store();
L->retrieve();
Queue Q;
L = &Q;
L->store();
L->retrieve();
return 0;
}
OUTPUT
EXPERIMENT – 22
AIM
Create a base class called SHAPE. Use this class to store two double type values. Derive two specific
classes called TRIANGLE and RECTANGLE from the base class. Add to the base class, a member
function getdata to initialize base class data members and another member function display to compute
and display the area of figures. Make display a virtual function and redefine this function in the derived
classes to suit their requirements. Using these three classes design a program that will accept driven of a
TRIANGLE or RECTANGLE interactively and display the area.
SOURCE CODE
#include <iostream>
class Shape
{
public :
double value1;
double value2;
void getdata()
{
cout<<"\nEnter value 1 :";
cin>>value1;
cout<<"\nEnter value 2 :";
cin>>value2;
}
{
public :
double area;
void display()
{
area = value1 * value2;
cout<<"\nThe area of rectangle is : "<<area<<"\n\n";
}
};
{
public :
double area;
void display()
{
area = 0.5 * value1 * value2;
cout<<"\nThe area of triangle is : "<<area<<"\n\n";
}
};
int main()
{
Shape *S;
Rectangle R;
S = &R;
S->getdata();
S->display();
Triangle T;
S = &T;
S->getdata();
S->display();
return 0;
}
OUTPUT
EXPERIMENT – 23
AIM
Write a program to define the function template for calculating the square of given numbers with
different data types.
SOURCE CODE
#include <iostream>
template<typename T>
T square(T a){
return a*a;
}
int main()
{
int i;
float j;
cout<<"Enter integer value: ";
cin>>i;
cout<<"\nThe square is : "<<square(i);
cout<<"\nEnter float value :";
cin>>j;
cout<<"\nThe square is : "<<square(j);
}
OUTPUT
EXPERIMENT – 24
AIM
Write a program to demonstrate the use of special functions, constructor and destructor in the class
template. The program is used to find the largest of two numbers entered.
SOURCE CODE
#include <iostream>
public :
Bigger(T x,T y)
{
a = x;
b = y;
}
~Bigger()
{
cout<<"\nObject Destroyed";
}
T Compare()
{
if(a>b)
{
return a;
}
return b;
}
};
int main()
{
int x,y;
float e,d;
cout<<"Enter the integer numbers : ";
cin>>x>>y;
Bigger<int> b(x,y);
cout<<"\nThe bigger number is : "<<b.Compare();
cout<<"\nEnter the float numbers : ";
cin>>e>>d;
Bigger<float> c(e,d);
cout<<"\nThe bigger number is : "<<c.Compare();
}
OUTPUT
EXPERIMENT – 25
AIM
Write a program to define a function template for swapping two items of the various data types such as
integer, float and character.
SOURCE CODE
#include <iostream>
template<class T>
void Swap(T &a,T &b){
T temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int i,j;
cout<<"Enter the two integer values : ";
cin>>i>>j;
Swap(i,j);
cout<<"\nThe numbers after swapping are : "<<i<<" and "<<j;
float a,b;
cout<<"\nEnter the two float values : ";
cin>>a>>b;
Swap(a,b);
cout<<"\nThe numbers after swapping are : "<<a<<" and "<<b;
char c,d;
cout<<"\nEnter the two characters : ";
cin>>c>>d;
Swap(c,d);
cout<<"\nThe characters after swapping are : "<<c<<" and "<<d;
return 0;
}
OUTPUT
EXPERIMENT – 26
AIM
Write a program to illustrate how template functions can be overloaded.
SOURCE CODE
#include <iostream>
template<typename T>
void square(T a){
cout<<"\nTemplate function called.";
cout<<"\nThe square is :"<<a*a;
}
int main()
{
int i;
float j;
cout<<"Enter integer value: ";
cin>>i;
square(i);
cout<<"\nEnter float value :";
cin>>j;
square(j);
}
OUTPUT
EXPERIMENT – 27
AIM
Write a program to illustrate how to define and declare a class template for reading two data items from
the keyboard and find their sum.
SOURCE CODE
#include <iostream>
public :
Sum(T x,T y)
{
a = x;
b = y;
}
T Addition()
{
return a+b;
}
};
int main()
{
int x,y;
float e,d;
cout<<"Enter the integer numbers : ";
cin>>x>>y;
Sum<int> b(x,y);
cout<<"\nThe sum is : "<<b.Addition();
cout<<"\nEnter the float numbers : ";
cin>>e>>d;
Sum<float> c(e,d);
cout<<"\nThe sum is : "<<c.Addition();
return 0;
}
OUTPUT
EXPERIMENT – 28
AIM
Write a program to read a set of lines from keyboard and store it on a specified file.
SOURCE CODE
#include <iostream>
#include<fstream>
int main()
{
string data;
cout<<"Enter a string : ";
getline(cin,data);
ofstream out_file;
out_file.open("test.txt");
out_file << data << endl;
cout<<"File made successfully.";
out_file.close();
}
OUTPUT
EXPERIMENT – 29
AIM
Write a program to read a text file and display its contents on the screen.
SOURCE CODE
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int n;
char str1[100], str2[100];
fstream outfile;
outfile.open("exp23.txt", ios::out);
cout << "Writing file...." << endl;
cin.get();
cout << "Type something : ";
gets(str1);
outfile << str1 << endl;
outfile.close();
fstream infile;
infile.open("exp23.txt", ios::in);
cout << "Reading from file : " << endl;
while (infile.getline(str2, 100))
{
puts(str2);
}
infile.close();
return 0;
}
OUTPUT
EXPERIMENT – 30
AIM
Write a program to copy the contents of a file into another.
SOURCE CODE
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int n;
char str1[100], str2[100], str3[100];
fstream outfile1;
outfile1.open("myfile.txt", ios::out);
cout << "Writing to 1st file....." << endl;
cin.get();
cout << "Enter data : "; gets(str1);
outfile1 << str1 << endl;
outfile1.close();
fstream infile1, outfile2;
infile1.open("myfile.txt", ios::in);
outfile2.open("myfile1.txt", ios::out);
cout << "Reading from the first file and copying to second file" << endl;
while (infile1.getline(str2, 100))
{
puts(str2);
outfile2 << str2 << endl;
}
infile1.close();
outfile2.close();
fstream infile2;
infile2.open("myfile1.txt", ios::in);
cout << "Reading from copied file" << endl;
while (infile2.getline(str3, 100))
{
puts(str3);
}
infile2.close();
return 0;
}
OUTPUT
EXPERIMENT – 31
AIM
Write a program to read the class object of student_info such as name, age, sex, height and weight from
the keyboard and to store them in a file using read() and write() functions. Again the same file is opened
for reading and displaying the contents of the file on the screen
SOURCE CODE
#include <iostream>
#include<fstream>
using namespace std;
class Student
{
public:
string name;
int age;
char sex;
float weight,height;
void enterdata()
{
cout<<"\nEnter student name : ";
getline(cin,name);
cout<<"\nEnter student age : ";
cin>>age;
cout<<"\nEnter gender : ";
cin>>sex;
cout<<"Enter height(in m) : ";
cin>>height;
cout<<"Enter Weight(in kg) : ";
cin>>weight;
}
void showdata()
{
cout<<"\nThe student name is : "<<name;
cout<<"\nThe student age is: "<<age;
cout<<"\nThe gender is: "<<sex;
cout<<"\nThe height is(in m) : "<<height;
cout<<"\nThe Weight is(in kg) : "<<weight;
}
};
int main()
{
Student S;
S.enterdata();
fstream outfile;
outfile.open("Input.txt",ios::out);
outfile.write((char*)&S, sizeof(S));
outfile.close();
Student D;
fstream infile;
infile.open("Input.txt",ios::in);
infile.read((char*)&D, sizeof(D));
D.showdata();
outfile.close();
return 0;
}
OUTPUT
EXPERIMENT – 32
AIM
Write a program to perform the deletion of white spaces such as horizontal tab, vertical tab, space,
line feed, new line and carriage return from a text file and store the contents of the file without the white
spaces on another file.
SOURCE CODE
int main()
{
int n;
char str1[100], str2[100];
cout << "Enter no. of lines : "; cin >> n;
fstream outfile; outfile.open("exp8in.txt", ios::out); cout
<< "Writing file...." << endl; cin.get();
for (int i = 1; i <= n; i++)
{
cout << "Type something : "; gets(str1);
outfile << str1 << endl;
}
outfile.close();
{
fout << s;
}
fin.close();
fout.close();
fstream infile; infile.open("exp8out.txt", ios::in); cout <<
"Reading from file : " << endl; while (infile.getline(str2,
100))
{
puts(str2);
}
infile.close();
return 0;
}
OUTPUT
EXPERIMENT – 33
AIM
Write a program to raise an exception if any attempt is made to refer to an element whose index is
beyond the array size.
SOURCE CODE
#include <iostream>
class Array
{
private :
int a[100];
int size;
public :
Array(int y)
{
size = y;
for (int i = 0;i<size;i++)
{
cin>>a[i];
}
showArray();
}
void showArray()
{
cout<<"\nThe array is :";
for(int i = 0;i< size;i++)
{
cout<<a[i]<<" ";
}
}
int main()
{
int size,i;
cout << "Enter size of array :" << endl;
cin>>size;
cout<<"\nEnter array : ";
Array a(size);
cout<<"\nEnter index of element to be accessed : ";
cin>>i;
try
{
int element = a.accessElement(i);
cout<<"\nThe element is : "<<element;
}catch(const char* msg)
{
cout<<msg<<endl;
}
return 0;
}
OUTPUT
EXPERIMENT – 34
AIM
Write a program to read two numbers and then divide first no by second no and raise an exception if
second number is zero.
SOURCE CODE
#include <iostream>
class Divide
{
private :
float a;
float b;
public :
Divide(int x,int y)
{
a=x;
b=y;
}
float Division()
{
if(b==0)
{
throw "Denominator is zero.Exception caused.";
}
return (a/b);
}
};
int main()
{
float a,b;
cout<<"\nEnter element 1 : ";
cin>>a;
cout<<"\nEnter element 2 : ";
cin>>b;
Divide d(a,b);
try {
float z = d.Division();
cout<<"\nThe answer is :"<<z;
}
catch (const char* msg){
cout<<msg<<endl;
}
return 0;
}
OUTPUT