Oops Lab Program
Oops Lab Program
//swap 1:
#include<iostream>
using namespace std;
class Swap
{
public:
void display()
{
int a,b;
cout<<" enter two no\n";
cin>>a>>b;
cout<<"\n number before swapping";
cout<<"\n first number="<<a;
cout<<"\n second number="<<b;
int *x,*y,temp;
x=&a;
y=&b;
temp=*x;
*x=*y;
*y=temp;
cout<<"\n number after swapping";
cout<<"\n first number="<<a;
cout<<"\n second number="<<b;
}
};
int main()
{
Swap s;
s.display();
return 0;
}
output
//2.palindrome:
#include<iostream>
#include<string.h>
using namespace std;
class palindrome
{
char s[20];
void getstring()
{
cout<<"\n enter string to check palindrome or not";
cin>>s;
int i,flag=0;
int len=strlen(s);
for(i=0;i<len-1;i++)
{
if(s[i]!=s[len-i-1])
{
flag=1;
break;
}
}
if (flag==1)
{
cout<<s<<"is not a palidrome";
}
else {
cout<<s<<"is a palindrome";
}
}
public:
void display()
{
getstring();
}
};
int main()
{
palindrome p;
p.display();
return 0;
}
Output:
//dynamically memory transpose 3:
#include<iostream>
using namespace std;
class trans
{
int **a,**b;
int m,n;
public:
void initialize();
void transpose();
void display();
void deallocate();
};
void trans::initialize()
{
int i,j;
cout<<" no. of coulmn and row :";
cin>>m>>n;
a=new int*[m];
for(i=0;i<m;i++)
a[i]=new int[n];
cout<<" enter element:\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
b=new int*[n];
for(i=0;i<n;i++)
b[i]=new int[m];
}
void trans::transpose()
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<m;j++)
b[j][i]=a[i][j];
}
void trans::display()
{
int i,j;
cout<<"transpose :\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
cout<<b[i][j]<<" ";
cout<<"\n";
}
}
void trans::deallocate(){
int i;
for(i=0;i<m;i++)delete a[i];
delete a;
for(i=0;i<n;i++)delete b[i];
delete b;
}
int main(){
trans t;
t.initialize();
t.transpose();
t.display();
t.deallocate();
}
Output:
}
cout<<endl;
}
}
int main()
{
M1 m1;
M2 m2;
m1.getMatrix1();
m2.getMatrix2();
m1.putMatrix1();
m2.putMatrix2();
matsum(m1,m2);
return 0;
}
Output:
Output:
//fibanocci 6:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Fibonacci
{
int a,b;
public :
Fibonacci()
{
a=0;b=1;
}
void fibseries(int n)
{
int i,next;
cout<<"resultant fibseries:\n";
for(i=0;i<n;i++)
{
next = a+b;
cout<<"\t"<<next<<"\t";
a=b;
b=next;
}
}
};
int main()
{
Fibonacci F;
int n;
cout<<"range:";
cin>>n;
F.fibseries(n);
return 0;
}s
Output:
//Copy constructor 7:
#include<iostream>
using namespace std;
class matrix
{
public:
int **a;
matrix()
{
int i,j;
a=new int*[3];
for(i=0;i<3;i++)
a[i]=new int[3];
cout<<"enter a element for 3*3 matrix:\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
}
matrix(matrix &x)
{
int i,j;
a=new int*[3];
for(i=0;i<3;i++)
a[i]=new int[3];
for(j=0;j<3;j++)
a[j]=new int[3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=x.a[i][j];
}
~matrix()
{
int i;
for(i=0;i<3;i++)
delete a;
}
void putmatrix();
friend voidadd(matrix,matrix);
};
void matrix::putmatrix()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
void add(matrix m1,matrix m2)
{
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++)
cout<<(m1.a[i][j]+m2.a[i][j])<<" ";
cout<<endl;
}
}
int main()
{
matrix obj1;
matrix obj2(obj1);
cout<<"matrix1 (and matrix2 also):\n";
obj1.putmatrix();
cout<<" sum of matrix:\n";
add(obj1,obj2);
return 0;
}
Output:
//Single inheritance 8:
#include<iostream>
using namespace std;
class BCA
{
protected:
int rollno;
char name[20];
public:
void getdata()
{
cout<<"enter rollno.";
cin>>rollno;
cout<<"enter name";
cin>>name;
}
void display()
{
cout<<"\n name:"<<name;
cout<<"\n rollno"<<rollno;
}
};
class mark:public BCA
{
int m1,m2,m3,total;
float avg;
public:
void getMarks()
{
cout<<" enter 3 mark";
cin>>m1>>m2>>m3;
}
void displayavg()
{
total=m1+m2+m3;
avg=total/3.0;
cout<<"total:"<<total;
cout<<"avg :"<<avg;
}
};
int main()
{
mark m;
m.getdata();
m.getMarks();
m.display();
m.displayavg();
return 0;
}
Output:
//12.virtual function
#include<iostream.h>
class GFG_Base
public:
void print(){
};
public:
void display()
}
void print()
};
int main()
GFG_Base base;
GFG_Child child;
base.display();
base.print();
child.display();
child.print();
return 0;
//13.function template
#include<iostream>
using namespace std;
template<class T>
class array
{
int size,i,j;
T a[30];
public:
void get()
{
cout<<"\nenter size";
cin>>size;
for(i=0;i<size;i++)
cin>>a[i];
}
void display(){
cout<<"\nthe sorted array";
for(i=0;i<size;i++)
cout<<a[i]<<"\t";
}
void sort()
{
T temp;
for(i=0;i<size;i++)
{
for(j=0;j<size-1;j++){
if(a[j]>a[j+1])
{
temp = a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
};
int main()
{
array<int> iobj;
array<float> fobj;
cout<<"enter integer";
iobj.get();
iobj.sort();
cout<<"\nInt sorted";
iobj.display();
cout<<"\nenter float";
fobj.get();
fobj.sort();
cout<<"\nfloat soprted";
fobj.display();
return 0;
}
//14.class template for searching
#include<iostream>
#include<stdlib.h>
#define MAX 5
template<class T>
class Tclasssearch
T x[MAX];
T element;
public:
T classsearch()
void readelements()
int i=0;
for(i=0;i<MAX;i++)
cin>>x[i];
cout<<"\n enter elements to search:";
cin>>element;
T getsearch()
int i;
for(i=0;i<MAX;i++)
cout<<"\t"<<x[i];
for(i=0;i<MAX;i++)
if(x[i]==element)
break;
if(i==MAX)
};
int main()
{
Tclasssearch<int> iMAX=Tclasssearch<int>();
Tclasssearch<float> fMAX=Tclasssearch<float>();
iMAX.readelements();
iMAX.getsearch();
fMAX.readelements();
fMAX.getsearch();
return 0;
//15.Exception handling
#include<iostream>
using namespace std;
int main()
{
int n,mark[10];
float avg=0,tot=0;
cout<<" \n enter the no. of subject";
cin>>n;
cout<<" \n enter the marks of a student\n"<<n<<"student \n";
try
{
if((n>0)&&(n<=10))
{
for(int i=0;i<n;i++)
{
cout<<" enter a mark of student"<<i<<"\t";
cin>>mark[i];
tot +=mark[i];
}
avg=tot/n;
cout<<" total mark:\t"<<tot<<"\n average of mark:\t"<<avg;
}
else if(n==0)
throw(0);
else if(n>10)
throw(10.0);
}
catch(int i)
{
cout<<" divide by zero"<<i;
}
catch(double i)
{
cout<<" out of bound exception"<<i;
}
return 0;
}