Lab Manual
Lab Manual
Lab Manual
DEPARTMENT OF BCA
OBJECT ORIENTED PROGRAMMING
USING C++
LAB MANUAL
(BCA303T)
FOR THIRD SEMESTER
NAME……………………………………………
REG NO…………………………………………
NO
NO.
.
PART
–A
PRO
GRA
MS
Procedure:
#include<iostream.h>
#include<conio.h>
class item
private:
char name[25];
float rate;
int qty;
float amount;
public:
void getdata()
cin>> name;
cin>> qty;
cin>> rate;
cout<<”___________________________”<<endl;
void printdata()
cout<<setw(7)<<name;
cout<<setw(8)<<qty;
cout<<setw(8)<<rate;
amount=rate*qty;
cout<<setw(8)<<amount<<endl;
};
void main()
clrscr();
item shop[10];
int n;
cin>>n;
shop[i].getdata();
cout<<”__________________________”<<endl;
for(i=0;i<n;i++)
shop[i].printdata();
cout<<”____________________________”<<endl;
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
class current;
class saving
char name[15];
int accno;
float balance;
public:
void getdata()
cout<<”\n name”;
cin>>name;
cout<<”account number:”;
cin>>accno;
cout<<”balance:”;
cin>>balance;
};
class current
private:
char name[15];
int accno;
float balance;
public:
void getdata()
cout<<”\name:”;
cin>>name;
cout<<”account number:”;
cin>>accno;
cout<<”balance:”;
cin>>balance;
};
cout<<endl<<”*********************************”<<endl;
void main()
saving s;
current c;
clrscr();
s.getdata();
c.getdata();
cout<<endl<<”***************************************************<<”endl;
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
class sample
private:
int x;
int y;
public:
x=a;
y=b;
void showdata()
cout<<”\nx=”<<x<<”y=”<<y;
};
void swap(sample& s)
int temp;b
temp=s.x;
s.x=s.y; s.y=temp;
void main()
sample s;
clrscr();
cin>>x1>>x2;
s.setdata(x1,x2);
cout<<”\nbefore swapping”;
s.showdata();
swap(s);
s.showdata();
Output:
4) Write a program to find area and circumference of a circle using inline function.
Procedure:
#include<iostream.h>
#include<conio.h>
const pi=3.14;
return(2*pi*x);
return(pi*x*x);
void main()
float r;
clrscr();
cin>>r;
cout<<\n circumference:”<<circum(r);
cout<<”\narea:”<<area(r);
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
class matrix
private:
int mat[10][10];
int rc;
public:
matrix() {};
void size(int n)
rc=n;
void matinput();
void matprint();
};
for(int i=0;i<rc;i++)
for(int j=0;j<rc;j++)
cin>>mat[i][j];
void matrix::matprint()
for(int i=0;i<rc;i++)
for(int j=0;j<rc;j++)
cout<<mat[i][j]<<setw(6);
cout<<”\n”;
matrix matprod;
matprod.size(rc);
for(int j=0;j<rc;j++)
matprod.mat[i][j]=0;
for(int k=0;k<rc;k++)
matprod.mat[i][j]=matprod.mat[i][j]+mat[i][k]*m.mat[k][j];
return matprod;
void main()
clrscr();
matrix a,b,c;
int rc;
cin>>rc;
a.size(rc);
b.size(rc);
c.size(rc);
cout<<”\nmatrix a:”;
a.matinput();
cout<<”\nmatrix b:”;
b.matinput();
cout<<”\nmatrix a:”;
a.matprint();
cout<<”\nmatrix b:”;
b.matprint();
c=a*b;
c.matprint();
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queue
private:
int qu[max];
int front,rear;
public:
queue()
front=-1;
rear=-1;
void addq();
void delq();
void show();
};
int item;
if(rear==max-1)
cout<<endl<<”queue is full”;
return;
rear++;
cin>>item;
qu[rear]=item;
if(front==-1)
front=0;
int item;
if(front==-1)
cout<<endl<<”queue is empty”;
return;
item=qu[front];
if(front==rear)
front=rear=-1;
else
front++;
return;
void queue::show()
if(front==-1)
else
cout<<endl<<”queue elements:\n”;
for(int i=front;i<=rear;i++)
cout<<setw(4)<<qu[i];
void main()
int choice;
queue q;
clrscr();
do
cout<<”\n-----------------queue operations--------------------“;
cout<<”\n1. insert”;
cout<<”\n2.delete”;
cout<<”\n3.display”;
cout<<”\n4. exit”;
cin>>choice;
switch(choice)
case 1:q.addq();
break;
case 2: q.delq();
break;
case 3: q.show();
break;
case 4: exit(0);
default:cout<<”\nwrong choice!!”;
while(choice!=4);
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
class student
protected:
char name[20];
int reg;
public:
void getdata()
cin>>reg;
cout<<”type name:”;
cin>>name;
void showdata()
cout<<setprecision(2);
cout.setf(ios::left,ios::adjusted);
cout<<”\nroll no:”<<setw(3)<<reg;
cout<<”\nname:”<<setw(10)<<name;
};
protected:
int m1,m2,m3;
float perc;
public:
void getmarks()
cin>>m1>>m2>>m3;
perc=(m1+m2+m3)/300.0*100;
void showmarks()
cout<<”\nc++:”<<setw(5)<<m1;
cout<<”\nmath:”<<setw(5)<<m2;
cout<<”\ndbms:”<<setw(5)<<m3;
cout<<”\npercent:”<<setw(8)<<perc;
};
private:
char grade;
public:
void display()
showdata();
showmarks();
cout<<”\ngrade:”<<setw(10)<<getgrade()<<endl;
char getgrade()
if(m1<40||m2<40||m3<40)
grade=’f’;
else if(perc>75)
grade=’a’;
else if(perc>60)
grade=’b’;
else if(perc>50)
grade=’c’;
else if(perc>40)
grade=’d’;
else
grade=’f’;
return grade;
};
void main()
clrscr();
result std;
cout<<” b->60-74”<<endl;
cout<<” c->50-59”<<endl;
cout<<” d->40-49”<<endl;
std.getdata();
std.getmarks();
std.display();
getch();
Output:
8) Write a Program to find the area and volume of respective figures using
function overloading.
Procedure:
#include<iostream.h>
#include<conio.h>
class shape
public:
void measure(double r)
void measure(float f)
};
void main()
int b,h;
long l;
double r;
float f;
shape obj;
clrscr();
cin>>r;
obj.measure( r );
cin>>l>>b>>h;
obj.measure(l,b,h);
cin>>f;
obj.measure(f);
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
class rectangle
private:
int length;
int width;
public:
this->length=len;
int getlength()
return this->length;
void setwidth(int w)
width=w;
void getwidth()
return width;
};
void main()
rectangle rect;
clrscr();
rect.setlength(20);
rect.setwidth(10);
cout<<”and”<<rect.getwidth()<<”feet wide”<<endl;
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
Template<class S>
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
Template<class T>
t temp;
temp=x;
x=y;
y=temp;
void main()
int i,j,a[5]={25,2,56,,4,34};
float p[5]={25.25,3.45,4.2,20.5,5.8};
clrscr();
cout.precision(2);
for(i=0;i<5;i++)
cout<<a[i]<< ‘ ‘;
for(i=0;i<5;i++)
cout<<p[i]<<’ ‘ ;
bubble(a,5);
bubble(p,5);
for(i=0;i<5;i++)
cout<<a[i]<<’ ‘ ;
for(i=0;i<5;i++)
cout<<p[i]<<’ ‘ ;
getch();
Output:
PART
–B
PRO
GRA
MS
11) Write a program to illustrate class with member functions defined outside the
class.
Procedure:
#include<iostream.h>
#include<conio.h>
class employee
private:
char name[20];
int basic,,netpay;
public:
void gaetdata();
void showdata();
};
void employee::getdata()
cin.getline(name,20);
cin>>basic;
void employee::showdata()
netpay= basic+(basic*20/100)+(basic*10/100)-(basic*10/100);
cout<<”Net Salary=”<<netpay;
void main()
employee emp;
clrscr();
emp.getdata();
emp.showdata();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
class leap
private:
int year;
public:
int checkyear();
void getdata();
void showdata();
};
int leap::checkyear()
int r4=year%4;
int r100=year%100;
int r400=year%400;
return 1;
else
return 0;
void leap::getdata()
cin>>year;
void leap::showdata()
if(checkyear())
else
void main()
leap yr;
clrscr();
yr.getdata();
yr.showdata();
Output:
13) Write a program to find the roots of a quadratic equation using constructors
Procedure:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class quad
private:
float a,b,c;
float x1,x2;
public:
quad()
a=0;b=0;c=0;
void getdata()
cin>>a>>b>>c;
float disc()
return(b*b-4*a*c);
void root_equal()
cout<<”\n x1=x2=”<<-b/(2*a)<<endl;
void root_real()
x1=(-b+sqrt(disc()))/(2*a);
x2=(-b+sqrt(disc()))/(2*a);
cout<<”\nx1=”<<x1<<endl;
cout<<”\nx2=”<<x2<<endl;
void root_imag()
rpart=-b/(2*a);
ipart=sqrt(abs(disc()))/(2*a);
cout<<”\nx1=”<<rpart<<”+i”<<ipart<<endl;
cout<<”\nx2=”<<rpart<<”-i”<<ipart<<endl;
};
void main()
quad r1;
clrscr();
r1.getdata();
if(r1.disc()==0)
r1.root_equal();
elseif(r1.disc()>0)
r1.root_real();
else
r1.root_imag();
getch();
Output:
14) Write a program to add two distances given in feet and inches.
Procedure:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class dist
private:
int feet;
float inches;
public:
disc()
feet=0; inches=0.0;
feet=ft; inches=in;
void getdist()
cin>>feet;
cin>>inches;
void showdist()
cout<<feet<<”’-“<<inches<<”\””;
};
inches=(d1.inches+d2.inches)%12;
feet=d1.feet+d2.feet+(d1.inches+d2.inches)/12;
void main()
clrscr();
dist d1,d3;
d1.getdist();
dist d2(12,9);
d3.sumdist(d1,d2);
cout<<”\n d1=”;
d1.showdist();
cout<<”\n d2=”;
d2.showdist();
cout<<”\n d3=”;
d3.showdist();
getch();
Output:
15) Write a program to find maximum of two numbers using friend function.
Procedure:
#include<iostream.h>
#include<conio.h>
class max
private:
int x;
int y;
public:
void getdata()
cout<<endl<<”enter a number”;
cin>>x;
cin>>y;
void showdata()
cout<<endl<<”x is:”<<x;
cout<<endl<<”y is:”<<y;
};
int large(max m)
if(m.x>m.y)
return m.x;
else
return m.y;
void main()
max m;
int big;
clrscr();
m.getdata();
m.showdata();
cout<<endl<<”largest is:”<<large(m);
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
class clock
private:
int hr;
int min;
public:
hr=hh;
min=mm;
void showtime()
cout<<hr<<”hours and”<<min<<”minutes”<<endl;
};
clock clock::operator+(clock t)
clock temp;
temp.min=min+t.min;
temp.hr=hr+t.hr;
if(temp.min>=60)
temp.min=temp.min-60;
return temp;
void main()
clrscr();
clock t1,t2,t3;
t1.gettime(1,25);
t2.gettime(3,30);
t3=t1+t2;
t1.showtime();
t2.showtime
t3.showtime();
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
class date
private:
int day,month,year;
public:
date() {}
void getdate();
void showdate();
};
void date::getdate()
cin>>day>>month>>year;
void date::showdate()
cout<<day<<”/”<<month<<”/”<<year<<endl;
void main()
clrscr();
d1.getdate();
cout<<”\n date1:”;
d1.showdate();
cout<<”\n date2:”;
d2.showdate();
cout<<”\n date3:”;
d3.showdate();
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
private:
char name[50];
int length;
public:
void getdata()
cin>>name;
void show()
cout<<name<<endl;
int strlength(string s)
length=strlen(s.name);
};
void main()
clrscr();
string s1;
cout<<”enter string:”;
s1.getdata();
cout<<”given string:”;
s1.show();
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
class matrix
private:
int mat[10][10];
int rc;
public:
matrix() {};
void size(int n)
rc=n;
void matinput();
void matprint();
};
void matrix::matinput()
cout<<”\n type”<<rc*rc<<”elements”;
for(int i=0;i<rc;i++)
for(int j=0;j<rc;j++)
cin>>mat[i][j];
void matrix::matprint()
for(int i=0;i<rc;i++)
for(int j=0;j<rc;j++)
cout<<mat[i][j]<<setw(6);
cout<<”\n”;
matrix matsum;
matsum.size(rc);
for(int i=0;i<rc;i++)
for(int j=0;j<rc;j++)
matsum.mat[i][j]=mat[i][j]+m.mat[i][j];
return matsum;
void main()
clrscr();
matrix a,b,c;
int rc;
cin>>rc;
a.size(rc);
b.size(rc);
c.size(rc);
a.matinput();
b.matinput();
c.matinput();
b.matprint();
a.matprint();
c.matprint();
getch();
Output:
Procedure:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
private:
char str[80];
public:
void show()
cout<<str;
void getdata()
cin>>str;
};
string temp;
strcpy(temp.str, s1.str);
strcat(temp.str,s2.str);
return(temp);
void main()
clrscr();
string s1,s2,s3;
s1.getdata();
s2.getdata();
cout<<endl<<”string 1: “;
s1.show();
cout<<endl<<”string 2: “;
s2.show();
s3=s1+s2;
cout<<endl<<”concatenated string:”;
s3.show();
getch();
Output: