5 Sem Lab BSC Programs - C++
5 Sem Lab BSC Programs - C++
----------------------------------------------------------------------------------------------------------------------------------------------------
PROGRAM NO :1
To illustrate class with member function defined outside/inside the class: -To accept and print employee
information – name, designation & basic salary
----------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
class employee
{
char empname[10],designation[10];
float basic;
public : void display();
void read()
{
cout<<"\nEnter employee name : ";
cin>>empname;
cout<<"\nEnter employee designation : ";
cin>>designation;
cout<<"\nEnter basic salary : ";
cin>>basic;
}
};
void employee::display()
{
cout<<" "<<empname;
cout<<" "<<designation;
cout<<" "<<basic;
}
void main()
{
clrscr();
employee e;
e.read();
cout<<" EmpName Designation Basic Salary\n";
cout<<"-----------------------------------------------\n";
e.display();
getch();
}
1
Department of Computer Science Fifth Semester Lab Programs
--------------------------------------------OUTPUT------------------------------------------------
2
Department of Computer Science Fifth Semester Lab Programs
----------------------------------------------------------------------------------------------------------------------------------------------------
PROGRAM NO :2
To illustrate an array of objects: To accept and print three employees information – name, designation & basic pay
----------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
class employee
{
int empno;
char empname[10],designation[15];
float basic;
public :
void read()
{
cout<<"\nEnter employee name : ";
cin>>empname;
cout<<"\nEnter employee designation : ";
cin>>designation;
cout<<"\nEnter basic salary : ";
cin>>basic;
}
void display()
{
cout<<" \t"<<empname;
cout<<"\t"<<designation;
cout<<" \t"<<basic<<"\n";
}
};
void main()
{
clrscr();
int n,i;
employee e[20];
cout<<"\nEnter the number of employees : ";
cin>>n;
for(i=0;i<n;i++)
e[i].read();
cout<<" EmpName Designation Basic Salary\n";
cout<<"-----------------------------------------------\n";
for(i=0;i<n;i++)
e[i].display();
cout<<"\n----------------------------------------------";
getch();
}
3
Department of Computer Science Fifth Semester Lab Programs
--------------------------------------------OUTPUT------------------------------------------------
----------------------------------------------
4
Department of Computer Science Fifth Semester Lab Programs
----------------------------------------------------------------------------------------------------------------------------------------------------
PROGRAM NO :3
To illustrate the friend function: -Create a class to hold information for a customer about his SB account & Current
account in a bank. Using friend function, find the total balance of both the accounts.
----------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
class current;
class savings
{
private: char name[20];
int acno;
long int bal;
public: void read();
void write();
friend long int total(savings,current);
};
void savings:: read()
{
cout<<"enter name,accno and balance"<<endl;
cin>>name>>acno>>bal;
}
void savings:: write()
{
cout<<"name=" <<name<<endl;
cout<<"acno=" <<acno<<endl;
cout<<"balance=" <<bal<<endl;
}
class current
{
private: char name[20];
int acno;
long int bal;
public: void read();
void write();
friend long int total(savings,current);
};
void current:: read()
{
cout<<"enter name,accno and balance"<<endl;
cin>>name>>acno>>bal;
}
void current:: write()
{
cout<<"name=" <<name<<endl;
cout<<"acno=" <<acno<<endl;
cout<<"balance=" <<bal<<endl;
}
5
Department of Computer Science Fifth Semester Lab Programs
long int total(savings s,current c)
{
return(s.bal+c.bal);
}
void main()
{
savings s;
current c;
cout<<"enter the savings name,acnoand balance"<<endl;
s.read();
s.write();
cout<<"enter the current name,acno and balance"<<endl;
c.read();
c.write();
cout<<"total balance of ac="<<total(s,c);
getch();
}
--------------------------------------------OUTPUT------------------------------------------------
6
Department of Computer Science Fifth Semester Lab Programs
----------------------------------------------------------------------------------------------------------------------------------------------------
PROGRAM NO: 4
To illustrate Constructors & Destructors: Create a Bank database (which includes customer name, Account type, Account
number & balance amount) using i) Constructors ii) destructors iii) default constructors v) input/output functions for 5
peoples.
----------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
class bank
{
char name[10],acctype[10];
int accno,balance;
public:
bank()
{
balance=500;
}
bank (int b)
{
balance=b;
}
~bank()
{
cout<<"\n customer account deleted";
}
void get()
{
cout<<"\n enter customer name,acc type and accno:\n";
cin>>name>>acctype>>accno;
}
void put()
{
cout<<"\n customer name:"<<name;
cout<<"\n A/c type:"<< acctype;
cout<<"\n A/c no:"<<accno;
cout<<"\n balance:"<<balance;
}
};
void main()
{
clrscr();
bank b1,b2(12000),b3(15000),b4(20000),b5(25000);
cout<<"enter details for 5 people\n";
b1.get(); b2.get(); b3.get(); b4.get(); b5.get();
cout<<"\n customer details\n";
b1.put(); b2.put(); b3.put(); b4.put(); b5.put();
getch();
}
7
Department of Computer Science Fifth Semester Lab Programs
--------------------------------------------OUTPUT------------------------------------------------
Enter details for 5 people
enter customer name,acc type and accno:
Sangeeetha
savings
100
enter customer name,acc type and accno:
Noothana
saving
200
enter customer name,acc type and accno:
Gowri
current
300
enter customer name,acc type and accno:
Nisarga
saving
400
enter customer name,acc type and accno:
sambrama
current
500
customer details
customer name:Sangeeetha
A/c type: savings
A/c no:100
balance:500
customer name:Noothana
A/c type: savings
A/c no:200
balance:12000
customer name: Gowri
A/c type: current
A/c no:300
balance:15000
customer name: Nisarga
A/c type: saving
A/c no:400
balance:20000
customer name: sambrama
A/c type: current
A/c no:500
balance:25000
8
Department of Computer Science Fifth Semester Lab Programs
----------------------------------------------------------------------------------------------------------------------------------------------------
PROGRAM NO: 6
To illustrate operator overloading: Program to find the next date of given date by overloading ++ operator.
----------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
class date
{
private:int dd,mm,yy;
public:void read()
{
cin>>dd>>mm>>yy;
}
void display()
{
cout<<dd<<"/"<<mm<<"/"<<yy<<endl;
}
void operator++()
{
dd++;
if(dd>31)
{
if(mm==12)
{
dd=1;
mm=1;
yy++;
}
else
{
dd=1;
mm++;
}
}
else
if(((mm==4)||(mm==6)||(mm==9)||(mm==11))&&(dd>30))
{
dd=1;
mm++;
}
else
if((mm==2)&&(yy%4==0)&&(dd>29))
{
dd=1;
mm++;
}
else
if((mm==2)&&(yy%4!=0)&&(dd>28))
{
dd=1;
mm++;
}
9
Department of Computer Science Fifth Semester Lab Programs
}
friend int valid(date D);
};
int valid(date D)
{
if((D.dd<=0)||(D.dd>31)||(D.mm<=0)||(D.mm>12)||(D.yy==0))
return(0);
else
if(((D.mm==4)||(D.mm==6)||(D.mm==9)||(D.mm==11))&&(D.dd>30))
return(0);
else
if((D.mm==2)&&(D.yy %4==0)&&(D.dd>29))
return(0);
if((D.mm==2)&&(D.yy %4!=0)&&(D.dd>28))
return(0);
else
return(1);
}
void main()
{
clrscr();
date D;
cout<<"enter date in day,month and year"<<endl;
D.read();
if(valid(D))
{
cout<<"the original date"<<endl;
D.display();
++D;
cout<<"next date is "<<endl;
D.display();
}
else
cout<<"invalid date"<<endl;
}
-------------------------------------------------OUTPUT------------------------------------------------
enter date in day,month and year
28 8 96
the original date
28/8/96
next date is
29/8/96
10
Department of Computer Science Fifth Semester Lab Programs
----------------------------------------------------------------------------------------------------------------------------------------------------
PROGRAM NO: 7
To illustrate operator overloading:-Interactive Program to find sum, difference and product of two complex numbers by
overloading +, -,* operators.
----------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<string.h>
class complex
{
int i,r;
public:
void read()
{
cout<<"\nEnter Real Part:";
cin>>r;
cout<<"Enter Imaginary Part:";
cin>>i;
}
void display()
{
cout<<"\n= "<<r<<"+"<<i<<"i";
}
complex operator+(complex a2)
{
complex a;
a.r=r+a2.r;
a.i=i+a2.i;
return a;
}
complex operator-(complex a2)
{
complex a;
a.r=r-a2.r;
a.i=i-a2.i;
return a;
}
complex operator*(complex a2)
{
complex a;
a.r=(r*a2.r)-(i*a2.i);
a.i=(r*a2.i)+(i*a2.r);
return a;
}
};
void main()
{
int ch;
clrscr();
complex a,b,c;
do
11
Department of Computer Science Fifth Semester Lab Programs
{
cout<<"\n1.Addition 2.Substraction";
cout<<" 3.Mulitplication 4.Exit\n";
cout<<"\nEnter the choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=a+b;
c.display();
break;
case 2:
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=b-a;
c.display();
break;
case 3:
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=a*b;
c.display();
break;
}
}while(ch!=4);
getch();
}
12
Department of Computer Science Fifth Semester Lab Programs
-------------------------------------------------OUTPUT------------------------------------------------
13
Department of Computer Science Fifth Semester Lab Programs
----------------------------------------------------------------------------------------------------------------------------------------------------
PROGRAM NO: 8
To illustrate inheritance:-Program to create a base class for stack and implement push and pop operations. Include
derived class to check for stack criteria (stack is full and stack is empty) .
----------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
class STACK
{
protected:
int a[5];
int top;
public:
STACK()
{
top=-1;
}
void push(int val)
{
a[++top]=val;
}
int pop()
{
return a[top--];
}
void display();
};
void STACK::display()
{
cout<<"\n The stack is. . . \n";
for(int i=0;i<=top;i++)
cout<<"\t"<<a[i];
}
//CLASS DECLARATION OF STACK1 INHERITED FROM STACK
class STACK1:public STACK
{
public:
STACK1()
{
top=-1;
}
int empty() //TO CHECK WHETHER STACK IS EMPTY OR NOT
{
if(top==-1)
return 1;
return 0;
14
Department of Computer Science Fifth Semester Lab Programs
}
int full() //TO CHECK WHETHER STACK IS FULL OR NOT
{
if(top==4)
return 1;
return 0;
}
void push(int val)
{
if(full())
cout<<"\n The stack is FULL. . ";
else
STACK::push(val);
}
int pop()
{
if(empty())
return NULL;
else
return (STACK::pop());
}
void display()
{
if(empty())
cout<<"\n The stack is empty. . .";
else
STACK::display();
}
};
void main()
{
STACK1 st;
int val,top;
do
{
int ch;
clrscr();
cout<<"\n-------------------------";
cout<<"\n MENU ";
cout<<"\n-------------------------";
cout<<"\n1) PUSH";
cout<<"\n2) POP";
cout<<"\n3) DISPLAY ";
cout<<"\n4) EXIT";
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
15
Department of Computer Science Fifth Semester Lab Programs
case 1:
if(top==4)
cout<<"STACK IS FULL";
else
{
cout<<"Enter the value to push: ";
cin>>val;
st.push(val);
st.display();
}
break;
case 2: val=st.pop();
if(val==NULL)
cout<<"STACK IS EMPTY";
else
{
cout<<"The item poped is :"<<val;
st.display();
}
break;
case 3: st.display();
break;
case 4: return;
}
getch();
}while(1);
}
16
Department of Computer Science Fifth Semester Lab Programs
---------------------------------------------------------------------------------------------------------------------------------------------------
PROGRAM NO: 9
To illustrate polymorphism:- To create a base class called G-F with a, b & area as its public members. Two new classes
namely Triangle & Rectangle are inherited from the base class. The base class contains two member functions called get()
& display() to read the data & print them. Develop an interactive program to accept dimensions and appropriate area
----------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
class GF
{
public: int a,b;
double area;
void get()
{
cout <<" enter a,b";
cin>>a>>b;
}
virtual void display()
{
cout<<"A="<<a<<endl;
cout<<"B="<<b<<endl;
}
};
class Tri:public GF
{
public: void display()
{
area=0.5*a*b;
cout<<" area of a triangle:\n"<<area<<endl;
}
};
class Rect:public GF
{
public: void display()
{
area=a*b;
cout<<"area of a rectangle:\n" <<area<<endl;
}
};
void main()
{
GF g;
clrscr();
Tri t;
17
Department of Computer Science Fifth Semester Lab Programs
Rect r;
GF *ptr;
int ch;
do
{
cout<<"enter your choice:\n";
cout<<"1.Area of triangle:\n";
cout<<"2.Area of rectangle:\n";
cout<<"3.Exit";
cin>>ch;
switch(ch)
{
case 1: ptr=&t;
ptr->get();
ptr->display();
break;
case 2: ptr=&r;
ptr->get();
ptr->display();
break;
}
} while(ch!=3);
getch();
}
-------------------------------------------------OUTPUT------------------------------------------------
enter your choice:
1.Area of triangle:
2.Area of rectangle:
3.Exit
1
enter a,b5
6
area of a triangle:
15
enter your choice:
1.Area of triangle:
2.Area of rectangle:
3.Exit
2
enter a,b
6
3
area of a rectangle:
18
enter your choice:
1.Area of triangle:
2.Area of rectangle:
3.Exit
18
Department of Computer Science Fifth Semester Lab Programs
3
19