/*q.N 3.Write a program to add complex numbers of two different classes using friend class */
#include<iostream.h>
#include<conio.h>
class mula;
class aalu;
class gajar
{
      private:
              float realp,imagp;
              public:
                     void getdata();
                     //void show();
                     friend class mula;
                     };
                     
      void gajar::getdata()
      {
           cout<<"\nFor First Class \n";
           cout<<"\nEnter Real Part : ";
           cin>>realp;
           cout<<"\nEnter Imaginary Part : ";
           cin>>imagp;
           }
           
class aalu
      {
            private:
                    float realp,imagp;
                    public:
                           void getdata();
                           //void show();
                           friend class mula;
                           };
                     
      void aalu::getdata()
      {
           cout<<"\nFor Second Class \n";
           cout<<"\nEnter Real Part : ";
           cin>>realp;
           cout<<"\nEnter Imaginary Part : ";
           cin>>imagp;
           }
           
class mula
{
      private:
              float realp,imagp;
              public:
                     void add(gajar &g1,aalu &a1);
                     void display();
                     };
                     
              
      void mula::add(gajar &g1,aalu &a1)
      {
           realp=g1.realp+a1.realp;
           imagp=g1.imagp+a1.imagp;
           }
           
      void mula::display()
      {
           cout<<"\n*****************************\n";
           cout<<"\nThe Added Values Are : \n";
           cout<<"\n"<<realp<<" + i"<<imagp<<endl;
           }
           
int main()
{
    mula m;
    aalu a;
    gajar g;
    
    a.getdata();
    g.getdata();
    m.add(g,a);
    m.display();
    getch();
    return 0;
}
           
           
      
                     
Tuesday, February 5, 2013
swapping
/* Q.N 2. Write a program for swapping private data of two classes using friendly function and call by raference . */
#include<iostream.h>
#include<conio.h>
class class_2;
class class_1
{
private:
int x;
public:
void getdata();
void display();
friend void swap(class_1 &,class_2 &);
};
                     
void class_1::getdata()
{
cout<<"\nEnter A Value For X : ";
cin>>x;
}
           
void class_1::display()
{
cout<<"\nThe Value Of X : "<<x;
}
           
class class_2
{
private:
int y;
public:
void getdata();
void display();
friend void swap(class_1 &,class_2 &);
};
                     
void class_2::getdata()
{
cout<<"\nEnter A Value For Y : ";
cin>>y;
}
           
void class_2::display()
{
cout<<"\nThe Value Of Y : "<<y;
}
void swap(class_1 &a,class_2 &b)
{
int temp;
temp=a.x;
a.x=b.y;
b.y=temp;
}
int main()
{
class_1 p;
class_2 q;
    
p.getdata();
q.getdata();
cout<<"\n*******************************\n";
cout<<"\nThe Values Before Swapping :\n";
p.display();
q.display();
swap(p,q);
cout<<"\n*******************************\n";
cout<<"\nThe Values After Swapping :\n";
p.display();
q.display();
    
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
class class_2;
class class_1
{
private:
int x;
public:
void getdata();
void display();
friend void swap(class_1 &,class_2 &);
};
void class_1::getdata()
{
cout<<"\nEnter A Value For X : ";
cin>>x;
}
void class_1::display()
{
cout<<"\nThe Value Of X : "<<x;
}
class class_2
{
private:
int y;
public:
void getdata();
void display();
friend void swap(class_1 &,class_2 &);
};
void class_2::getdata()
{
cout<<"\nEnter A Value For Y : ";
cin>>y;
}
void class_2::display()
{
cout<<"\nThe Value Of Y : "<<y;
}
void swap(class_1 &a,class_2 &b)
{
int temp;
temp=a.x;
a.x=b.y;
b.y=temp;
}
int main()
{
class_1 p;
class_2 q;
p.getdata();
q.getdata();
cout<<"\n*******************************\n";
cout<<"\nThe Values Before Swapping :\n";
p.display();
q.display();
swap(p,q);
cout<<"\n*******************************\n";
cout<<"\nThe Values After Swapping :\n";
p.display();
q.display();
getch();
return 0;
}
friend function
/* Q.N 1. Write a program using friend function to add numerical values of three objects of different classes */
#include<iostream.h>
#include<conio.h>
class class_2;
class class_3;
class class_1
{
private:
int x;
public:
void getdata();
friend void add(class_1,class_2,class_3);
};
                    
void class_1::getdata()
{
cout<<"\nEnter A Value For X : ";
cin>>x;
}
          
class class_2
{
private:
int y;
public:
void getdata();
friend void add(class_1,class_2,class_3);
};
                    
void class_2::getdata()
{
cout<<"\nEnter A Value For Y : ";
cin>>y;
}
          
class class_3
{
private:
int z;
public:
void getdata();
friend void add(class_1,class_2,class_3);
};
                    
void class_3::getdata()
{
cout<<"\nEnter A Value For Z : ";
cin>>z;
}
          
void add(class_1 a,class_2 b,class_3 c)
{
int sum;
sum=(a.x+b.y+c.z);
cout<<"\n**********************************\n";
cout<<"\nThe Sum Of Given Three Integer : "<<sum<<endl;
}
    
int main()
{
class_1 p;
class_2 q;
class_3 r;
   
p.getdata();
q.getdata();
r.getdata();
add(p,q,r);
getch();
return 0;
}
    
#include<iostream.h>
#include<conio.h>
class class_2;
class class_3;
class class_1
{
private:
int x;
public:
void getdata();
friend void add(class_1,class_2,class_3);
};
void class_1::getdata()
{
cout<<"\nEnter A Value For X : ";
cin>>x;
}
class class_2
{
private:
int y;
public:
void getdata();
friend void add(class_1,class_2,class_3);
};
void class_2::getdata()
{
cout<<"\nEnter A Value For Y : ";
cin>>y;
}
class class_3
{
private:
int z;
public:
void getdata();
friend void add(class_1,class_2,class_3);
};
void class_3::getdata()
{
cout<<"\nEnter A Value For Z : ";
cin>>z;
}
void add(class_1 a,class_2 b,class_3 c)
{
int sum;
sum=(a.x+b.y+c.z);
cout<<"\n**********************************\n";
cout<<"\nThe Sum Of Given Three Integer : "<<sum<<endl;
}
int main()
{
class_1 p;
class_2 q;
class_3 r;
p.getdata();
q.getdata();
r.getdata();
add(p,q,r);
getch();
return 0;
}
Transpose With Pointer
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class matrix
{
int **mat,**t,r,c;
public:
void getmat();
void transpose();
void display();
};
             
void matrix::getmat()
{
cout<<"\nEnter the size of Matrix : ";
cout<<"\nRow : ";
cin>>r;
cout<<"\nColumn : ";
cin>>c;
mat=new int *[r];
for(int i=0;i<r;i++)
mat[i]=new int [c];
     
cout<<"\nEnter Your Matrix :\n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"\nEnter Element ["<<i+1<<"]["<<j+1<<"] : ";
cin>>mat[i][j];
}
}
}
                     
void matrix::display()
{
cout<<"\nThe Original Matrix : \n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<endl;
}
}
void matrix::transpose()
{
t=new int *[c];
for(int i=0;i<c;i++)
t[i]=new int [r];
cout<<"\nThe Transpose Matrix : \n";
for(int i=0;i<c;i++)
{
for(int j=0;j<r;j++)
{
t[i][j]=mat[j][i];
cout<<t[i][j]<<"\t";
}
cout<<endl;
}
}
                     
int main()
{
matrix M;
M.getmat();
M.display();
M.transpose();
getch();
return 0;
}
#include<conio.h>
#include<stdlib.h>
class matrix
{
int **mat,**t,r,c;
public:
void getmat();
void transpose();
void display();
};
void matrix::getmat()
{
cout<<"\nEnter the size of Matrix : ";
cout<<"\nRow : ";
cin>>r;
cout<<"\nColumn : ";
cin>>c;
mat=new int *[r];
for(int i=0;i<r;i++)
mat[i]=new int [c];
cout<<"\nEnter Your Matrix :\n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"\nEnter Element ["<<i+1<<"]["<<j+1<<"] : ";
cin>>mat[i][j];
}
}
}
void matrix::display()
{
cout<<"\nThe Original Matrix : \n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<endl;
}
}
void matrix::transpose()
{
t=new int *[c];
for(int i=0;i<c;i++)
t[i]=new int [r];
cout<<"\nThe Transpose Matrix : \n";
for(int i=0;i<c;i++)
{
for(int j=0;j<r;j++)
{
t[i][j]=mat[j][i];
cout<<t[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
matrix M;
M.getmat();
M.display();
M.transpose();
getch();
return 0;
}
Transpose Matrix
#include<iostream.h>
#include<conio.h>
class matrix
{
int mat[10][10],t[10][10],r,c;
public:
void getmat();
void transpose();
void display();
};
             
void matrix::getmat()
{
cout<<"\nEnter the size of Matrix : ";
cout<<"\nRow : ";
cin>>r;
cout<<"\nColumn : ";
cin>>c;
cout<<"\nEnter Your Matrix :\n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"\nEnter Element of ["<<i+1<<"]["<<j+1<<"] : ";
cin>>mat[i][j];
}
}
}
                     
void matrix::display()
{
cout<<"\nThe Original Matrix : \n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<endl;
}
}
void matrix::transpose()
{
cout<<"\nThe Transpose Matrix : \n";
for(int i=0;i<c;i++)
{
for(int j=0;j<r;j++)
{
t[i][j]=mat[j][i];
cout<<t[i][j]<<"\t";
}
cout<<endl;
}
}
                     
int main()
{
matrix M;
M.getmat();
M.display();
M.transpose();
getch();
return 0;
}
#include<conio.h>
class matrix
{
int mat[10][10],t[10][10],r,c;
public:
void getmat();
void transpose();
void display();
};
void matrix::getmat()
{
cout<<"\nEnter the size of Matrix : ";
cout<<"\nRow : ";
cin>>r;
cout<<"\nColumn : ";
cin>>c;
cout<<"\nEnter Your Matrix :\n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"\nEnter Element of ["<<i+1<<"]["<<j+1<<"] : ";
cin>>mat[i][j];
}
}
}
void matrix::display()
{
cout<<"\nThe Original Matrix : \n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<endl;
}
}
void matrix::transpose()
{
cout<<"\nThe Transpose Matrix : \n";
for(int i=0;i<c;i++)
{
for(int j=0;j<r;j++)
{
t[i][j]=mat[j][i];
cout<<t[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
matrix M;
M.getmat();
M.display();
M.transpose();
getch();
return 0;
}
calculating the interest
/* Q.N 4. Write a program to create that calculates the interest of any number */
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class interest
{
int year;
float rate,amount;
public:
void getval();
void calcint();
};
             
void interest::getval()
{
cout<<"Enter Principal Amount : "<<endl;
cin>>amount;
cout<<"\nEnter Rate and Time in Year : "<<endl;
cin>>rate>>year;
}
           
void interest::calcint()
{
float inter=(amount*year*rate/100);
cout<<"\nThe Interest Amount : "<<inter<<endl;
}
           
int main()
{
interest I;
I.getval();
I.calcint();
getch();
return 0;
}
             
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class interest
{
int year;
float rate,amount;
public:
void getval();
void calcint();
};
void interest::getval()
{
cout<<"Enter Principal Amount : "<<endl;
cin>>amount;
cout<<"\nEnter Rate and Time in Year : "<<endl;
cin>>rate>>year;
}
void interest::calcint()
{
float inter=(amount*year*rate/100);
cout<<"\nThe Interest Amount : "<<inter<<endl;
}
int main()
{
interest I;
I.getval();
I.calcint();
getch();
return 0;
}
sorting
/*Q.N 3. Write a C++ code to sort the given 10 numbers in assending order .*/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class sort
{
int val[10];
public:
void getval(void);
void sorts(void);
void disp(void);
};
             
void sort::getval()
{
int i;
cout<<"\nEnter 5 integer Data : \n";
for(i=0;i<5;i++)
{
cout<<"\nEnter Value "<<i+1<<" :";
cin>>val[i];
}
}
                            
void sort::sorts()
{
int i,j,temp;
           
for(j=0;j<5;j++)
{
for(i=0;i<4;i++)
{
if(val[i]>val[i+1])
{
temp=val[i];
val[i]=val[i+1];
val[i+1]=temp;
}
}
}
}
                                                              
void sort::disp()
{
int i;
cout<<"\nThe Sorted Array : \n";
for(i=0;i<5;i++)
{
cout<<"\nValue "<<i+1<<":"<<val[i];
}
}
                            
int main()
{
sort S;
S.getval();
S.sorts();
S.disp();
getch();
return 0;
}
                            
           
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class sort
{
int val[10];
public:
void getval(void);
void sorts(void);
void disp(void);
};
void sort::getval()
{
int i;
cout<<"\nEnter 5 integer Data : \n";
for(i=0;i<5;i++)
{
cout<<"\nEnter Value "<<i+1<<" :";
cin>>val[i];
}
}
void sort::sorts()
{
int i,j,temp;
for(j=0;j<5;j++)
{
for(i=0;i<4;i++)
{
if(val[i]>val[i+1])
{
temp=val[i];
val[i]=val[i+1];
val[i+1]=temp;
}
}
}
}
void sort::disp()
{
int i;
cout<<"\nThe Sorted Array : \n";
for(i=0;i<5;i++)
{
cout<<"\nValue "<<i+1<<":"<<val[i];
}
}
int main()
{
sort S;
S.getval();
S.sorts();
S.disp();
getch();
return 0;
}
Subscribe to:
Comments (Atom)
