Ex: No: 1
Matrix class using default argument, static data
members and friend function
Aim:
To write a C++ program to perform matrix
manipulation using static variable, default argument and friend function.
Algorithm:
Step 1: Declare the class as Matrix.
Step 2: Declare the data member as r,
c and
**x.
Step 3: Declare the member function as
Matrix(int r1=2,int c1=2);
void get();
void put();
friend Matrix add(Matrix,Matrix);
friend Matrix mul(Matrix,Matrix);
Step 4: Member function with default argument
is used to initialize the value of the
matrix.
Step 5: get() function is used to get the
values of two matrices.
Step 6: add() and mul() function are used to
perform addition and multiplication of the
matrices.
Step 7: In the main, create objects A and B
for the Matrix class.
Step 8: Call the get() method to get the
value of matrix A and B.
Step 9: Call the add() and mul() method to
perform the particular operation and finally
display the result.
Program:
#include
#include
#include
class matrix
{
static int r,c;
int**x;
public:
matrix(int r1=2,int c1=2);
void get();
void put();
friend matrix add(matrix,matrix);
friend matrix mul(matrix,matrix);
};
matrix::matrix(int r1,int c1)
{
r=r1;c=c1;
x=new int*[r];
for(int i=0;i
x[i]=new int[c];
for(i=0;i
for(int j=0;j
{
x[i][j]=0;
}
}
void matrix::get()
{
cout<<"\n enter the matrix of
size"<
for(int i=0;i
for(int j=0;j
cin>>x[i][j];
}
void matrix::put()
{
for(int i=0;i
for(int j=0;j
cout<
}
int matrix::r;
int matrix::c;
matrix add(matrix a,matrix b)
{
matrix c;
c.x[i][j]=a.x[i][j]+(b.x[i][j]);
return c;
}
matrix mul(matrix a,matrix b)
{
matrix c;
for(int j=0;j
{
c.x[i][j]=c.x[i][j]+a.x[i][k]*(b.x[k][j]);
}
return c;
}
void main()
{
clrscr();
matrix a,b,c1,d1;
a.get();
b.get();
cout<<"The matrix
A:"<
a.put();
cout<<"The matrix
B:"<
b.put();
c1=add(a,b);
cout<<"The resultant matrix
(A+B):"<
c1.put();
cout<<"\n\n The resultant
matrix(A*B):"<
d1=mul(a,b);
d1.put();
getch();
}
Result:-
Thus the C++ program to perform matrix
manipulation using static variable, default argument and friend function was
executed.
Complex Numbers with
Operator Overloading and Type Conversion
Aim:
To write a C++ program to
implement complex number class with necessary operator overloading and type
conversion
Algorithm:
Step 1: Start the program.
Step 2: Create a class
Complex.
Step 3: Define the default
and two parameterized constructors. One constructor is having two integer
arguments and another one will have the two double data type arguments
Step 4: Declare the operator
function which are going to be overloaded.
Step 5: Define the overloaded
functions such as +,-,*,/,>>,<<.
Step 6: Create the objects
and pass the complex values.
Step 7: Invoke the overloaded
functions.
Step 8: Display the converted
result.
Step 9: Stop the process.
Program:
#include
#include
#include
class complex
{
private:
float real;
float imag;
public:
complex()
{
real=imag=0.0;
}
complex(int r,int i) //conversion constructor
{
real = r;
imag = i;
}
complex(double r, double i)//conversion constructor
{
real = r;
imag = i;
}
#include
#include
class complex
{
private:
float real;
float imag;
public:
complex()
{
real=imag=0.0;
}
complex(int r,int i) //conversion constructor
{
real = r;
imag = i;
}
complex(double r, double i)//conversion constructor
{
real = r;
imag = i;
}
friend istream&
operator>>(istream &, complex &);
friend ostream& operator<<(ostream &, complex &);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
friend ostream& operator<<(ostream &, complex &);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
friend double condou(complex t);
//complex–>double
};
};
double condou(complex t)
{
return t.real+t.imag;
}
{
return t.real+t.imag;
}
istream& operator >>(istream
&in, complex &c)
{
cout<<”\nReal Part:”;
in>>c.real;
cout<<”Imag Part:”;
in>>c.imag;
return in;
}
{
cout<<”\nReal Part:”;
in>>c.real;
cout<<”Imag Part:”;
in>>c.imag;
return in;
}
ostream& operator<<(ostream
&out, complex &c)
{
if (c.imag<0 br=""> out<
else
out<
return out;
} 0>
{
if (c.imag<0 br=""> out<
out<
}
complex complex::operator+(complex c)
{
complex temp;
temp.real = real+c.real;
temp.imag = imag+c.imag;
return temp;
}
{
complex temp;
temp.real = real+c.real;
temp.imag = imag+c.imag;
return temp;
}
complex complex::operator-(complex c)
{
complex temp;
temp.real = real-c.real;
temp.imag = imag-c.imag;
return temp;
}
{
complex temp;
temp.real = real-c.real;
temp.imag = imag-c.imag;
return temp;
}
complex complex::operator*(complex c)
{
complex temp;
{
complex temp;
float t=c.real;
temp.real = real*c.real-imag*c.imag;
temp.imag = real*c.imag+imag*t;
return temp;
}
temp.real = real*c.real-imag*c.imag;
temp.imag = real*c.imag+imag*t;
return temp;
}
complex complex::operator/(complex c)
{
complex temp;
float qt;
{
complex temp;
float qt;
float res=(imag*c.real-real*c.imag);
qt = c.real*c.real+c.imag*c.imag;
temp.real = (real*c.real+imag*c.imag)/qt;
temp.imag = res/qt;
return temp;
}
qt = c.real*c.real+c.imag*c.imag;
temp.real = (real*c.real+imag*c.imag)/qt;
temp.imag = res/qt;
return temp;
}
void main()
{
complex c1, c2, c3,c4(4,9),c5(3.23004,4.666304444);
double t;
clrscr();
t=condou(c5);
cout<<”\nEnter complex number 1: “;
cin>>c1;
cout<<”\nEnter complex number 2: “;
cin>>c2;
cout<<”\nEnter complex numbers are:”;
cout<<”\nComplex 1: “<
cout<<”\nComplex 2: “<
c3=c1+c2;
cout<<”\nResult of addition is:”<
c3=c1-c2;
cout<<”\nResult of subtraction is:”<
c3=c1*c2;
cout<<”\nResult of multiplication is:”<
c3=c1/c2;
cout<<”\nResult of division is:”<
cout<<”\nInteger–>complex:”<
cout<<”\nDouble–>complex:”<
cout<<”\nConverted to double”<
{
complex c1, c2, c3,c4(4,9),c5(3.23004,4.666304444);
double t;
clrscr();
t=condou(c5);
cout<<”\nEnter complex number 1: “;
cin>>c1;
cout<<”\nEnter complex number 2: “;
cin>>c2;
cout<<”\nEnter complex numbers are:”;
cout<<”\nComplex 1: “<
cout<<”\nResult of addition is:”<
cout<<”\nResult of subtraction is:”<
cout<<”\nResult of multiplication is:”<
cout<<”\nResult of division is:”<
getch();
}
}
OUTPUT:
Enter complex number 1:
Real Part:2
Imag Part:4
Enter
complex number 2:
Real Part:3
Imag Part:5
Enter complex numbers are:
Complex 1: 2+4i
Complex 2: 3+5i
Result of addition is:5+9i
Result of subtraction
is:-1-1i
Result of multiplication
is:-14+22i
Result of division
is:0.764706+0.058824i
Integer->complex:4+9i
Double->complex:3.23004+4.666305i
Converted to double7.896345
Result:
Thus the program for operator
overloading for complex numbers and their type conversions was executed.
Matrix Class with
Constructor, Destructor, Copy constructor and Assignment operator overloading
Aim:
To implement Matrix class with dynamic memory allocation with constructors,
destructor, and overloading of assignment operator.
Algorithm:
Start the process.
Create a class matrix.
Declare and define default,
parameterized, copy constructor.
Define member functions
getmatrix and showmatrix.
Define destructor.
Get the size of the matrix
and the elements of the matrix.
Depends on the size of the
matrix, the memory is allotted for the matrix dynamically.
Get the elements of the
matrix.
Call the copy constructor and
copy the values of object m1 to m2.
With the help of assignment
operator, we are assigning the values of m1 to m3.
Display the result.
Stop the process.
Program :
#include
#include
class matrix
{
int **m;
int row, col;
public:
matrix()
{
row=col=0;
m=NULL;
}
matrix(int
r ,int c);
~matrix();
void
getmatrix();
void
showmatrix();
matrix(matrix &m2); //copy constructor
matrix& operator=(matrix &m2);
};
matrix::~matrix()
{
for(int i=0;i|
delete m[i];
delete
m;
}
matrix::matrix(int r ,int c)
{
row=r;
col=c;
m=new int*[row];
for(int i=0;i|
m[i]=new int[col];
}
matrix::matrix(matrix &m2)
{
cout<<"\nCopy
constructor invoked\n";
row=m2.row;
col=m2.col;
m=new int*[row];
for(int i=0;i<=row;i++)
m[i]=new int[col];
for(i=0;i<=row;i++)
for(int j=0;j<=row;j++)
m[i][j]=m2.m[i][j];
}
matrix&
matrix::operator=(matrix &m2)
{
cout<<"\nAssignment Operator Overloading\n";
row = m2.row;
col = m2.col;
m = new int*[row];
for(int i=0;i<=row;i++)
m[i]=new int[col];
for(i=0;i<=row;i++)
for(int j=0;j<=row;j++)
m[i][j]=m2.m[i][j];
return *this;
}
void matrix::getmatrix()
{
for(int i=0;i|
for(int j=0; j
cin>>m[i][j];
}
void matrix::showmatrix()
{
for(int i=0;i|
{
for(int j=0;j
cout<<"\t"<
cout<<"\n";
}
}
void main()
{
int r,c;
clrscr();
cout<<"\nEnter rows and columns of the matrix:\n";
cin>>r>>c;
matrix
m1(r,c);
cout<<"\nEnter the matrix elements one by
one:\n";
m1.getmatrix();
cout<<"\nEntered matrix is:\n";
m1.showmatrix();
//invoking copy constructor
matrix m2=m1;
cout<<"\nResult of the copy constructor is:\n";
m2.showmatrix();
matrix
m3;
m3=m1;
cout<<"\nResult of assignment operator
overloading:\n";
m3.showmatrix();
getch();
}
OUTPUT 1:
Enter rows and columns of the
matrix:
3
2
Enter the matrix elements one
by one:
2
3
1
4
5
7
Entered matrix is:
2 3
1 4
5 7
Copy constructor invoked
Result of the copy
constructor is:
2 3
1 4
5 7
Assignment Operator
Overloading
Result of assignment operator
overloading:
2 3
1 4
5 7
Output 2:
Enter rows and columns of the
matrix:
3 3
Enter the matrix elements one
by one:
12 23 34 45 56 67 78 89 90
Entered matrix is:
12 23 34
45 56 67
78 89 90
Copy constructor invoked
Result of the copy
constructor is:
12 23 34
45 56 67
78 89 90
Assignment Operator
Overloading
Result of assignment operator
overloading:
12 23 34
45 56 67
78 89 90
Result:
Thus the program for
constructor, destructor, copy constructor and assignment operator overloading
was executed.
OVERLOADING NEW AND DELETE
OPERATOR
AIM:
To write a C++ program to
implement the overloading of new and delete operators to provide dynamic
allocation of memory
ALGORITHM:
Step 1: Start the program.
Step 2: Create a class ‘op’;
Step 3: Declare the
overloaded member functions new & delete.
Step 4: Invoke the base class
constructor and passing arguments for the new function.
Step 5: The inbuilt
identifiers __FILE__ and __LINE__ will get the current file which under processing
and the line which currently executing the __LINE__ will be passed.
Step 6: Using malloc the
memory will be allocated for the current processing file.
Step 7: Invoke the overloaded
operated function delete and pass the pointer variable free the memory which is
already allocated.
Step 8: Stop the program.
PROGRAM:
#include
#include
#include
class op
{
public:
void *operator new(size_t size, char const *file,int line);
void operator delete(void *p);
};
void *op::operator new(size_t
size, char const *file,int line)
{
void *p = malloc(size);
cout<<"\n
New called the file:"<
return p;
}
void op::operator delete(void
*p)
{
cout<<"\n
Delete called p:"<
free(p);
}
void main()
{
clrscr();
op *X =
new(__FILE__,__LINE__)op;
delete X;
getch();
}
OUTPUT:
New called the
file:NEWDEL1.CPP
line:24
size:1
p:0x8fba0df6
Delete called
p:0x8fba0df6
RESULT:
Thus the C++ program to implement
the overloading of new and delete operators to provide dynamic allocation of
memory was executed.
/* Template : Linked List Class*/
Aim:
To write a C++ program to implement Template of Linked list class
Program:-
#include
#include
#include
template
struct
node
{
type
data;
node*
next;
};
template
class
list
{
public:
list();
int
length(void) const;
void
makeempty(void);
void
insert(void);
void
remove(void);
void
display(void);
private:
node*
linklist;
int
count;
};
template
void
list::display(void)
{
node*
cur = linklist;
cout<<”\nThe
linked list is…\n”;
while(cur!=NULL)
{
cout<data<<”->”;
cur=cur->next;
}
cout<<”NULL\n”;
}
template
list::list()
{
count=0;
linklist=NULL;
}
template
int
list::length(void) const
{
return
count;
}
/*
G.Roy Antony Arnold – Infant Jesus */
template
void
list::makeempty(void)
{
node*
temp;
while(linklist
!=NULL)
{
temp=linklist;
linklist=linklist->next;
delete
temp;
}
count=0;
cout<<”\nNow
List is empty…”;
}
template
void
list::insert(void)
{
node*
temp;
type
item;
cout<<”\nEnter
the item to insert…”;
cin>>item;
temp=new
node;
temp->data
= item;
temp->next
= linklist;
linklist=temp;
count++;
}
template
void
list::remove(void)
{
node*
cur = linklist;
type
item;
cout<<”\nEnter
the item to remove…”;
cin>>item;
node*
temp;
if(item==linklist->data)
{
temp
= cur;
linklist=linklist->next;
}
else
{
while(!(item==(cur->next->data)))
cur=cur->next;
temp
= cur->next;
cur->next
= (cur->next)->next;
}
delete
temp;
count–;
}
/*
G.Roy Antony Arnold – Infant Jesus */
void
main()
{
int
ch;
list
list1;
clrscr();
while(1)
{
cout<<”\n
Single Linked List – Menu\n”;
cout<<”\n
1.Insert \n2.Delete\n 3.Empty\n 4.Exit\n”;
cout<<”\nEnter
your Choice… “;
cin>>ch;
switch(ch)
{
case
1:
list1.insert();
list1.display();
break;
case
2:
if(list1.length()>0)
{
list1.remove();
list1.display();
}
else
cout<<”\nList
Empty”;
break;
case
3:
list1.makeempty();
break;
case
4:
exit(0);
default:
cout<<”\nInvalid
Choice\n”;
}
}
}
Result:
-
Thus the C++ program to implement Template of Linked list class
was executed.
Ex.No: 6A Bubble sort using class template
Aim:
To write a C++ program for bubble sort using
template.
Algorithm:
Step 1: Specify the template declaration and
create a class as bubble.
Step 2: Declare the data members as size
and
*v as template variable.
Step 3: Allocate the memory space using
default constructor.
Step 4: Destroy the memory space using
destructor.
Step 5: Declare the member functions as
void read()
void sort()
void display()
Step 6: read() function is used to get the
elements.
Step 7: sort() function is used to sort the
elements.
7.1 Use the for loop to continue the
iteration.
7.2 if statement is used to compare the
element, if it is true, swap the elements.
Step 8: display() function is used to display
the element.
Step 9: In the main, create the object using
the following syntax:
classnameobject
Step10: Call the read() function, to get the
elements.
Step11: Call the sort() function, to sort the
elements.
Step12: Call the display() function, to
display the sorted elements.
Program:
#include
#include
template
class bub
{
T *v;
int s;
public:
bub(int x)
{
s=x;
v=new T[s];
}
void read();
void sort();
void display();
~bub()
{
delete v;
}
};
template
void bubinsert::read()
{
for(int i=0;i
{
cin>>v[i];
}
}
template
void bub::display()
{
for(int i=0;i
{
cout<
}
cout<<"\n";
}
template
void bub::sort()
{
for(int i=0;i
{
for(int j=0;j>=s-1;j++)
if(v[j]>v[j+1])
{
T t;
T=v[j];
v[j]=v[j+1];
v[j+1]=t;
}
}
}
void main()
{
clrscr();
int r;
cout<<"\n\nEnter the size:";
cin>>r;
bubI(r);
cout<<"\nEnter the Integer
Elements:";
I.read();
I.sort();
bubI1(r);
cout<<"\n\nEnter the Float
Elements:";
I1.read();
I1.sort();
bubI2(r);
cout<<"\n\nEnter the Character
Elements:";
I2.read();
I2.sort();
getch();
}
Output:
Enter the size:5
Enter the Integer Elements:23 12 11 45 1
12 23 11 45 1
11 12 23 45 1
11 12 23 45 1
1 11 12 23 45
Enter the Float Elements: 2.3 1.2 1.1 4.5 1.0
1.2 2.3 1.1 4.5 1
1.1 1.2 2.3 4.5 1
1.1 1.2 2.3 4.5 1
1 1.1 1.2 2.3 4.5
Enter the Character Elements :r w a t b
r w a t b
a r w t b
a r t w b
a b r t w
Result: -
Thus the C++ program for bubble sort using
template was executed.
Ex.No: 6B Insertion sort using class template
Aim:
To write a C++ program for insertion sort
using template.
Algorithm:
Step 1: Specify the template declaration and
create a class as insertion.
Step 2: Declare the data members as size
and
*v as template variable.
Step 3: Allocate the memory space using
default constructor.
Step 4: Destroy the memory space using
destructor.
Step 5: Declare the member functions as
void read()
void sort()
void display()
Step 6: read() function is used to get the
elements.
Step 7: sort() function is used to sort the
elements.
7.1 Use the for loop to continue the
iteration.
7.2 if statement is used to compare the
element, if it is true, swap the elements.
Step 8: display() function is used to display
the element.
Step 9: In the main, create the object using
the following syntax:
classnameobject
Step10: Call the read() function, to get the
elements.
Step11: Call the sort() function, to sort the
elements.
Step12: Call the display() function, to
display the sorted elements.
Program:
#include
#include
template
class insert
{
T *v;
int s;
public:
insert(int x)
{
s=x;
v=new T[s];
}
void read();
void sort();
void display();
~insert()
{
delete v;
}
};
template
void insert::read()
{
for(int i=0;i
{
cin>>v[i];
}
}
template
void insert::display()
{
for(int i=0;i
{
cout<
}
cout<<"\n";
}
template
void insert::sort()
{
for(int i=1;i
{
T t=v[i];
for(int j=i-1;j>=0 &&
t
v[j+1]=v[j];
v[j+1]=t;
display();
}
}
void main()
{
clrscr();
int r;
cout<<"\n\nEnter the size:";
cin>>r;
insertI(r);
cout<<"\nEnter the Integer
Elements:";
I.read();
I.sort();
insertI1(r);
cout<<"\n\nEnter the Float
Elements:";
I1.read();
I1.sort();
insertI2(r);
cout<<"\n\nEnter the Character
Elements:";
I2.read();
I2.sort();
getch();
}
Output:
Enter the size:5
Enter the Integer Elements:23 12 11 45 1
12 23 11 45 1
11 12 23 45 1
11 12 23 45 1
1 11 12 23 45
Enter the Float Elements: 2.3 1.2 1.1 4.5 1.0
1.2 2.3 1.1 4.5 1
1.1 1.2 2.3 4.5 1
1.1 1.2 2.3 4.5 1
1 1.1 1.2 2.3 4.5
Enter the Character Elements :r w a t b
r w a t b
a r w t b
a r t w b
a b r t w
Result: -
Thus the C++ program for insertion sort using
template was executed.
Ex.No: 6C Merge sort using class template
Aim:
To write a C++ program for merge sort using
template.
Algorithm:
Step 1: Specify the template declaration and
create a class as sort.
Step 2: Declare the data members as size
and
*v as template variable.
Step 3: Allocate the memory space using
default constructor.
Step 4: Destroy the memory space using
destructor.
Step 5: Declare the member functions as
void get()
void mergesort()
void merge()
void show()
Step 6: get() function is used to get the
element.
Step 7: merge() sort function is used to
divide the given element in equal parts .
Step 8: show() function is used to display
the element.
Step 9: merge() function is used to perform
sort operation.
Step10: In the main, create the object using
the following syntax:
classnameobject
Step11: Call the get() function, to input the
elements.
Step12: Call the mergesort() and merge()
function to perform divide and conquer
operation.
Step13: Call the show function to display the
sorted elements.
Program:
#include
#include
template
class Sort
{
private:
int n,l,r;
T *Array;
T *b;
public:
Sort();
void get();
void callMerge();
void mergeSort(T a[],int l,int r);
void merge(T c[],T d[],int l,int m,int r);
void copy(T b[],T a[],int l,int r);
void show();
~Sort();
};
template
Sort::Sort()
{
cout<<"\nEnter the size of the
array:";
cin>>n;
Array=new T[n];
b=new T[n];
l=0;
r=n-1;
}
template
void Sort::get()
{
cout<<"\nEnter the
Elements:\n";
for(int i=0;i
cin>>Array[i];
}
template
void Sort::callMerge()
{
(*this).mergeSort(Array,l,r);
}
template
void Sort::mergeSort(T a[],int l,int
r)
{
if(l
{
int i=(l+r)/2;
mergeSort(a,l,i);
mergeSort(a,i+1,r);
merge(a,b,l,i,r);
copy(b,a,l,r);
}
}
template
void Sort::merge(T c[],T d[],int
l,int m,int r)
{
int i=l,j=m+1,k=l;
while((i<=m) && (j<=r))
if(c[i]<=c[j])
d[k++]=c[i++];
else
d[k++]=c[j++];
if(i>m)
for(int q=j;q<=r;q++)
d[k++]=c[q];
else
for(int q=i;q<=m;q++)
d[k++]=c[q];
}
template
void Sort::copy(T b[],T a[],int
l,int r)
{
for(int i=l;i<=r;i++)
a[i]=b[i];
}
template
void Sort::show()
{
cout<<"\nThe Elements in the
Sorted Array:\t";
for(int
i=0;i
cout<
}
template
Sort::~Sort()
{
delete b;
delete Array;
}
void main()
{
clrscr();
cout<<"\n\t\t\t************\n";
cout<<"\n\t\t\tInteger
Sort\n";
cout<<"\n\t\t\t************\n";
Sort obj;
obj.get();
obj.callMerge();
obj.show();
cout<<"\n";
cout<<"\n\t\t\t**********\n";
cout<<"\n\t\t\tFloat Sort\n
";
cout<<"\n\t\t\t**********\n";
Sort obj2;
obj2.get();
obj2.callMerge();
obj2.show();
cout<<"\n";
cout<<"\n\t\t\t************\n";
cout<<"\n\t\t\tCharater Sort\n
";
cout<<"\n\t\t\t************\n";
Sort obj3;
obj3.get();
obj3.callMerge();
obj3.show();
getch();
}
Output:
************
Integer Sort
************
Enter the size of the array:5
Enter the Elements:
23
11
1
67
2
The Elements in the Sorted Array: 1 2 11 23
67
**********
Float Sort
**********
Enter the size of the array:5
Enter the Elements:
2.3
1.1
6.7
4.5
9.9
The Elements in the Sorted Array: 1.1 2.3 4.5
6.7 9.9
***********
Charater Sort
***********
Enter the size of the array:5
Enter the Elements:
w
q
a
s
b
The Elements in the Sorted Array: a b q s w
Result: -
Thus the C++ program for Merge sort using
template was executed.
Ex.No: 6D Quick sort using class template
Aim:
To write a C++ program for quick sort using
template.
Algorithm:
Step 1: Specify the template declaration and
create a class as sort.
Step 2: Declare the data members n, lb
and
ub and *a as a template variable.
Step 3: Allocate the memory space using
default constructor.
Step 4: Destroy the memory space using
destructor.
Step 5: Declare the member functions as
void get()
void callquick()
void quick()
void show()
Step 6: get() function is used to get the
element.
Step 7: callquick() function is used to call
the quick function .
Step 8: quick() function is used to perform
sort operation by using pivot element .
Step 9: In the main, create the object using
the following syntax:
classnameobject
Step10: Call the get() function, to input the
elements.
Step11: Call the callquick() and quick()
functions to perform sort operation.
Step12: Call the show() function to display
the sorted elements.
Program:
#include
#include
#include
template
class Sort
{
private:
int n,lb,ub;
T *a;
public:
void get();
void callquick();
void quick(int lb,int ub);
void show();
~Sort();
};
template
void Sort::get()
{
cout<<"\nEnter the size of the
array:";
cin>>n;
a=new T[n];
cout<<"\nEnter the
Elements:";
for(int i=0;i
cin>>a[i];
lb=0;
ub=n-1;
}
template
void Sort::callquick()
{
(*this).quick(lb,ub);
}
template
void Sort::quick(int lb,int ub)
{
T temp=0;
T flag=1,i=0,j=0,key=0;
if(lb
{
i=lb;
j=ub+1;
key=a[lb];
while(flag==1)
{
i=i+1;
while(a[i]
i=i+1;
j=j-1;
while(a[j]>key)
j=j-1;
if(i
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
flag=0;
}
temp=a[lb];
a[lb]=a[j];a[j]=temp;
show();
quick(lb,j-1);
quick(j+1,ub);
}
}
template
void Sort::show()
{
cout<<"\nThe Elements in the
Array:\t";
for(int
i=0;i
}
template
Sort::~Sort()
{
delete a;
}
void main()
{
clrscr();
Sortobj;
Sortobj2;
Sortobj3;
int w;
do
{
cout<<"\n\t\t***************\n";
cout<<"\n\t\t QUICK SORT\n";
cout<<"\n\t\t***************\n";
cout<<"\n\t\t1.Integer
Sort\n\t\t2.Float Sort\n\t\t3.Character Sort\n\t\t4.Exit\n";
cout<<"\n\t\tEnter Ur
choice:";
cin>>w;
switch(w)
{
case 1:
obj.get();
obj.callquick();
break;
case 2:
obj2.get();
obj2.callquick();
break;
case 3:
obj3.get();
obj3.callquick();
break;
case 4:
exit(0);
}
}while(w!=4);
getch();
}
Output:
***************
QUICK SORT
***************
1.Integer Sort
2.Float Sort
3.Character Sort
4.Exit
Enter Ur choice:1
Enter the size of the array:5
Enter the Elements:23 45 11 78 1
The Elements in the Array: 11 1 23 78 45
The Elements in the Array: 1 11 23 78 45
The Elements in the Array: 1 11 23 45 78
***************
QUICK SORT
***************
1.Integer Sort
2.Float Sort
3.Character Sort
4.Exit
Enter Ur choice:3
Enter the size of the array:5
Enter the Elements:r s k a q
The Elements in the Array: a q k r s
The Elements in the Array: a q k r s
The Elements in the Array: a k q r s
***************
QUICK SORT
***************
1.Integer Sort
2.Float Sort
3.Character
Sort
4.Exit
Enter Ur choice:2
Enter the size of the array:5
Enter the Elements:2.2 4.5 1.1 7.8 0.1
The Elements in the Array: 1.1 0.1 2.2 7.8
4.5
The Elements in the Array: 0.1 1.1 2.2 7.8
4.5
The Elements in the Array: 0.1 1.1 2.2 4.5
7.8
***************
QUICK SORT
***************
1.Integer Sort
2.Float Sort
3.Character Sort
4.Exit
Enter Ur choice:4
Result: -
Thus the C++ program for quick sort using
template was executed.
IMPLEMENTATION
OF QUEUE USING EXCEPTION HANDLING
AIM:
To implement
a Queue using exception handling using C++.
ALGORITHM:
Step 1:
Start the process.
Step 2:
Create a class queue.
Step 3:
Initialize the front and rear pointers of the queue.
Step 4:
Create the members full, empty, insert, dele and display.
Step 5:
Inside the insert function check if rear = max. If rear is not equal to max
then add the element to the queue. If rear reaches the max then throw the
message “Queue Overflow”.
Step 6:
Inside the dele function, check if front = rear. If front is not equal to rear
then deletion is possible. If front is equal to the rear, throw the message
“Queue Underflow”.
Step 7:
Display the queue.
Step 8: Stop
the process.
PROGRAM:
#include
#include
using
namespace std;
class queue
{
private:
int *q;
int max,
front, rear;
int cnt;
public:
class
FULL{}; //for exception handling
class
EMPTY{}; //for exception handling
queue(int);
void
insert(int);
int
dele(void);
void
display(void);
};
queue::queue(int
m)
{
q=new
int[m];
rear=0;
front=0;
cnt=0;
max=m;
}
void
queue::insert(int item)
{
if(cnt
{
front =
front % max;
q[front++]=item;
cnt++;
}
else
throw
FULL(); //FULL object is thrown
}
int
queue::dele(void)
{
if(cnt>0)
{
cnt--;
rear = rear
% max;
return
q[rear++];
}
else
throw
EMPTY(); //EMPTY object is thrown
}
void
queue::display(void)
{
if(cnt>0)
for(int
i=0,j=front; i
cout<<"|"<
else
throw
EMPTY();
}
int main()
{
int item,
size;
int ch=1;
cout<<"\nEnter
the size of the queue…";
cin>>size;
queue
q(size);
cout<<"\nQueue
Operations using Exception Handling";
cout<<"\n\n\tMENU\n1.INSERT\n2.DELETE\n
3.SHOW QUEUE\n4.EXIT";
cout<<"\nEnter
your choice…";
cin>>ch;
do
{
switch(ch)
{
case 1:
cout<<"\nEnter
the item to insert in to the queue…";
cin>>item;
try
{
q.insert(item);
}
catch(queue::FULL)
//FULL object is caught
{
cout<<"\n***Queue
Full***\n";
}
break;
case 2:
try
{
cout<<"\nRemoved
Item from the Qis…"<
}
catch(queue::EMPTY)
//EMPTY object is caught
{
cout<<"\n***Queue
Empty***\n";
}
break;
case 3:
cout<<"\nThe
Queue is…\n";
try
{
q.display();
}
catch(queue::EMPTY)
{
cout<<"\n***Queue
Empty***\n";
}
break;
case 4:
exit(0);
}
cout<<"\nEnter
your choice…";
cin>>ch;
}while(ch<5 span="">5>
return 0;
}
OUTPUT:
[ex@localhost
~]$ g++ q2.cpp
[ex@localhost
~]$ ./a.out
Enter the
size of the queue…2
Queue
Operations using Exception Handling
MENU
1.INSERT
2.DELETE
3.SHOW
QUEUE
4.EXIT
Enter your
choice…1
Enter the
item to insert in to the queue…12
Enter your
choice…1
Enter the
item to insert in to the queue…23
Enter your
choice…1
Enter the
item to insert in to the queue…34
***Queue
Full***
Enter your
choice…3
The Queue
is…
|12||23|
Enter your
choice…2
Removed Item
from the Qis…12
Enter your
choice…2
Removed Item
from the Qis…23
Enter your
choice…2
***Queue
Empty***
Enter your
choice…3
The Queue
is…
***Queue
Empty***
Enter your
choice…4
RESULT:
Thus the
program to implement the queue using exception handling was executed.
Ex.No: 7A Implementation of Stack
Aim:
To write a C++ program to implement stack
using array.
Algorithm:
Step 1: Create a class as stk.
Step 2: Declare the data members as x,
top, stack[10].
Step 3: Declare the member function as
stk()
void push()
void pop()
void display()
Step 4: Default constructor is used to
initialize the value to zero.
Step5: In the push function,
5.1 Check the stack for overflow.
5.2 Increment the top value.
5.3 Read the value to be pushed to the stack.
5.4 Push the value into the stack.
5.5 Display the pushed value.
Step 6: In the pop function,
6.1 Check the stack for underflow.
6.2 Pop the value from the stack.
6.3 Decrement the top value.
6.4 Display the value that is popped from the
stack.
Step 7: display () function is used to display
the stack content.
Step 8: In the main, create the object for
the class stk.
Step 9: In the do-while loop, it display the
choice as push, pop and display.
9.1 If the choice is 1, call push function.
9.2 If the choice is 2, call pop function.
9.3 If the choice is 3, call display
function.
Program:
#include
#include
class stk
{
int x,top,stack[10];
public:
stk()
{
top=0;
}
void push();
void pop();
void display();
};
void stk::push()
{
if(top==10)
cout<<"\nSTACK FULL";
else
top=top+1;
cout<<"\nENTER THE NUMBER TO BE
PUSHED:";
cin>>x;
stack[top]=x;
cout<<"\nTHE VALUE IS PUSHED INTO
THE STACK IS:"<
}
void stk::pop()
{
if(top==0)
cout<<"\nSTACK EMPTY\n:";
else
{
x=stack[top];
top=top-1;
cout<<"\nTHE VALUE RETREIVED FROM
STACK IS:"<
}
}
void stk::display()
{
cout<<"\nCONTENTS OF STACK:";
for(int i=top;i>0;i--)
{
cout<<"\n"<
}
}
void main()
{
clrscr();
stk s;
int a;
cout<<"\n\t\t\t*****\n";
cout<<"\n\t\t\tSTACK\n";
cout<<"\n\t\t\t*****\n";
do
{
cout<<"\n\n\t\t1.PUSH \n\t\t2.POP
\n\t\t3.DISPLAY
\n\t\t4.EXIT";
cout<<"\n\t\tENTER THE
CHOICE:";
cin>>a;
switch(a)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
default:
cout<<"\nINVALID CHOICE";
}
} while(a<4 span="">4>
getch();
}
Output:
*****
STACK
*****
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:1
ENTER THE NUMBER TO BE PUSHED:67
THE VALUE IS PUSHED INTO THE STACK IS:67
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:1
ENTER THE NUMBER TO BE PUSHED:89
THE VALUE IS PUSHED INTO THE STACK IS:89
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:3
CONTENTS OF STACK:
89
67
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:2
THE VALUE RETREIVED FROM STACK IS:89
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:3
CONTENTS OF STACK: 67
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:4
INVALID CHOICE
IMPLEMENTATION
OF STACK USING EXCEPTION HANDLING
AIM:
To implement
a Stack using exception handling using C++.
ALGORITHM:
Step 1:
Start the process.
Step 2:
Create a Class Stack.
Step 3:
Initialize the top pointer of the stack.
Step 4:
Create class full and empty.
Step 5:
Create the members push, pop and display.
Step 6:
Inside the push function check the top of the stack for overflow. If the top is
not equal to the size (n-1) then push the elements onto the stack and increment
the top pointer. If the top reaches the maximum size of n then throw the
elements on to the stack overflow.
Step 7:
Inside the pop function check the stack underflow condition. If the stack is
empty, top is equal to zero then deletion cannot be performed, throws a stack
underflow exception. Otherwise pop the elements and decrement the pointer.
Step 8:
Display the stack elements.
Step 9: Stop
the process.
PROGRAM:
#include
#include
class stack
{
private:
int *s; int
max; int top;
public:
class
full{};
class empty{};
stack(int);
void
push(int);
int
pop(void);void display(void);
};
stack::stack(int
m)
{
s=new
int[m];
top = -1;
max=m;
}
void
stack::push(int item)
{
if(top
s[++top]=item;
else throw
full();
}
int
stack::pop(void)
{
if(top>=0)
return
s[top--];
else
throw
empty();
}
void
stack::display(void)
{
if(top>=0)
for(int i =
top; i>=0;i--)
cout<<"\n
\t |"<
else
throw
empty();
}
int main()
{
int
item,size;
int ch = 1;
cout<<"\n
Enter the size of the Stack:";
cin>>size;
stack s1(size);
cout<<"\n
Stack with exception handling";
cout<<"\n
\n MENU \n 1.PUSH \n 2.POP \n3.SHOW THE STACK \n4.EXIT";
cout<<"\n
Enter your choice:";
cin>>ch;
do
{
switch(ch)
{
case 1:
cout<<"\n
Enter the item to push:";
cin>>item;
try
{
s1.push(item);
}
catch(stack::full)
{
cout<<"\n
Stack Overflow";
}
break;
case 2:
try
{
cout<<"The
Popped element is:";
cout<
}
catch(stack::empty)
{
cout<<"\n
Stack is empty";
}
break;
case 3:
cout<<"The
stack is....";
try
{
s1.display();
}
catch(stack::empty)
{
{
cout<<"\n
Stack empty";
}
break;
case 4:
exit(0);
}
cout<<"\n
Enter your choice:\t";
cin>>ch;
}while(ch<5 span="">5>
return 0;
}
OUTPUT:
[ex@localhost
~]$ vi stack.cpp
[ex@localhost
~]$ g++ stack.cpp
[ex@localhost
~]$ ./a.out
Enter
the size of the Stack:2
Stack
with exception handling
MENU
1.PUSH
2.POP
3.SHOW THE
STACK
4.EXIT
Enter
your choice:1
Enter
the item to push:12
Enter
your choice: 1
Enter
the item to push:34
Enter
your choice: 1
Enter
the item to push:23
Stack
Overflow
Enter
your choice: 2
The Popped
element is:34
Enter
your choice: 3
The stack
is....
| 12|
-----
Enter your
choice: 4
RESULT:
Thus the
program to implement the stack using exception handling was executed.
Ex.No: 7B Implementation of Queue
Aim:
To write a C++ program to implement queue
using array.
Algorithm:
Step 1: Create a class as stk.
Step 2: Declare the data members as x,
front, q[10], rear and result.
Step 3: Declare the member functions as
queue()
void enq()
void dque()
void disp()
Step 4: Default constructor is used to
initialize the front and rear value to zero.
Step5: In the enqueue function,
5.1 check the queue for overflow.
5.2 Read the value to be enqueued.
5.3 Increment the rear value.
5.4 Enqueue the value into the queue.
5.5 Display the enqueued value.
Step 6: In the dequeue function,
6.1 Check the queue for underflow.
6.2 Dequeue the value from the queue.
6.3 Increment the front.
6.4 Display the value that is dequeued from
the queue.
Step 7: disp() function is used to display
the queue content.
Step 8: In the main, create the object for
the class stk.
Step 9: In the do-while loop, it display the
choice as enqueue, dequeue and display.
9.1 If the choice is 1, call enqueue
function.
9.2 If the choice is 2, call dequeue
function.
9.3 If the choice is 3, call display
function.
Program:
#include
#include
class queue
{
public:
int q[5],front,rear,x,result;
void enq();
void dque();
void disp();
queue()
{
front=0;
rear=0;
}
};
void queue::enq()
{
if(rear>=5)
cout<<"\nQueue overflow!!\n";
else
{
cout<<"\nEnter the number to be
inserted: ";
cin>>x;
rear++;
q[rear]=x;
cout<<"\nNumber pushed in the
queue:"<
}
}
void queue::dque()
{
if(rear==0)
cout<<"\nQueue
underflow!!\n";
else
{
if(front==rear)
{
front=0;
rear=0;
}
else
front++;
}
cout<<"\nDeleted element
is:";
result=q[front];
cout<
}
void queue::disp()
{
if(rear==0)
cout<<"\nQueue
underflow!!\n";
else
cout<<"\nContents of queue
is:";
for(int i=front+1;i<=rear;i++)
cout<
}
void main()
{
int c;
queue qu;
clrscr();
cout<<"\n\t\t\t*****\n";
cout<<"\n\t\t\tQUEUE\n";
cout<<"\n\t\t\t*****\n";
do
{
cout<<"\n\n\n\t\t\t1.Insertion\n\t\t\t2.Deletion\n\t\t\t3.Display\n";
cout<<"\n\t\tEnter your
choice:";
cin>>c;
switch(c)
{
case 1:
qu.enq();
break;
case 2:
qu.dque();
break;
case 3:
qu.disp();
break;
default:
cout<<"\nInvalid choice!!\n";
}
}
while(c<4 span="">4>
getch();
}
OUTPUT:
*****
QUEUE
*****
1.Insertion
2.Deletion
3.Display
Enter your choice:1
Enter the number to be inserted: 78
Number pushed in the queue:78
1.Insertion
2.Deletion
3.Display
Enter your choice:1
Enter the number to be inserted: 90
Number pushed in the queue:90
1.Insertion
2.Deletion
3.Display
Enter your choice:3
Contents of queue is:78 90
1.Insertion
2.Deletion
3.Display
Enter your choice:2
Deleted element is:78
1.Insertion
2.Deletion
3.Display
Enter your choice:3
Contents of queue is:90
1.Insertion
2.Deletion
3.Display
Enter your choice:4
Invalid choice!!
MINIMUM SPANNING TREE
AIM:
To find the minimum cost
spanning tree in a graph using Prims algorithm.
ALGORITHM:
Step 1 : Start the process.
Step 2: Create two classes as POINT and ARC.
Step 3: Define a Graph class which represents the collection of Point and
Arc objects.
Step 4: Write a method to find a minimum cost spanning tree in a graph.
Step 5: Enter the Number of nodes,
source node, destination node, weight for the corresponding edge.
Step6: Repeat step5 until all
vertices, edges and weight are defined.
Step 7: The edge does not
form a cycle in the graph, for Minimum Spanning Tree.
Step 8: Thus set of edges is
connected and form a Minimum Spanning Tree.
Step 9: Display the adjacency
matrix for the graph and the minimum total path of all the edges.
Step 10: Stop the process.
PROGRAM:
#include
#include
#define MAX 50
#define TRUE 1
#define FALSE 0
#define MAXINT 250
class node
{
public:
int no;
node()
{}
node (int a)
{
no = a;
}
};
class arc
{
public:
int adj;
int weight;
arc()
{}
arc(int a)
{
adj = a;
}
};
class graph
{
public:
node nodes[MAX];
arc arcs[MAX][MAX];
graph(int n)
{
for(int i=1;i<=n;i++)
{
nodes[i].no = 0;
for(int j=1;j<=n;j++)
arcs[i][j].adj = FALSE;
}
}
void join(node n1, node n2,
int w)
{
arcs[n1.no][n2.no].adj = w;
arcs[n2.no][n1.no].adj = w;
}
void displayadj(int n)
{
cout<<"\n The
adjacency matrix....";
cout<
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=n;j++)
cout<<"\t"<
cout<
}
cout<
}
void shortpath(int n)
{
int lcost[20];
int clost[20],i,j,k,min;
for(i = 2; i<=n;i++)
{
lcost[i]=arcs[1][i].adj;
clost[i]=1;
}
cout<<"\n Minimum
cost spanning tree edges are:\n";
for(i=2;i<=n;++i)
{
min=lcost[2];
k=2;
for(j=3;j<=n;++j)
if(lcost[j]
{
min = lcost[j];
k=j;
}
cout<<"\n"<"<
lcost[k]=MAXINT;
for(j=2;j<=n;++j)
if((arcs[k][j].adj
{
lcost[j]=arcs[k][j].adj;
clost[j]=k;
}
}
}
};
int main()
{
int n;
clrscr();
cout<<"\n Enter
total number of nodes...";
cin>>n;
graph g(n);
cout<<"\n Assigning
number for each node...";
for(int i = 1;i<=n;i++)
g.nodes[i].no=i;
char ch ='y';
int w;
do
{
node a,b;
cout<<"\n Create
path b/w the nodes";
cout<<"\n Enter
the source node...";
cin>>a.no;
cout<<"\n Enter
the destination node...";
cin >>b.no;
cout<<"\n Enter
the weight:";
cin>>w;
g.join(a,b,w);
cout<<"\n Want to
continue...[y]es or [n]:";
cin>>ch;
}while(ch=='y');
g.displayadj(n);
g.shortpath(n);
cin>>n;
getch();
return 0;
}
OUTPUT:
Enter
total number of nodes...4
Assigning number for
each node...
Create path b/w the
nodes
Enter the source
node...1
Enter the destination
node...2
Enter the weight:1
Want to
continue...[y]es or [n]:y
Create path b/w the
nodes
Enter the source
node...1
Enter the destination
node...4
Enter the weight:2
Want to continue...[y]es
or [n]:y
Create path b/w the
nodes
Enter the source
node...1
Enter the destination
node...3
Enter the weight:2
Want to
continue...[y]es or [n]:y
Create path b/w the
nodes
Enter the source
node...2
Enter the destination
node...4
Enter the weight:4
Want to
continue...[y]es or [n]:y
Create path b/w the
nodes
Enter the source
node...2
Enter the destination
node...3
Enter the weight:5
Want to
continue...[y]es or [n]:y
Create path b/w the
nodes
Enter the source node...3
Enter the destination
node...4
Enter the weight:3
Want to
continue...[y]es or [n]:n
The adjacency
matrix....
0 1
2 2
1 0
5 4
2 5
0 3
2 4
3 0
Minimum cost spanning
tree edges are:
2<->1->
3<->1->
4<->1->
RESULT:
Thus the program to find the
minimum cost spanning tree in a graph using Prims algorithm was executed.
Ex.No: 9 Hierarchal inheritance with virtual function and
RTTI
Aim:
To write a C++ program to draw rectangle,
square and circle using multiple inheritance with virtual function.
Algorithm:
Step 1: Create a class Point.
1.1 Declare the data members x and y.
1.2 Declare the member functions as
Point(int tempx,int tempy)
int getx()
int gety()
1.3 getx() function is used to return the
value of x .
1.4 gety() function is used to return the
value of y.
Step 2: Create a base class shape.
2.1 Declare the necessary data members.
2.2 Declare the member function as
virtual void draw()
Step 3: Create a derived class square.
(Base
class – shape)
3.1 Create an abstract class.
3.2 Get the sides of square.
3.3 Draw the square using draw() function.
Step 4: Create a derived class rectangle.
(Base
class – shape)
4.1 Create an abstract class.
4.2 Get the length and breadth of rectangle.
4.3 Draw the rectangle using draw() function.
Step 5: Create a derived class circle.
(Base
class – shape)
5.1 Create an abstract class.
5.2 Get the radius of circle.
3.3 Draw the circle using draw() function.
Step 6: In the main,
6.1 Create the objects for point class.
6.2 Create a base pointer object for shape
class.
6.3 Call the draw() function to draw the
square, rectangle and circle.
Program:
#include
#include
#include
class Point
{
public:
int x;
int y;
Point(){}
Point(int tempX, int tempY)
{
x=tempX;
y=tempY;
}
int GetX()
{
return x;
}
int GetY()
{
return y;
}
friend ostream & operator<<(ostream
& tempout, Point & tempPoint)
{
tempout<<"("<
return tempout;
}
};
class Shape
{
Point Position;
public:
Shape(){}
virtual void draw()
{
cout<<"shape is drawn";
}
};
class Square : public Shape
{
Point LeftBottom;
int Length;
public:
Square(){}
Square(Point tLeftBottom, int tLength)
{
LeftBottom=tLeftBottom;
Length=tLength;
}
void draw()
{
cout<<"Square is drwan
at"<
as"<
setcolor(14);
rectangle(LeftBottom.GetX(),LeftBottom.GetY(),LeftBottom.GetX()+Length,LeftBottom.GetY()+Length);
}
};
class Rectangles : public Shape
{
Point LeftBottom, LeftTop, RightBottom,
RightTop;
public:
Rectangles(){}
Rectangles(Point tLeftBottom, Point tLeftTop,
Point tRightBottom, Point
tRightTop)
{
LeftBottom=tLeftBottom;
LeftTop=tLeftTop;
RightBottom=tRightBottom;
RightTop=tRightTop;
}
void draw()
{
cout<<"Rectangle is drwan
at("<
setcolor(4);
rectangle(LeftBottom.GetX(),LeftBottom.GetY(),RightTop.GetX(),RightTop.GetY());
}
};
class Circle : public Shape
{
Point Center;
int Radius;
public:
Circle(){}
Circle(Point tCenter, int tRadius)
{
Center=tCenter;
Radius=tRadius;
}
void draw()
{
cout<<"Circle is drawn
at"<<" "<
is"<
setcolor(5);
circle(Center.GetX(),Center.GetY(),Radius);
}
};
int main()
{
clrscr();
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,
"D:/Tc/BGI");
Point p1(100,200);
Point p2(50,50);
Point p3(100,300);
Square sq(p1,50);
Rectangles rect(p1,p2,p1,p2);
Circle c(p3,50);
Shape*s;
s=&sq; s->draw();getch();
s=▭s->draw();getch();
s=&c; s->draw();getch();
return 0;
}
Output:
Square is drawn at (100,200) and with length
as 50
Rectangle is drawn at ((100,200), (100,200) )
and ( (50,50), (50,50) )
Circle is drawn at (100,300) and the radius
is 50
Result: -
Thus the C++ program to draw rectangle,
square and circle using multiple inheritance with virtual function was
executed.
OPERATIONS ON COMPLEX
NUMBERS USING FILES AS STORAGE
AIM:
To write a C++ program to
perform addition of complex numbers using files.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a class called
COMPLEX and create its members: real and imaginary.
Step 3: Generate the random
numbers by using rand() function.
Step 4: Write the randomly
generated numbers in a file called “complex1.txt”.
Step 5: Add the two complex
numbers.
Step 6: Store the Resultant
complex number in another file called “result.txt”
Step 7: Display the resultant
value.
Step 8: Stop the program.
PROGRAM:
#include
#include
#include
#include
#include
class complex
{
public:
int real;
int imag;
complex(int r, int i)
{
real = r;
imag = i;
}
complex()
{
real = imag = 0;
}
void display(void);
};
void complex::display(void)
{
cout<
}
void main()
{
clrscr();
ofstream
ocom("complex1.txt");
float real,imag;
time_t ti;
srand((unsigned)
time(&ti));
real = rand()%100;
imag = rand()%100;
ocom<<"("<
real = rand()%100;
imag = rand()%100;
ocom<<"("<
ocom.close();
ifstream
icom("complex1.txt");
char no,t,ch,op;
icom>>no;
icom>>real;
icom>>ch;
icom>>no;
icom>>imag;
imag=(ch=='+')?imag:-imag;
icom>>no;
icom>>op;
complex a(real,imag);
icom>>no;
icom>>real;
icom>>ch;
icom>>no;
icom>>imag;
imag=(ch=='+')?imag:-imag;
icom>>no;
icom>>op;
complex b(real,imag);
complex c;
switch(op)
{
case '+':
c.real = a.real+b.real;
c.imag = a.imag+b.imag;
break;
case '-':
c.real = a.real-b.real;
c.imag = a.imag-b.imag;
break;
case '*':
c.real =
(a.real*b.real)-(a.imag*b.imag);
c.imag =
(a.real*b.imag)+(a.imag*b.real);
break;
case '/':
float qt;
qt = b.real*b.real+b.imag*b.imag;
c.real =
(a.real*b.real+a.imag*b.imag)/qt;
c.imag =
(a.imag*b.real-a.real*b.imag)/qt;
break;
default:
cout<<"\n
Invalid";
}
cout<<"\n complex
1:";
a.display();
cout<<"\n complex
2:";
b.display();
cout<<"\n
Resultant complex:";
c.display();
ofstream
out("result.txt");
out<<"("<
out.close();
}
OUTPUT:
complex1.txt:
(0+i72)+(39+i71)
result.txt
(39+i143)
RESULT:
Thus the program for addition
of complex numbers using files was executed.
No comments:
Post a Comment