0% found this document useful (0 votes)
29 views21 pages

Oops Lab Program

Uploaded by

vedvv1871
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views21 pages

Oops Lab Program

Uploaded by

vedvv1871
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

// OOPs using CPP lab programs for BCA Ist year 2nd sem

//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:

//friend function add two matrices 4:


#include<iostream>
using namespace std;
class M2;
class M1
{
int a[5][5],c[5][5],i,j;
public :
void getMatrix1();
void putMatrix1();
friend void matsum(M1,M2);
};
void M1::getMatrix1()
{
cout<<" enter 9 element";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
}
void M1::putMatrix1()
{
cout<<"matrix:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
cout<<endl;
}
}
class M2
{
int b[5][5],i,j;
public :
void getMatrix2();
void putMatrix2();
friend void matsum(M1,M2);
};
void M2::getMatrix2()
{
cout<<" enter 9 element";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j];
}
void M2::putMatrix2()
{
cout<<"matrix:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<b[i][j]<<"\t";
cout<<endl;
}
}
void matsum(M1 m1,M2 m2)
{
M1 temp;int i,j;
cout<<" addtion of 2:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
temp.c[i][j]=m1.a[i][j]+m2.b[i][j];
cout<<temp.c[i][j]<<"\t";

}
cout<<endl;
}
}
int main()
{
M1 m1;
M2 m2;
m1.getMatrix1();
m2.getMatrix2();
m1.putMatrix1();
m2.putMatrix2();
matsum(m1,m2);
return 0;
}
Output:

// geometric shapes function overloading 5:


#include<iostream>
#include<stdlib.h>
using namespace std;
class Area
{
public:
int l,b,a;
float r;
public:
float Area1(float r)
{
return(3.14*r*r);
}
int Area1(int a,int b)
{
return (l*b);
}
int Area1(int a)
{
return (6*a*a);
}
void display()
{
cout<<" area of circle:"<<Area1(r)<<endl;
cout<<"area of rectangle :"<<Area1(l,b)<<endl;
cout<<"area of cube:"<<Area1(a)<<endl;
}
void getdata()
{
cout<<"enter a radius:";
cin>>r;
cout<<"\n enter length and breath:";
cin>>l>>b;
cout<<"\n enter side:";
cin>>a;
}
};
int main()
{
Area A;
A.getdata();
A.display();
return 0;
}

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:

//unary operator :10


#include<iostream>
using namespace std;
class matrix
{
int x[5][5],m,n;
public:
matrix(){}
matrix(int a,int b)
{
m=a;
n=b;
}
void get()
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>x[i][j];
}
void put()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<x[i][j]<<"\t";
cout<<endl;
}
}
matrix operator!()
{
matrix c(n,m);
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c.x[i][j]=x[j][i];
return c;
}
};
int main()
{
int m,n;
cout<<"enter m and n";
cin>>m>>n;
matrix a(m,n),c;
a.get();
cout<<"oringial matrix \n";
a.put();
cout<<"\n transpose :\n";
c=!a;
c.put();
return 0;
}
Output:
//11.binaryoperator overloading
#include<iostream>
using namespace std;
class matrix
{
private:
int mat[2][2],add[2][2];
public:
void get(void)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cin>>mat[i][j];
}
}
}
void operator +(matrix a)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
add[i][j]=mat[i][j]+a.mat[i][j];
cout<<add[i][j]<<"\t";
}
cout<<"\n";
}
}
};
int main(void)
{
class matrix p,q;
cout<<"\n Enter four element for first matrix\n";
p.get();
cout<<"\n enter four element for second matrix\n";
q.get();
p+q;
return 0;
}

//12.virtual function

#include<iostream.h>

class GFG_Base

public:

virtual void display()

cout<<"called virtual base class function"<<"\n\n";

void print(){

cout<<"called GFG_base print function"<<"\n\n";

};

class GFG_Child:public GFG_Base

public:

void display()

cout<<"called GFG_child display function"<<"\n\n";

}
void print()

cout<<"called GFG_child print function"<<"\n\n";

};

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

using namespace std;

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;

cout <<"\n your data:";

for(i=0;i<MAX;i++)

cout<<"\t"<<x[i];

for(i=0;i<MAX;i++)

if(x[i]==element)

cout<<"\n class template search:\n


element:"<<element<<"\n found: positions:"<<i++<<":\n";

break;

if(i==MAX)

cout<<"\n search elements :"<<element<<":not found\n";

};

int main()

{
Tclasssearch<int> iMAX=Tclasssearch<int>();

Tclasssearch<float> fMAX=Tclasssearch<float>();

cout<<"simple class template array program example:searchnumbers \n";

cout<<"\n enter"<<MAX<<"elements for searching integer:"<<endl;

iMAX.readelements();

iMAX.getsearch();

cout<<"\n enter "<<MAX<<"elements for searching float:"<<endl;

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;
}

You might also like