You are on page 1of 78

OOPS

PRACTICAL FILE

Submitted by :- Shubham Dhoundiyal


Enrollment no. :- 04096402719
Batch :- 4C13
INDEX

S Experiment Name Marks Total Signature


No Mark and
Remarks
R1 R2 R3 R4
1 Write a program to find whether a
number is prime or not.

2 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
3 Using the concept of function
overloading, write function for
calculating area of triangle, circle and
rectangle
4 Create a class Student, which have
data members as name, branch, roll
no., age, sex, five subjects. Display
the name of student & his percentage
who has more than 70%.

5 Write a program for multiplication of


two matrices using OOP.

6 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 one
argument is used to initialize real and
imag parts to equal values and third
which takes two argument is used to
initialized real and imag to two
different values.

7 To create a class TIME with members


hours, minutes and seconds. Take
input, add two-time objects and
passing objects to function and
display the result.

8 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.

9 Write a program to enter any number


and find its factorial using
constructor.

10 Write a program to generate a


Fibonacci series using Copy
Constructor.

11 Write a program to find the biggest of


three number using Friend Function.

12 Write a program to demonstrate the


use of friend function with Inline
assignment.

13 Write a program to find the sum of


two numbers declared in a class and
display the numbers and sum using
friend class.

14 Write a program to find the greatest


of two given numbers in two different
classes using friend function.

15 Write a program to overload new and


delete operator.

16 Write a program to overload unary


increment (++) operator.

17 Implement a class string containing


the following functions:

1.Overload + operator to carry out the


concatenation of strings.

2.Overload = operator to carry out


string copy.

3.Overload <= operator to carry out


the comparison of strings.

4.Function to display the length of


string.
5.Function tolower() to convert upper
case to lower case.

6.Function toupper() to convert lower


case letters to upper case.

18 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.

19 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.(Single
Inheritance)

20 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. (Multiple Inheritance)

21 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.(Multilevel Inheritance)

22 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.

23 Write a program to define the


function template for calculating the
square of given numbers with
different data types.

24 Write a program to demonstrate the


use of special functions, constructor
and destructor in the class template.
The program is used to find the bigger
of two entered numbers.

25 Write a program to define the


function template for swapping two
items of various datatypes such as
integers, float and characters.

26 Write a program to illustrate how


Template function can be overloaded.

27 Write a program to illustrate how to


define and declare a class template for
reading two data items from the
keyboard and to find their sum.

28 Write a program to read a set of lines


from the keyboard and to store it on a
specified file.

29 Write a program to read a text file and


display its contents on the screen.

30 Write a program to copy the contents


of a file into another.

31 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.

32 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 on a
specified file using read () and write
() functions. Again, the same file is
opened for reading and displaying the
contents of the file on the screen.

33 Write a program to raise an exception


if any attempt is made to refer to an
element whose index is beyond the
array size.

34 Write a program to read two numbers


and then divide first no by second no
and raise an exception if second
number is zero.
EXPERIMENT 1

AIM : Write a program to find whether a number is prime or not .

Source Code:

#include<iostream> using
namespace std; class prime {
int a, k, i; public:
prime(int x) {
a = x;
}
void calculate() {
k = 1;
{
for (i = 2; i <= a / 2; i++)
if (a % i == 0) {
k = 0;
break;
} else {
k = 1;
}
}
}

void output() {
if (k == 1)
cout << a << " is Prime.";
else
cout << a << " is not Prime.";
}
};

int main() {
int a;
cout << "Enter a number : "; cin>>a;
prime obj(a);
obj.calculate();
obj.output(); return 0;
}

Output :

EXPERIMENT 2
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 info
{

char name[20]; char


address[100]; int age;
float salary;

public:
inline void input()
{
cout << "Enter name : ";
cin >> name;
cout << "Enter address : ";
cin >> address;
cout << "Enter age : ";
cin >> age;
cout << "Enter salary : ";
cin >> salary;
}

inline void output()


{
cout << "Your name is : " << name << endl;
cout << "Your address is : " << address << endl;
cout << "Your age is : " << age << endl;
cout << "Your salary is : " << salary << endl;
}
};

int main()
{
info obj;
obj.input();
obj.output();

Output :
EXPERIMENT 3

AIM : Using the concept of function overloading, write function


for calculating area of triangle, circle and rectangle.

Source Code:

#include <iostream>
#define pi 3.14
using namespace std;

class CalcArea
{
public:
void area(int); void area(int,
int);
void area(float, int, int);
};

void CalcArea::area(int a)
{

cout << "Area of Circle : " << pi * a * a;


}

void CalcArea::area(int a, int b)


{
cout << "Area of rectangle : " << a * b;
}

void CalcArea::area(float t, int a, int b)


{
cout << "Area of triangle : " << t * a * b;
}

main()
{
int ch;
int a, b, r;
CalcArea obj;
cout << "\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n";
cout << "Enter Choice : "; cin >> ch;
switch (ch)
{
case 1:
cout << "Enter Radius of the Circle : ";
cin >> r; obj.area(r); break;
case 2:
cout << "Enter Sides of Rectangle : ";
cin >> a >> b;
obj.area(a, b); break;
case 3:
cout << "Enter Sides of the Triangle : ";
cin >> a >> b;
obj.area(0.5, a, b); break;
case 4:
exit(0);
}
}

Output
EXPERIMENT 4

AIM : Create a class Student, which have data members as name,


branch, roll no., age, sex, five subjects. Display the name of student
& his percentage who has more than 70%.

Source Code :

#include <iostream> using


namespace std; class student
{
private:
char name[20]; char
branch[10]; char sex;
int age; int
rn;
float marks[5];

public:
float ptge;
void input();
void percent();
void output();
};
void student::percent()
{
float sum = 0;
for (int i = 0; i < 5; i++)
{
sum += marks[i];
}
ptge = (sum / 500) * 100;
}
void student::input()
{
cout << "Enter Name : ";
cin >> name;
cout << "Enter Branch : ";
cin >> branch;
cout << "Enter Roll No. : ";
cin >> rn;
cout << "Enter Age : ";
cin >> age;
cout << "Enter Sex : ";
cin >> sex;
for (int i = 0; i < 5; i++)
{
cout << "Enter marks in subject " << i + 1 << " : ";
cin >> marks[i];
}
}
void student::output()
{
cout << "Name : ";
puts(name);
cout << "Percentage : " << ptge << endl;
}
main()
{
student obj[10]; int n;
cout << "Enter no. of students : ";
cin >> n;
for (int i = 0; i < n; i++)
{
obj[i].input();
}
cout << endl;
for (int j = 0; j < n; j++)
{
obj[j].percent();
if (obj[j].ptge > 70)
{
obj[j].output();
}
}
Shubham Dhoundiyal 4C13 040964002719
\

Output :
Shubham Dhoundiyal 4C13 040964002719
\

EXPERIMENT 5

AIM : Write a program for multiplication of two matrices using OOP.

Source Code :

#include <iostream> using


namespace std; class matrix
{
int i, j, a[10][10], b[10][10], c[10][10], row1, row2,col1, col2, k;

public:
void input();
void output();
void multiply();
};
void matrix::input()
{
cout << "Enter no. of rows and column for the first matrix: ";
cin >> row1 >> col1;
cout << "Enter the elements for first matrix : ";
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
cin >> a[i][j];
}
}
cout << "Enter the row size & column size of second matrix:";
cin >> row2 >> col2;
if (col1 == row2)
{
cout << "Enter the elements for" << row2 << "*" << col2 << "matrix:";
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
cin >> b[i][j];
}
}
multiply();
output();
}
else
cout << "Can't Multiply";
}
void matrix::multiply()
{
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
c[i][j] = 0;
for (k = 0; k < row2; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
}
void matrix::output()
{
cout << "After Matrix Multiplication : ";
for (i = 0; i < row1; i++)
{
cout << "\n";
for (j = 0; j < col2; j++)
{
cout << "\t" << c[i][j];
}
}
}
main()
{
matrix obj;
obj.input();
}

Output :
EXPERIMENT 6

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 one argument is used to initialize real and imag parts to
equal values and third which takes two argument is used to
initialized real and imag to two different values.

Source Code :

#include <iostream> using


namespace std; class complex
{
private:
double r;
double i;

public:
complex();
complex(double a);
complex(double x, double y);
void sum(complex c1, complex c2);
void output();
};
complex::complex()
{
r = 0.0;
i = 0.0;
}
complex::complex(double a)
{
r = a;
i = a;
}
complex::complex(double x, double y)
{
r = x;
i = y;
}
void complex::sum(complex c1, complex c2)
{
r = c1.r + c2.r;
i = c1.i + c2.i;
}
void complex::output()
{
if (i > 0)
{
else
} {
cout << r << "+" << i << "i" << endl;
}
}

cout << r << i << "i" << endl;


EXPERIMENT 7

AIM : To create a class TIME 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> using


namespace std; class Time
{
private:
int hr;
int min; int
sec;

public:
void input(void);
void output(void);
void calculate(Time t1, Time t2);
};

void Time::input(void)
{
cout << "Enter time :" << endl;
cout << "Hours : ";
cin >> hr;
cout << "Minutes : ";
cin >> min;
cout << "Seconds : ";
cin >> sec;
}

void Time::output(void)
{
cout << endl;
cout << "After Adding : ";
cout << hr << " : " << min << " : " << sec << endl;
}

void Time::calculate(Time t1, Time t2)


{

sec = t1.sec + t2.sec;


min = t1.min + t2.min + sec / 60;
hr = t1.hr + t2.hr + (min / 60);
min %= 60;
sec %= 60;
}

int main()
{
Time t1, t2, T3;
t1.input();
t2.input();
T3.calculate(t1, t2);
T3.output();
return 0;
}

Output :
EXPERIMENT 8

AIM : 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> using
namespace std;
void Power(double m,int n=2)
{
double p=1;
for(int i=1;i<=n;i++)
{
p*=m;
}
cout<<m<<"^"<<n<<"="<<p<<endl;
}
main()
{
double m; int
n;
cout<<"Enter Power : "; cin>>n;
cout<<"Enter base : "; cin>>m;
cout<<"Not using default value : "<<endl; Power(m,n);
cout<<"Using default value : "<<endl; Power(m);
}

Output :
EXPERIMENT 9

AIM : Write a program to enter any number and find its factorial using
constructor.

Source Code :

#include<iostream> using
namespace std; class factorial
{
int num, f, i; public:

factorial() {
cout << "Enter a number : "; cin>>num;

f = 1;
for (i = 1; i <= num; i++)
{
f = f*i;
}
}

void show() {
cout << "The Factorial is:" << f;
}
};

int main() {

factorial obj;
obj.show();

return 0;
}

Output :
EXPERIMENT 10

AIM : Write a program to generate a Fibonacci series using


Copy Constructor.

Source Code :

#include <iostream> using


namespace std; class
fibonacci
{
private:
int f0, f1, fib;

public:
fibonacci()
{
f0 = 0;
f1 = 1;
fib = f0 + f1;
}
fibonacci(fibonacci &ptr)
{
f0 = ptr.f0;
f1 = ptr.f1;
fib = ptr.fib;
}
void increment()
{
f0 = f1;
f1 = fib;
fib = f0 + f1;
}
void output()
{
cout << fib << " ";
}
};
main()
{
int n;
cout << "Enter no. of elements in series : ";
cin >> n;
fibonacci obj;
for (int i = 1; i <= n; i++)
{
obj.output();
obj.increment();
}
}

Output :
EXPERIMENT 11

AIM : Write a program to find the biggest of three number using Friend
Function.

Source Code :

#include<iostream> using
namespace std; class Friend
{
private:
int a,b,c;
public:
void input()
{
cout<<"Enter 1st no. : "; cin>>a;
cout<<"Enter 2nd no. : "; cin>>b;
cout<<"Enter 3rd no. : "; cin>>c;
}
friend void biggest(Friend a);
};
void biggest(Friend l)
{
if((l.a>l.b)&&(l.a>l.c))
{
cout<<"Biggest no. : "<<l.a<<endl;
}
else if((l.b>l.a)&&(l.b>l.c))
{
cout<<"Biggest no. : "<<l.b<<endl;
}
else
{
cout<<"Biggest no. : "<<l.c<<endl;
}
}
main()
{
Friend obj;
obj.input();
biggest(obj);
}

Output :
EXPERIMENT 12

AIM : Write a program to demonstrate the use of friend function with


Inline assignment.

Source Code :

#include <iostream> using


namespace std;

class A;
class B
{
private:
int x;

public:
B(int m = 0)
{
x = m;
}
void setB(int m)
{
x = m;
}
friend void output(B &B, A &A);
};
class A
{
private:
int y;

public:
A(int n = 0)
{
y = n;
}
void setA(int n)
{
y = n;
}
friend void output(B &B, A &A)
{
cout << "B : " << B.x << " and A :" << A.y << endl;
}
};
main()
{
A obj1(10);
B obj2(12);
output(obj2, obj1);
}

Output :
EXPERIMENT 13

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> using


namespace std; class Add
{
private: int
a,b;
public:
void num()
{
cout<<"Enter 1st no. :"; cin>>a;
cout<<"Enter 2nd no. :"; cin>>b;
}
friend void sum(Add obj);
};
void sum(Add obj)
{
cout<<"Sum of numbers is : "<<obj.a+obj.b<<endl;
}
main()
{
Add obj;
obj.num();
sum(obj);
}

Output :

EXPERIMENT 14
AIM : Write a program to find the greatest of two given numbers in two
different classes using friend function.

Source Code :

#include <iostream> using


namespace std; class B;
class A
{
private:
int x;

public:
void num()
{
cout << "Enter number : ";
cin >> x;
}
friend void greatest(A a, B b);
};
class B
{
private:
int x;

public:
void num()
{
cout << "Enter number : ";
cin >> x;
}
friend void greatest(A a, B b);
};
void greatest(A a, B b)
{
if (a.x > b.x)
{
cout << "Greatest : " << a.x << endl;
}
else
{
cout << "Greatest : " << b.x << endl;
}
}
main()
{
A obj1;
B obj2;
obj1.num();
obj2.num(); greatest(obj1,
obj2);
}

Output :
EXPERIMENT 15

AIM : Write a program to overload new and delete operator

Source Code:

#include <iostream> using


namespace std; class student
{
string name; int
age;
public:

student(string name, int age)


{
this->name = name; this-
>age = age;
}
void output()
{
cout << "Name:" << name << endl;
cout << "Age:" << age << endl;
}
void *operator new(size_t size)
{
cout << "Overloading new operator with size: " << size << endl;
void *p = ::operator new(size);
return p;
}

void operator delete(void *p)


{
cout << "Overloading delete operator " << endl;
free(p);
}
};
int main()
{
student *p = new student("Udit", 20);
p->output();
delete p;
}

Output :
EXPERIMENT 16

AIM : Write a program to overload unary increment (++) operator.

Source Code :

#include<iostream> using
namespace std;

class unaryInc
{
private:
int n;

public:
void input(int x)
{
n=x;
}
void output(void)
{
cout << "value of N : " << n;
}
void operator ++ (void)
{
n=++n;
}

};
int main()
{
int y;
cout<<"Enter a number : "; cin>>y;
unaryInc obj;

obj.input(y);
++obj;
cout << "After Increment ";
obj.output(); cout
<< endl;

return 0;
}

Output :
EXPERIMENT 17
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:

#include <iostream>
#include <cstring> #include
<string>

using namespace std; class

my_string
{
private:
char str[30];

public:
void getdata(); void
display();
void operator=(my_string str1);
int operator<=(my_string str1);
void operator+(my_string str1);
int str_length();
void tolower();
void toupper();
};

void my_string::getdata()
{
cin >> str;
}

void my_string::display()
{
cout << "\n"<< str;
}
void my_string::operator+(my_string str1)
{
strcat(str, str1.str);
cout << "String After Concat is : " << str << endl;
}

void my_string::operator=(my_string str1)


{
strcpy(str1.str, str);
cout << "Copied String is : " << str1.str << endl;
}

int my_string::operator<=(my_string str1)


{
if (strcmp(str, str1.str) == 0) return 1;

return 0;
}

int my_string::str_length()
{
return strlen(str);
}

void my_string::toupper()
{
int l = strlen(str);
for (int i = 0; i <= l; i++)
{
if (str[i] >= 97 && str[i] <= 122)
{
str[i] = str[i] - 32;
}
}
cout << "UPPERCASE : " << str << endl;
}

void my_string::tolower ()
{
int l = strlen(str);
for (int i = 0; i <= l; i++)
{
if (str[i] >= 65 && str[i] <= 90)
{
str[i] = str[i] + 32;
}
}
cout << "LOWERCASE : " << str << endl;
}
int main()
{

int opt, c, opt1 = 1;

//object declaration my_string


a, b;

while (opt1 == 1 && opt != 7)


{

//displaying menu to the user


cout << "\n1. Concatenation\n2. Copy\n3. Comparision\n4.
Length\n5. Convert to lowercase\n6. Convert to Uppercase";
cout << "\n7.Exit\nEnter your choice : ";

cin >> opt; switch

(opt)
{
case 1: //concatenation
cout << "Enter the 1st string : "; a.getdata();
cout << "Enter the 2nd string : "; b.getdata();
a + b;
break;
case 2: //Copying
cout << "Enter the string : "; a.getdata();
a = b;
break;
case 3: //Comparision
cout << "Enter the 1st string : "; a.getdata();
cout << "Enter the 2nd string : "; b.getdata();
c = a <= b;
if (c == 1)
cout << "Strings are Equal" << endl;
else
cout << "Strings are not Equal" << endl;
break;
case 4: //Length
cout << "Enter the string : "; a.getdata();
cout << "The length of the string is :" << a.str_length() << endl;
;
break;
case 5: //To Uppercase
cout << "Enter the string : "; a.getdata();
a.tolower();
break;
case 6: // To lowercase
cout << "Enter the string : "; a.getdata();
a.toupper();
break;
case 7:

return 0;

default:
cout << "Invalid choice, Enter again : \n";
}
if (opt != 7)
{
cout << "Press 1 to continue : ";
cin >> opt1;
}
}
}

Output :
EXPERIMENT 18

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>
#include<stdlib.h>
#include<conio.h> using
namespace std;

struct node
{
int data; node
*next;
};

node *head=NULL,*tail=NULL;

class List
{
public: void
view()
{
node *n = head;
if(head==NULL)
{
cout<<"\n No elements found...";
}
else
{
cout<<" "; while(n!
=NULL)
{
if(n->next==NULL)
{
cout<<n->data;
}
else
{
cout<<n->data<<"->";
}
n = n->next;
}
}
}
virtual void store(int n)=0; virtual int
retrive()=0;
};

class Stack :public List


{
public:
void store(int n)
{
node *n1 = new node(); n1-
>data = n;
n1->next=NULL;
if((head==NULL)&&(tail==NULL))
{
head = n1; tail
= n1;
}
else
{
tail->next = n1; tail =
n1;
}
}
int retrive()
{
if((tail==NULL)&&(head==NULL))
{
return -1;
}
else
{
int n = tail->data; node *n1
= head;
while((n1->next!=tail)&&(head!=tail))
{
n1 = n1->next;
}
n1->next = NULL;
free(tail); if(head!=tail)
{
tail = n1;
}
else
{
tail=NULL;
head=NULL;
}
return n;
}
}
};

class Queue:public List


{
public:
void store(int n)
{
node *n1 = new node(); n1-
>data = n;
n1->next=NULL;
if((head==NULL)&&(tail==NULL))
{
head = n1; tail
= n1;
}
else
{
tail->next = n1; tail =
n1;
}
}
int retrive()
{
if((tail==NULL)&&(head==NULL))
{
return -1;
}
else
{
int n = head->data;
if(head==tail)
{
head = tail = NULL;
}
else
{
head = head->next;
}
return n;
}
}
};

int main()
{
Stack s1; int
ch; while(1)
{
cout<<"\n1. Stack";
cout<<"\n2. Queue";
cout<<"\n3. Exit";
cout<<"\nEnter your choice : "; cin>>ch;
if(ch==1)
{
Stack s1; int
ch1; while(1)
{
cout<<"\n\n1. Push Element"; cout<<"\n2.
Pop Element"; cout<<"\n3. View Stack";
cout<<"\n4. Exit";
cout<<"\nEnter your choice : "; cin>>ch1;
if(ch1==1)
{
int element;
cout<<"\nEnter the element you want to push : ";
cin>>element; s1.store(element);
cout<<"\nElement Pushed";
}
else if(ch1==2)
{
int element=0;
element = s1.retrive();
if(element==-1)
{
cout<<"\nStack is Empty";
}
else
{
cout<<"\nElement Popped = "<<element;
}
}
else if(ch1==3)
{
cout<<"\nElements in stack from bottom to top : ";
s1.view();
}
else if(ch1==4)
{
break;
}
else
{
cout<<"\nWrong choice";
}
getch();
}
}
else if(ch==2)
{
Queue q1; int
ch1; while(1)
{
cout<<"\n1. Push Element"; cout<<"\n2. Pop
Element"; cout<<"\n3. View Queue";
cout<<"\n4. Exit";
cout<<"\nEnter your choice - "; cin>>ch1;
if(ch1==1)
{
int element;
cout<<"\nEnter the element you want topush
: ";
cin>>element; q1.store(element);
cout<<"\nElement Pushed";
}
else if(ch1==2)
{
int element=0;
element = q1.retrive();
if(element==-1)
{
cout<<"\nQueue is Empty";
}
else
{
cout<<"\nElement Popped = "<<element;
}
}
else if(ch1==3)
{
cout<<"\nElements in queue from front to rear : ";
q1.view();
}
else if(ch1==4)
{
break;
}
else
{
cout<<"\nWrong choice";
}
getch();
}
}
else if(ch==3)
{
exit(0);
}
else
{
cout<<"\nWrong Choice";
}
getch();
}
return 0;

Output :
EXPERIMENT 19

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> using
namespace std; class
basic_info {

private:
char name[20]; char
sex;
int roll_no;

public:
void getdata();
void displaydata();
};

class physical_fit : public basic_info { private:

float height, weight; public:

void getdata();

void displaydata();
};

void basic_info:: getdata()


{
cout<<"Enter name : ";
cin>>name;
cout<<"Enter roll no : ";
cin>>roll_no;
cout<<"Enter sex : ";
cin>>sex;
}
void basic_info:: displaydata()
{
cout<<"Name : "<<name<<endl; cout<<"Roll no :
"<<roll_no<<endl; cout<<"Sex: "<<sex<<endl;
}

void physical_fit:: getdata()


{
basic_info::getdata(); cout<<"Enter
Height : "; cin>>height;
cout<<"Enter weight : ";
cin>>weight;
}

void physical_fit::displaydata()
{
basic_info::displaydata();
cout<<"Height is: "<<height<<endl;
cout<<"Weight is: "<<weight<<endl;
}

int main()
{
physical_fit obj1;
obj1.getdata();
cout<<"The data of student is : "<<endl; obj1.displaydata();
return 0;
}

Output :
EXPERIMENT 20

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 :

#include <iostream> using


namespace std;

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 21

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> using


namespace std; class student
{
private:
char name[25]; int
rno;

public:
void input()
{
cout << "Enter name : ";
cin >> name;
cout << "Enter roll no : ";
cin >> rno;
}
void output()
{
cout << "Name : " << name << endl;

cout << "Roll no. : " << rno << endl;


}
};
class exam : public student
{
private:
float marks[6];

public:
void input1(); float
add();
};
void exam::input1()
{
input();
for (int i = 0; i < 6; i++)
{
cout << "Enter marks in subject " << i + 1 << " : ";
cin >> marks[i];
}
}
float exam::add()
{
float sum = 0;
for (int i = 0; i < 6; i++)
{
sum = sum + marks[i];
}
return sum;
}
class result : public exam
{
private:
float total;

public:
void output1()
{
total = add();
output();
cout << "Total Marks : " << total << endl;
}
};
main()
{

result obj;
obj.input1();
obj.output1();
}

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> using


namespace std; class shape
{
protected:
double l, b;

public:
void getdata(double l1, double b1)
{
l = l1; b =
b1;
}
virtual void display_area() = 0;
};
class triangle : public shape
{
public:
void display_area()
{
double area;
area = 0.5 * l * b;
cout << "Area of triangle : " << area;
}
};
class rectangle : public shape
{
public:
void display_area()
{
double area; area =
l * b;
cout << "Area of rectangle : " << area;
}
};
int main()
{
shape *obj1, *obj2; double
a, b; triangle t;
obj1 = &t;
cout << "For Triangle : " << endl;
cout << "Enter base : ";
cin >> a;
cout << "Enter height : ";
cin >> b;
obj1->getdata(a, b);
obj1->display_area(); rectangle
r;
obj2 = &r;
cout << "\nFor Rectangle : " << endl;
cout << "Enter length : ";
cin >> a;
cout << "Enter breadth : ";
cin >> b;
obj2->getdata(a, b);
obj2->display_area();
;
}

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> using


namespace std; template
<class A> A square(A x)
{
A obj;
obj = x * x; return
obj;
}
main()
{

int i, ii; float a, aa;


double b, bb;
cout << "Enter integer value i : ";
cin >> i;
cout << "Enter float value a : ";
cin >> a;
cout << "Enter double value b : ";
cin >> b;
ii = square(i); aa =
square(a); bb =
square(b);
cout << "Square of " << i << " : " << ii << endl;
cout << "Square of " << a << " : " << aa << endl;
cout << "Square of " << b << " : " << bb << endl;
}

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 bigger of two entered numbers.

Source Code:

#include <iostream> using


namespace std; template <class
temp> class compare
{
private:
temp a, b;

public:
compare(temp x, temp y)
{
a = x; b
= y;
}
void display()
{
cout<<"The bigger number is : ";
if (a > b)
{
cout << a;
}
else
{
cout <<b;
}
}
~compare() {}
};
int main()
{
int a, b;
cout << "Enter 1st no. : ";
cin >> a;
cout << "Enter 2nd no. : ";
cin >> b;
compare<int> obj1(a, b);
obj1.display();
return 0;
}

Output :
EXPERIMENT 25

AIM : Write a program to define the function template for swapping two
items of various datatypes such as integers, float and characters.

Source Code:

#include <iostream> using


namespace std;

template <class T>


void Swap(T &n1, T &n2)
{
T temp; temp
= n1; n1 = n2;
n2 = temp;
}

int main()
{
int i1 = 7, i2 = 8;
float f1 = 3.4, f2 = 9.12;
char c1 = 'A', c2 = 'Z';

cout << "Before Swapping : \n";


cout << "i1 = " << i1 << "\t\ti2 = " << i2;
cout << "\nf1 = " << f1 << "\tf2 = " << f2;
cout << "\nc1 = " << c1 << "\t\tc2 = " << c2;

Swap(i1, i2);
Swap(f1, f2);
Swap(c1, c2);

cout << "\n\nAfter Swapping : \n";


cout << "i1 = " << i1 << "\t\ti2 = " << i2;
cout << "\nf1 = " << f1 << "\tf2 = " << f2;
cout << "\nc1 = " << c1 << "\t\tc2 = " << c2;
return 0;
}

Output :
EXPERIMENT 26

AIM : Write a program to illustrate how Template function can be


overloaded.

Source Code:

#include <iostream> using


namespace std; template <class
temp> void print(temp x)
{
cout << x << endl;
}
template <class temp>
void print(temp x, int n)
{
for (int i = 0; i < n; i++)
{
cout << x << endl;
}
}
int main()
{

int x;
int n, choice;
cout << "Enter value of x : ";
cin >> x;
cout << "1) Print x one time" << endl;
cout << "2) Print x n times" << endl;
cout << "Enter Choice : ";
cin >> choice; switch
(choice)
{
case 1:
print(x);
break;
case 2:
cout << "How many times to print x : ";
cin >> n;
print(x, n); break;
default:
cout << "Invalid Choice";
};
return 0;
}

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 to find their
sum.

Source Code:

#include <iostream> using


namespace std; template <class
temp> class sum
{
private:
temp a, b;

public:
sum(temp x, temp y)
{
a = x; b
= y;
}
void display()
{
cout << a << " + " << b << " = " << a + b;
}
};
int main()
{

int a, b;
;
cout << "Enter 1st no. : ";
cin >> a;
cout << "Enter 2nd no. : ";
cin >> b;
sum<int> obj1(a, b);
obj1.display();
return 0;
}

Output :
EXPERIMENT 28

AIM : Write a program to read a set of lines from the keyboard and to
store it on a specified file.

Source Code:

#include <fstream> #include


<iostream> using namespace
std;

int main()
{
int n;
char str1[100], str2[100];
cout << "Enter no. of lines : ";
cin >> n;
fstream outfile;
outfile.open("exp22.txt", ios::out | ios::binary);
cout << "Writing file....." << endl;
cin.get();

for (int i = 1; i <= n; i++)


{
cout << "Type something : "; gets(str1);
outfile.write((char *)&str1, sizeof(str1));
}
outfile.close(); fstream
infile;
infile.open("exp22.txt", ios::in | ios::binary);
cout << "Reading file.... " << endl;
while (infile.read((char *)&str2, sizeof(str2)))
{
puts(str2);
}
infile.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];
cout << "Enter no. of lines : ";
cin >> n;
fstream outfile; outfile.open("exp23.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(); 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];
cout << "Enter no. of lines to store : ";
cin >> n;
fstream outfile1; outfile1.open("myfile.txt", ios::out);
cout << "Writing to 1st file....." << endl;
cin.get();
for (int i = 0; i <n; i++)
{
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 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:

#include <iostream> #include


<fstream> using namespace
std;

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();

fstream fin("exp8in.txt");
fstream fout("exp8out.txt");
string s;
while (fin >> s)
{
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 32
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 on a specified 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 <fstream> #include


<iostream> using namespace
std; class student_info
{
private:

char name[20]; int


age;
char sex;
float ht, wt;

public:
void input()
{
cout << "Enter name : ";
cin >> name;
cout << "Enter age : ";
cin >> age;
cout << "Enter sex : ";
cin >> sex;
cout << "Enter height : ";
cin >> ht;
cout << "Enter weight : ";
cin >> wt;
}
void output()
{
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
cout << "Sex : " << sex << endl;
cout << "Height : " << ht << endl;
cout << "Weight : " << wt << endl;
}
};
int main()
{
student_info s1, s2; fstream
outfile;
outfile.open("exp9.txt", ios::out | ios::binary);

cout << "Writing file..." << endl; s1.input();


outfile.write((char *)&s1, sizeof(s1)); outfile.close();
fstream infile;
infile.open("exp9.txt", ios::in | ios::binary);
cout << "Reading from the file" << endl;
while (infile.read((char *)&s2, sizeof(s2)))
{
s2.output();
}
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> using


namespace std;

int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(int);
int i;
cout << "Size of array is : " << n << endl;
cout << "Which index you want to access : ";
cin >> i;
try
{
if (i >= n)
{
throw i;
}
else
{
cout << "Element at " << i << " is : " << arr[i];
}
}
catch (int)
{
cout << "Exception caught : Accessing index beyond of array size";
}

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> using


namespace std; int main()
{
int x, y;
cout << "Enter number 1 : ";
cin >> x;
cout << "Enter number 2 : ";
cin >> y;
try
{
if (y == 0)
{
throw y;
}
else
{
cout << "After Dividing : " << (x / y);
}
}
catch (int)
{
cout << "Exception Caught :Cannot divide by Zero";
}
return 0;
}

Output :

You might also like