Thursday 9 May 2013

OBJECT ORIENTED PROGRAMMING LAB MANUAL

EX.NO: 1                            FUNCTION OVERLOADING

AIM:
To write  a program  for performing function overloading  using 
              cube, cylinder & rectangle.
         
           ALGORITHM :

1.    Start the process
2.    Declare the same function name for all the functions
3.     In the function definition perform the calculations for all the functions.
4.    Compile and  save the program
5.    Stop the process

PROGRAM :                           

#include< iostream.h>
int volume(int);
double volume(double ,int);
long volume(long,int, int );
int main()
{
cout<<”Volume of Cube:” <<volume(10)<<”\n”;
cout<<”Volume of Cylinder:”<<volume(2.5,8)<<”\n”;
cout<<”Volume of rectangle :”<<volume(100L,75,15)<<”\n”;
return 0;
}
int volume(int s)
{
return(s*s*s);
}
double volume(double r,int h)
{
return(3.14519*r*r*h);
}
long volume(long l,int b,int h)
{
return(l*b*h);
}

OUTPUT:

Volume of Cube:  1000
Volume of Cylinder: 157.26
Volume of rectangle : 112500

RESULT:

Thus the above program has been completed and the output  is verified.






EX.NO: 2                        CLASS WITH STATIC DATA MEMBER


AIM:
To implement static data member in class to be by all the instances of the class.

ALGORITHM:
1. Create class ITEM with static data member as count.
2. Create a member function to increment the count.
    3. Declare the static datamember using scope resolution operator.
4. Display the count value.

PROGRAM:

#include<iostream.h>
class item
{
static int count;
int num;
public:
void getdata(int a)
{
num=a;
count++;
cout<<”Number”<<num;
}
void showcount()
{
cout<<”count”;
cout<<count<<”\n”;
}
};
int item::count;
int main()
{
item a,b,c;
a.showcount();
b.showcount();
c.showcount();
a.getdata(20);
b.getdata(30);
c.getdata(40);
a.showcount();
b.showcount();
c.showcount();
}






OUTPUT:

count        0
count        0
count        0
Number    20
Number    30
Number    40
count        3
count        3
count        3
        



































RESULT:

Thus the above program has been completed and the output is verified.



EX.NO: 3         CLASS WITH STATIC MEMBER FUNCTION

AIM:
To implement static  member function in class to be shared by all the instances of the class.

ALGORITHM
1.    Start the process
2.    Create class TEST with static member function.
3.    Invoke the static member function using classname with scope
    resolution operator and function name.
4.    Display the value
5.    Stop the process

PROGRAM:

#include<iostream.h>
class test
{
int code;
static int count;
public:
void setcode()
{
cout<<”Object Number:”<<code<<”\n”;
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcount();
t2.setcount();
test::showcount();
test t3;
t1.showcode();
t2.showcode();
t3.showcode();
return(0);
}












OUTPUT:

count            2
count            3
Object Number     1
Object Number     2
Object Number     3




































RESULT:

Thus the above program has been completed and the output is verified.









EX.NO: 4          OPERATOR OVERLOADING USING COMPLEX NUMBERS                                



AIM:
                  To implement operator  overloading  to perform addition , substraction ,
                   multiplication and division using Complex Numbers.

ALGORITHM:

1.    Start the program.
2.    Create Complex class to declare the necessary data members and member functions.
3.    Perform operator overloading to perform the necessary operation on the complex number class.
4.    Print the result of the operator overloading.
5.    Stop the program

PROGRAM:

#include<iostream.h>
#include<conio.h>
class Complex
{
 private:
  float real, img;
 public :
 Complex(){}
 Complex(float r, float i)
 {
  real=r;
  img=i;
 }
 Complex operator +(Complex);
 Complex operator -(Complex);
 Complex operator *(Complex);
 Complex operator /(Complex);
 void putdata()
 {
   cout<<"/n"<<real<<"+i"<<img;
 }
};
Complex Complex::operator +(Complex c2)
{
 Complex temp;
 temp.real=real+c2.real;
 temp.img=img+c2.img;
 return temp;
}
Complex Complex::operator -(Complex c2)
{
 Complex temp;
 temp.real=real-c2.real;
 temp.img=img-c2.img;
 return temp;
}
Complex Complex::operator *(Complex c2)
{
 Complex temp;
 temp.real=real*c2.real+img*c2.img;
 temp.img=real*c2.img+img*c2.real;
 return temp;
}
Complex Complex::operator /(Complex c2)
{
 Complex temp;
 float qt;
 qt=c2.real*c2.real+c2.img*c2.img;
 temp.real=(real*c2.real+img*c2.img)/qt;
 temp.img=(img*c2.real-real*.c2.img)/qt;
 return temp;
}
void main()
{
 Complex c1(10.2f,2.1f),c2(2.2f,3.4f),c3;
 c3=c1+c2;
 c3.putdata();
 c3=c1-c2;
 c3.putdata();
 c3=c1*c2;
 c3.putdata():
 c3=c1/c2;
 c3.putdata();
}


OUTPUT:

12.4+i5.5
8+i-1.3
29.58+i39.3
1.80366+i-1.83293












RESULT:

Thus the program for performing various operations on complex  numbers using operator overloading was executed and the output is verified.


EX.NO: 5      IMPLEMENT CLASS WITH DYNAMIC MEMORY ALLOCATION


AIM:
To implement  class with dynamic memory allocation using constructor,destructor and copy constructor.

ALGORITHM:

1. Create the class name as MATRIX.
2. Declare the data member and member function.
3. Declare constructor,destructor and copy constructor
4. Display the result.

PROGRAM:

#include<iostream.h>
#include<process.h>
const int TRUE=1;
const int FALSE=0;
class matrix
{
private:
int row;
int col;
int **p;
public:
matrix()
{
row=col=0;
p=NULL;
}
matrix(int r,int c);
~matrix();
void read();
void show();
void add(matrix &a,matrix &b);
void sub(matrix &a,matrix &b);

};
matrix::matrix(int r,int c)
{
row=r;
col=c;
p=new int *[row];
for(int i=0;i<row;i++)
p[i]=new int[col];
}
matrix::~matrix()
{
for(int i=0;i<row;i++)
delete p[i];
delete p;
}

void matrix::add(matrix &a,matrix &b)
{
int i,j;
row=a.row;
col=b.col;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
p[i][j]=a.p[i][j]+b.p[i][j];
}
void matrix::sub(matrix &a,matrix &b)
{
int i,j;
row=a.row;
col=b.col;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
p[i][j]=a.p[i][j]-b.p[i][j];
}
Void matrix::read()
{
int i,j;
for(inti=0;i<row;i++) for(intj=0;j<col;j++)
{
cout<<”Matrix[“<<i<<”,”<<j<<”]=?”;
cin>>p[i][j];
}

Void matrix::show()
{
int i,j;
for(inti=0;i<row;i++)
{
cout<<endl;
for(intj=0;j<col;j++)
{
cout<<p[i][j]<<” “;
}

}

void main()
{
int m,n,p,g;
cout<<”Enter the A matrix…”<<endl;
cout<<”Howmany rows?”;
cin>>m;
cout<<”How many col?”;
cin>>n;
matrix a(m,n);
a.read();

cout<<”Enter the B matrix…”<<endl;
cout<<”Howmany rows?”;
cin>>p;
cout<<”How many col?”;
cin>>q;
matrix b(p,q);
b.read();
cout<<”Matrix A is ..”;
a.show();
cout<<endl<<”Matrix B is…”;
b.show();
matrix c(m,n);
c.add(a,b);
cout<<endl<<”c=a+b..”;
c.show();
matrix d(m,n);
d.add(a,b);
cout<<endl<<”d=a-b..”;
d.show();
}

OUTPUT:

Enter the A matrix…
Howmany rows?2
How many col?2
Matrix[0,0]=?1
Matrix[0,1]=?1
Matrix[1,0]=?1
Matrix[1,1]=?1

Enter the B matrix…
Howmany rows?2
How many col?2
Matrix[0,0]=?1
Matrix[0,1]=?1
Matrix[1,0]=?1
Matrix[1,1]=?1

Matrix A is
1    1
1    1
Matrix B is
1    1
1    1
c=a+b
2    2
2     2
d=a-b
0    0
0    0


RESULT:

Thus the above program has been completed and the output is verified.


EX.NO: 6          TEMPLATE OF LINKED LIST CLASS AND ITS METHODS

AIM:
To implement the linked list to perform insert, delete and display the data.

ALGORITHM:
1. Create the class with LIST.
2. Create a member function for constructor and to insert the element. 
3. Use the pointer to refer the next list.
4. Display the list value.

PROGRAM:

#include<iostream.h>
#include<process.h>
class list
{
private:
int data;
list *next;
public:
list()
{
data=0;
next=NULL;
}
ist(int dat)
{
data=dat;next=null;
}
~list(){}
int get()
{
return data;
}
void insert(list *node)
friend void display(list *);
};
void list::insert(list *node)
{
list *last=this;
while(last->next)
last=last->next;last->next=node;
}
void display(list *first)
{
list *traverse;
cout<<” list traversal yields:”;
for(traverse=first;traverse;traverse=traverse->next)
cout<<traverse->data<<”,”;
cout<<endl;
}
void main()
{
int choice, data;
list *first=null;
list *node;
while(1)
{
cout<<”Linked list…”;
cout<<”1.Insert”<<endl;
cout<<”2.Display”<<endl;
cout<<”3..quit”<<endl;
cout<<”Enter the choice”;
cin>>choice;
switch(choice)
{
case 1:cout<<”Enter the data:”;
cin>>data;
node=new list(data);
if(first==null)
first=node;
elsefirst->insert(node)
break;
case 2:
display(first);
break;
case 3:exit(1);

default:cout<<”bad options”<<endl;
continue;
}
}
}

OUTPUT:

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:1
Enter the data:2




Linked list…
1.Insert
2.Display
3..quit
Enter the choice:2
list traversal yields:2

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:1
Enter the data:3

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:1
Enter the data:4

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:2
list traversal yields:2,3,4

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:3





























RESULT:

Thus the above program has been completed and the output is verified.

EX.NO: 7              BUBBLE SORT

AIM:
                   To create templates and sort the numbers using Bubble sort .

ALGORITHM:

1. Start the TC editor.
2. Include the header files necessary for performing the sorting operation.
3. Create the class templates for performing the Bubble sort.
4. Get the values to be sorted.
5. Compare each number in the array with every other number.
6. Obtain the sorted list of elements.
7. Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
template<class T>
void Print(T *a,int n)
{
 cout<<a[0];
 for(int i=1;i<n;i++)
 {
  cout<<","<<a[i];
 }
 cout<<endl;
}
template <class T>
void Sort(T *a,int a)
{
 for(int i=1;i<n;i++)
 {
  for(int j=1;j<=n-i;j++)
  {
   if(a[j-1]>a[j])
   {   T temp=a[j-1];
       a[j-1]a[j];
       a[j]=temp;
    }
    }
    }
    }
void main()
{
 int a[]={12,11,15,13,17,14,16,19,18};
 cout<<"\nBefore Sorting\n";
 Print(a,9);
 Sort(a,9);
 cout<<"\nAfter Sorting\n";
 Print(a,9);
 char ch[]={'b','d','a','f','h','c','e','i','g'};
 cout<<"\nBefore Sorting\n";
 Print(ch,9);
 Sort(ch,9);
 cout<,"\nAfter Sorting\n";
 Print(ch,9);
}


OUTPUT:
Before Sorting
12,11,15,13,17,14,16,19,18

After Sorting
11,12,13,14,15,16,17,18,19

Before Sorting
b,d,a,f,h,c,e,i,g

After Sorting
A,b,c,d,e,f,g,h,i






























RESULT:

                 Thus the template for class was created and the elements were sorted
                 using Bubble Sort.

EX.NO: 8              INSERTION SORT

AIM:
              To create templates and sort the numbers using Insertion sort .

ALGORITHM:

1. Start the TC editor.
2. Include the header files necessary for performing the sorting     operation.
3. Create the class templates for performing the Insertion sort.
4. Get the values to be sorted.
5. Compare each number in the array with every other number.
6. Obtain the sorted list of elements.
7. Stop the program.

PROGRAM:

include<iostream.h>
#include<conio.h>
template<class T>
void Print(T *a,int n)
{
 cout<<a[0];
 for(int i=1;i<n;i++)
 {
  cout<<","<<a[i];
 }
 cout<<endl;
}
template<class T>
void Sort(T*a,int n)
{
 for(i=1;i<n;i++)
 {
  T temp = a[i];
  for(int j=i=j>0&&a[j-1]>temp;j--)
  {
   a[j]=a[j-1];
  }
  a[j]=temp;
 }
}
void main()
{
 int a[]={12,11,15,13,17,14,16,19,18};
 cout<<"\nBefore Sorting\n";
 Print(a,9);
 Sort(a,9);
 cout<<"\nAfter Sorting\n";
 Print(a,9);
 char ch[]={'b','d','a','f','h','c','e','i','g'};
 cout<<"\nBefore Sorting\n";
 Print(ch,9);
 Sort(ch,9);
 cout<<"\nAfter Sorting\n";
 Print(ch,9);
}


OUTPUT:

Before Sorting
12,11,15,13,17,14,16,19,18

After Sorting
11,12,13,14,15,16,17,18,19

Before Sorting
b,d,a,f,h,c,e,i,g

After Sorting
A,b,c,d,e,f,g,h,i































RESULT:

                 Thus the template for class was created and the elements were sorted
                 using Insertion Sort.


EX.NO: 9              MERGE SORT

AIM:

                  To create templates and sort the numbers using Merge sort .

ALGORITHM:

1. Start the TC editor.
2. Include the header files necessary for performing the sorting    operation.
3. Create the class templates for performing the Merge sort.
4. Get the values to be sorted.
5. Compare each number in the array with every other number.
6. Obtain the sorted list of elements.
7. Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
template(class T>
void Print(T *a,int n)
{
 cout<<a[0];
 for(int i=1;i<n;i++)
 {
  cout<<","<<a[i];
 }
 cout<<endl;
}
template<class T>
void merge(T *a, int n1,int n2)
{
 T *temp = new T[n1+n2];
 int i=0,j1=0,j2=0;
 while(j1<n1 && j2<n2)
   temp[i++]=(a[j1]<=a[n1+j2] ? a[j1++]:a[n1+j2++]);
 while(j1<n1)
   temp[i++]=a[j1++];
 while(j2<n2)
   temp[i++]=(a+n1)[j2++];
 for(i=0;i<n1+n2;i++)
    a[i]=temp[i];
  delete[]temp;
}
template<class T>
void Sort(T *a, int n)
{
 if(n>1)
 {
   int n1=n/2;
   int n2=n-n1;
   Sort(a,n1);
   Sort(a+n1,n2);
   merge(a,n1,n2);
  }
 }
void main()
{
 int a[]={12,11,15,13,17,14,16,19,18};
 cout<<"\nBefore Sorting\n";
 Print(a,9);
 Sort(a,9);
 cout<<"\nAfter Sorting\n";
 Print(a,9);
 char ch[]={'b','d','a','f','h','c','e','i','g'};
 cout<<"\nBefore Sorting\n";
 Print(ch,9);
 Sort(ch,9);
 cout<<"\nAfter Sorting\n";
 Print(ch,9);
}

OUTPUT:

Before Sorting
12,11,15,13,17,14,16,19,18

After Sorting
11,12,13,14,15,16,17,18,19

Before Sorting
b,d,a,f,h,c,e,i,g

After Sorting
A,b,c,d,e,f,g,h,i
















RESULT:

Thus the template for class was created and the elements were sorted using Merge Sort.



EX.NO: 10              QUICK SORT

AIM:
                   To create templates and sort the numbers using Quick sort .

ALGORITHM:

1. Start the TC editor.
2. Include the header files necessary for performing the sorting operation.
3. Create the class templates for performing the Quick sort.
4. Get the values to be sorted.
5. Compare each number in the array with every other number.
6. Obtain the sorted list of elements.
7. Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
template<class T>
void Print(T *a,int n)
{
 cout<<a[0];
 for(int i=1;i<n;i++)
 {
  cout<<","<<a[i];
 }
 cout<<endl;
}
template<class T>
void quick(T *a, int first, int last)
{
 int i,j,pivot;
 if(first<last)
 {
   pivot=a[first];
   i=first;
   j=last;
   while(i<j)
   {
     while(a[i]<=pivot && i<last)
       i++;
       while (a[j]>=pivot && j>first)
     j--;
       if(i<j)
     swap(a,i,j);
   }
   swap(a,first,j);
   quick(a,first,j-1);
   quick(a,j+1,last);
  }
 }
 template<class T>
 void swap(T *a,int i, intj)
 {
  T temp=a[i];
  a[i]=a[j];
  a[j]=temp;
 }
void main()
{
 int a[]={12,11,15,13,17,14,16,19,18};
 cout<<"\nBefore sorting\n";
 Print(a,9);
 quick(a,0,9-1);
 cout<<"\nAfter Sorting\n";
 Print(a,9);
 char ch[]={'b','d','a','f','h','c','e','i','g'};
 cout<<"\nBefore Sorting\n";
 Print(ch,9);
 quick(ch,0,9-1);
 cout<<"\nAfter Sorting\n";
 Print(ch,9);
}


OUTPUT:

Before Sorting
12,11,15,13,17,14,16,19,18

After Sorting
11,12,13,14,15,16,17,18,19

Before Sorting
b,d,a,f,h,c,e,i,g

After Sorting
A,b,c,d,e,f,g,h,i















RESULT:

                     Thus the template for class was created and the elements were
                     Sorted using Quick Sort.

EX.NO: 11              QUEUE CLASS WITH EXCEPTION HANDLING

AIM:
                   To implement the Queue class with the necessary exception handling.

ALGORITHM:

1. Start the program.
2. Create the necessary Queue class.
3. Perform the operations on the Queue class such as Enqueue and   Dequeue.
4. Catch the exceptions using the exception handling routines.
5. Print the out of the operations performed on  Queue.
6. Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<process.h>
#define QSIZE 3
#define FULL 0
#define EMPTY 1
#define SOMEDATA 2
class except1{};
class except2{};
class except3{};
class except4{};
class Queue
{
 public:
      int front,rear,QStatus;
    void displaymenu();
        void enqueue(int[],int);
    int dequeue(int[]);
     void view(int[]);
};
void main()
{
    Queue q;
    int queue[QSIZE},choice,data;
    q.displaymenu();
    q.rear=-1;
    q.front=-1;
    q.QStatus=EMPTY;
    try
        {
          while(1)
          {
            cout<<"\n\tChoice[1-4]:?";
        cin>>choice;
            switch(choice)
            {
               case 1:
                  
             if(q.Q.Status==FULL)
                    throw except1();
                 else
                 {
                  cout<<"Enter the element";
                  cin>>data;
                  q.enqueue(queue,data);
                  q.view(queue);
                 }
                 break;
              
        case 2:
               
         if(q.QStatus==EMPTY)
                     throw except2();
                 else
                 {
                   q.dequeue(queue);
                   q.view(queue);
                 }
                 break;

                case 3:

                 q.view(queue);
                 break;

                case 4:
                   exit(0);
             }
 
            }           
         }catch(except1){cout<<"\nOverflow on enqueue";}
          catch(except2){cout<<Underflow on Dequeue";}
       }
       void Queue::displaymenu()
       {
        cout <<"\n\t1.Enqueue";
        cout<<"\n\t2.Dequeue";
        cout<<"\n\t3.View";
        cout<<"\n\t4.Exit";
       }

       void Queue::enqueue(int queue[],int data)
       {
        QStatus=SOMEDATA;
        rear++;
        if(rear==QSIZE) 
        rear=0;
        if((front==-1 && rear==(QSIZE-1)||rear==front))
           QStatus=FULL;
        queue[rear]=data;
       }

       int Queue::dequeue(int queue[])
       {
        front++;
        if(front==QSIZE)
           front=0;
        if(front==rear)
           QStatus=EMPTY;
        else
           QStatus=SOMEDATA;
        return (queue[front]);
       }

      void Queue::view(int queue[])
      {
        try
          {
            int i;
            if(QStatus==EMPTY)
                 throw except3();
            else
            {
              i=front;
              cout<<"Queue contains....Front....>;
              do
              {
                cout<<queue[i=(i+1)%QSIZE];
              }while(i!=rear);
              cout<<"Rear\n";
              if(QStatus==FULL)
                  throw except4();
          }
          }catch(except3){cout<<"\n Queue is empty";}
          catch(except4){Cout<<"\n Queue is Full";}
      }   
  






OUTPUT:

1.Enqueue
2. Dequeue
3.View
4.Exit
Choice[1-4]:?1                 

Enter the element 01
Queue contains….Front--->1Rear
Choice[1-4]:?1
Enter the element 20
Queue contains….Front--->120Rear
Choice[1-4]:?1
Enter the element 30
Queue contains ….Front--->12030Rear
Queue is Full
Choice[1-4]:?1

Overflow on enqueue
1.Enqueue
2.Dequeue
3.View
4.Exit
Choice[1-4]:?2

Underflow on dequeue
1.Enqueue
2.Dequeue
3.View
4.Exit
Choice[1-4]:?1
Enter the element 10
Queue contains….Front--->10Rear
Choice[1-4]:?1
Enter the element 20
Queue contains….Front--->1020Rear
Choice[1-4]:?2
Queue contains….Front--->20Rear
Choice[1-4]:?2
Queue  is empty.
Choice[1-4]:?2
Underflow on Dequeue






















RESULT:

Thus the Queue class with the necessary operations and exception handling was implemented and the output verified.

EX.NO: 12             AREA OF TRIANGLE AND RECTANGLE USING
                             INHERITANCE AND VIRTUAL FUNCTION

AIM:
To write a program to compute the Area of Triangle and Rectangle using Inheritance and Virtual Function

ALGORITHM:

1. Start the process
2. Create a class rectangle and calculate the area of the rectangle
3. Indicate the function rtarea as virtual to avoid duplicate
4. Inherit the class rectangle
5. Calculate the area of triangle in the derived class
6. Create a pointer for the base class and access the functions in both
    the Classes.
7. Display the values
8. Terminate the process   

PROGRAM:

#include<iostream.h>
#include<conio.h>
class rectangle
{
public:
int l,b, area;
virtual void rtarea()
{
cout<<"Enter the length and breadth of rectangle\n";
cin>>l;
cin>>b;
area=l*b;
cout<<"\nArea of the rectangle\n"<<area<<"\n\n";
}
};

class triangle:public rectangle
{
public:
int b,h,area;
void rtarea()
{
cout<<"Enter the breadth and height of triangle\n";
cin>>b;
cin>>h;
area=(b*h)/2;
cout<<"\nArea of the triangle\n"<<area<<"\n\n";
}
};

void main()
{
clrscr();
rectangle r;
triangle t;
rectangle *bptr;
bptr=&r;
bptr->rtarea();
bptr=&t;
bptr->rtarea();
getch();
}


OUTPUT:

Enter the length and breadth of rectangle
2
3
Area of the rectangle
6
Enter the breadth and height of triangle
2
3
Area of the triangle
3




























RESULT:

        Thus the Area of Triangle and Rectangle using Inheritance and Virtual
               Function has been computed.

EX.NO: 13         RANDOMLY GENERATED COMPLEX NUMBERS

AIM:
To implement the program that randomly generates complex numbers.

ALGORITHM:
1. Create a two file.
2. One file to read and another file to write.
3. Do the operation in one file and write the result in another file.
4. Display the result.

PROGRAM:

# include<iostream.h>
# include<stdlib.h>
# include<conio.h>
# include<fstream.h>
# include<iomanip.h>

class complex
{
public:
int real;
int img;
complex()
{
real=0;
img=0;
}
complex(int x,int y)
{
real=x;
img=y;
}
complex operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.img=img+c.img;
return temp;
}
complex operator-(complex c)
{
complex temp;
temp.real=real-c.real;
temp.img=img-c.img;
return temp;
}
complex operator*(complex c)
{
complex temp;
temp.real=(real*c.real)-(img*c.img);
temp.img=(real*c.img)+(img*c.real);
return temp;
}
void show()
{


cout<<real;
if(img<0)
cout<<"-i"<<abs(img);
else
cout<<"+i"<<img;
cout<<endl;
}
};
int ctoi(char c)
{
int i=0;
switch(c)
{
case '0': i=0; break;
case '1': i=1;break;
case '2': i=2;break;
case '3':i=3;break;
case '4': i=4;break;
case '5':i=5;break;
case '6':i=6;break;
case '7':i=7;break;
case '8':i=8;break;
case '9':i=9;break;
}
return i;
}
void main()
{
clrscr();
char line[20];
char c;
ifstream fin("aaa.txt");
ofstream fout("Result.txt");
while(!fin.eof())
{
fin.getline(line,20);
cout<<line<<endl;
c=line[0];
complex c1(ctoi(line[5]),ctoi(line[8]));
complex c2(ctoi(line[13]),ctoi(line[16]));
complex c3;

switch(c)
{
case '+':
fout<<setw(10)<<"SUM";
c3=c1+c2;
break;
case '-':
fout<<setw(10)<<"DIFF";
c3=c1-c2;
break;
case '*':
fout<<setw(10)<<"PRODUCT";
c3=c1*c2;
break;
}
fout<<setw(5)<<c3.real<<"+i"<<c3.img<<endl;
}
fin.close();
fout.close();
getch();
}


OUTPUT:

    File Open: aaa.txt
    Performing Randomly Generated Numbers

    File Output: Result.txt
    Printed the Randomly Generated Complex Numbers



























RESULT:

Thus the Randomly generated Complex Numbers has been computed and executed successfully.

EX.NO: 14          MATRIX-OVERLOADING USING CONSTRUCTORS

AIM:
To implement Matrix class with necessary constructors such copy                      constructor and destructor and overloading assignment operator.

ALGORITHM:

1. Start the program.
2. Implement the Matrix class to perform the operator overloading.
3. Use the necessary constructors and destructors.
4. Perform the operator overloading using the assignment operator.
5. Print the output of the operator overloading performed.
6. Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<process.h>
class matrix
{
 int **p;
 int row,col;
 public:
  matrix(int r,int c);
  matrix(matrix &v2);
  ~matrix();
  void operator= (matrix &v2);
  void show();
  void read();
};

matrix::matrix(int r,int c)
{
  row=r;
  col=c;
  p=new int *[row];
  for(int i=0;i<row;i++)
    p[i]=new int[col];
}
matrix::~matrix()
{
 for(int i=0;i<row;i++)
 {
  delete p[i];
 }
 delete p;
}
void matrix::read()
{
 int i,j;
 for(i=0;i<row;i++)
 {
  for(j=0;j<col;j++)
  {
   cin>>p[i][j];
  }
 }
}
void matrix::show()
{
 int i,j;
 for(i=0;i<row;i++)
 {
  cout<<endl;
  for(j=0;j<col;j++)
     cout<<p[i][j]<<"\t";
  }
 }
matrix::matrix(matrix &v2)
{
 cout<<"\ncopy constructor is invoked";
 row = v2.row;
 col=v2.col;
 p=new int*[v2.row];
 for(int i=0;i<v2.row;i++)
 {
  p[i]=v2.p[i];
 }
}

matrix::matrix(matrix &v2)
{
 cout<<"\ncopy constructor is invoked";
row=v2.row;
col=v2.col;
p=new int*[v2.row];
for(int i=0;i<v2.row;i++)
 {
   p[i]=v2.p[i];
 }
}
void matrix::operator=(matrix &v2)
{
 int i,j;
  cout<<"\nAssignment operation is invoked";
  for(i=0;i<v2.row;i++)
  {
    for(j=0;j<v2.col;j++)
    p[i]=v2.p[i];
  }
}
void main()
{
 int m,n,p,q;
 cout<<"\nEnter Matrix A details";
 cout<<"\nHow many rows?";
 cin>>m;
 cout<<"\nHow many columns?";
 cin>>n;
 cout<<"\nEnter Matrix B details";
 cout<<"\nHow many rows?";
 cin>>p;
 cout<<""\nHow many columns?";
 cin>>q;
 if(n!=p)
 {
  cout<<"Invalid order";
  exit(1);
 }
 matrix v1(m,n), v2(p,q);
 v2.read();
 v1=v2;
 v1.show();
 matrix v3=v1;
 v3.show();
}

OUTPUT:

Enter Matrix A details
How many rows ? 2
How many columns?2
Enter Matrix B details
How many rows ? 2
How many columns?2
1
2
3
4

Assignment operation invoked
1    2
2    4


Copy constructor invoked
1            2
3            4






RESULT:

Thus the matrix class was implemented with the necessary constructor and destructors and overloaded using assignment operator and thus the output was verified.




EX.No: 15            DYNAMIC ALLOCATION OF MEMORY

AIM:             
                   To overload the new and delete operators to provide custom dynamic
                      Allocation of memory

ALGORITHM:

1. Start the TC editor.
2. create vector class to perform the necessay operation.
3. overload the new and delete operators to perform the necessary overloading.
4. Print the output of the overloading performed.
5. Stop the program.


PROGRAM:
#include<iostream.h>
const int SIZE=10;
class Vector
{
 private:
  int *array;
 public:
 void *operator new (size_t_size)
 {
  Vector *myvector;
  myvector=::new Vector;
  return myvector;
 }
 void operator delete(void *vec)
 {
  Vector *myvector;
  myvector=(Vector*)vec;
  delete (int*)myvector->array;
  ::delete vec;
 }
 void Read()
 {
  for(int i=0;i<SIZE;i++)
  sum=sum+array[i];
  return sum;
 }


int Sum()
{
   int sum=0;
   for(int i=0;i<SIZE;i++)
        sum+=array[i];
   return sum;
}
};

void main()
{
 Vector *myvector=new Vector;
 cout<<"Enter Vector Data..."<<endl;
 myvector->Read();
 cout<<"Sum of Vector:"<<myvector->Sum();
 delete myvector;
}



OUTPUT:

Enter Vector Data…
Vector[0]:1
Vector[1]:2
Vector[2]:3
Vector[3]:4
Vector[4]:5
Vector[5]:6
Vector[6]:7
Vector[7]:8
Vector[8]:9
Vector[9]:10
Sum of Vector :55






















RESULT:

Thus the program for dynamic allocation of memory was executed and the operators new and delete was overloaded and the output was verified.





EX.No: 16            FRIEND FUNCTION

AIM:
To write a program to read a Value of Distance from One Object and add with a Value in another Object Using Friend Function

ALGORITHM:

1. Start the process
2. Create a Class one and also declare the class two before defining        the Class one.
3. Get the Value for both the Classes
4. In both the Classes declare a friend function to sum the Value of the    
    Two Objects
5. Display the Value.

PROGRAM:

#include<iostream.h>
#include<conio.h>

class two;

class one
{
    int data1;
    public:
        void getdata(int init)
        {
            data1=init;
            cout<<"Distance1: "<<data1<<"\n";
        }
friend int add(one a,two b);
};

class two
{
    int data2;
    public:
        void getdata(int init)
        {
            data2=init;
            cout<<"Distance2: "<<data2<<"\n";
        }
friend int add(one a,two b);
};

int add(one a,two b)
{
    return a.data1+b.data2;
}

void main()
{
    clrscr();
    one a;
    two b;
    a.getdata(5);
    b.getdata(10);
    cout<<"Sum of two distance: "<<add(a,b);
    getch();
}




OUTPUT:

    Distance1: 5
Distance2: 10
Sum of Two Distances: 15


































RESULT:

Thus the Program has been computed and executed successfully.

No comments:

Post a Comment