Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PG OOPS

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 49

NMS SERMATHAI VASAN COLLEGE FOR

WOMEN
Avaniyapuram
Madurai-12

DepartmentOf CS IT& BCA

CLASS :I-M.SC(CS)
SUBJECT : OOPS LAB
SUBJECTCODE :
YEAR : 2023 - 2024
NMS SERMATHAI VASAN COLLEGE FOR WOMEN
Avaniyapuram
Madurai-12

BONAFIDE CERTIFICATE

Reg No: Class : I-MSC (CS)


Subject: OOPS LAB Code :

This is certified to be BONAFIDE record workdone by


__________________________ submitted for the practical
examination held at, NMS SERMATHAI VASAN COLLEGE FOR
WOMEN, MADURAI.

Date: StaffIncharge

ExternalExaminer ExternalExaminer
INDEX
S.NO DATE TOPIC PAGE NO SIGNATURE
1 Stack using array

2 Stack using linked list

3 Merge short

4 Queue using array

5 Queue using linked list

6 Heap tree

7 Tree traversal

8 Greedy method

9 2 Queen attack

10 Friend function

11 Inline function

12 Virtual function

13 Function overloading

14 Single inheritance

15 Multilevel inheritance

16 Multiple inheritance

17 Hierarchical inheritance

18 Constructor

19 Electricity bill
EX NO : 1

STACK USING ARRAY


AIM :

To write a c++ program for stack using array.

SOURCE CODE :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{
int stack1[10];
int top;
public:
stack()
{
Top=-1;
}
void push(int x)
{
if(top>4)
{
Cout<<”stack over flow”;
return;
}
stack1[++top]=x;
cout<<”Inserted”<<x;
}
void pop()
{
if(top<0)

{
Cout<<”stack under flow”;
return;
cout<<”deleted”<<stack1[top--];
}
Void display()
{
if(top<0)
{
Cout<<”stack is empty”;
return;
}
For(int i=top;i>=0;i--)
{
Cout<<stack[i]<<””;
}
}
};
Void main()
{
Int ch;
Stack s;
Clrscr();
While(1)
{
Cout<<”\n 1.push\n 2.pop\n 3.display\n 4.exit\n”;
Cout<<”enter your choice”;
Cin>>ch;
Switch(ch)
{
Case 1:cout<<”Enter the element”;

Cin>>ch;
s.push(ch);
break;
case2:s.pop();
break;
case3:s.display();
break;
case4:exit(0);
break;
}
}
getch();
}
OUTPUT :
1. Push
2. Pop
3. Display
4. Exit
Enter Your Choice : 1
Enter The Element : 12
Inserted 12
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 1
Entr The Element : 23
Inserted 23
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 3
23 12
1. Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 2
Deleted 23
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 3
12
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 4

RESULT :

Thus the program was executed successfully.


EX NO :2

STACK USING LINKED LIST


AIM:

To write a c++ program for stack using linked list.

SOURCE CODE :

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
Struct node
{
Int data;
Struct node*next;
};
Class stack
{
Struct node*top;
Public:
Stack()
{
Top=NULL;
}
Void push();
Void pop();
Void show();
};
Void stack::push()
{
Int value;
Struct node*ptr;
Cou<<”PUSH OPERATION”;

Cout<<”enter thee element inserted”;


Cin>>value;
Ptr=new node;
Ptr->data=value;
Pytr->next=NULL;
If(top!=NULL)
Ptr->next=top;
Top=ptr;
Cout<<”new item is inserted to the stack”;
}
Void stack::pop()
{
Struct node*temp;
If(tpo==NULL)
{
Cout<<”the stack is empty”;
}
Temp=top;
Top=top->next;
Cout<<”poped value is “<<temp->data;
Delete temp;
}
Void stack::show()
{
Struct node*ptr1=top;
Cout<<”\n the stack is:\n”;
While(ptr1!=NULL)
{
Cout<<”ptr1->data<<”->”;
Ptr1=ptr1->next;
}

Cout<<”NULL\n”;
}
Void main()
{
Int ch;
Stack s;
Clrscr();
While(1)
{
Cout<<”\n 1.push\n 2.pop\n 3.display\n 4.exit\n”;
Cout<<”enter your choice”;
Cin>>ch;
Switch(ch)
{
Case 1: s.push(ch);
break;
case2:s.pop();
break;
case3:s.display();
break;
case4:exit(0);
break;
}
} getch();
OUTPUT :

1. Push
2. Pop
3. Display
4. Exit
Enter Your Choice : 1
PUSH OPERATION
Enter The Element to Insert : 12
New item is inserted to the stack
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 1
PUSH OPERATION
Enter The Element to Insert : 23
New item is inserted to the stack
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 3
PUSH OPERATION
Enter The Element to Insert : 34
New item is inserted to the stack
1. Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 3
The stack is:
34 -> 23-> 12 -> NULL
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 2
Poped value is :34
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 3
The stack is :
23 -> 12 -> NULL
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 4

RESULT :

Thus the program was executed successfully.


EX NO:

MERGE SHORT

AIM :
To write a python program for merge short.

SOURCE CODE :
EX NO :4
QUEUE USING ARRAY
AIM:
To write a c++ program for queue using array.

SOURCE CODE :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queue
{
int queue1[10];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear>4)
{
Cout<<”queue over flow”;
Front=rear=-1;
return;
}
queue1[++rear]=x;
cout<<”Inserted”<<x;
}
void delete()

{
if(front==rear)
{
cout<<”queue under flow”;
return;
cout<<”deleted”<<queue1[++front];
}
void display()
{
if(rear==front)
{
cout<<”queue is empty”;
return;
}
for(int i=frony;i<=rear;i++)
{
cout<<queue1[i]<<””;
}
}
};
void main()
{
int ch;
queue q;
clrscr();
while(1)
{
cout<<”\n 1.insert\n 2.delete\n 3.display\n 4.exit\n”;
cout<<”enter your choice”;
cin>>ch;
switch(ch)

{
case 1:cout<<”enter the element”;
cin>>ch;
q.insert(ch);
break;
case2:q.delete();
break;
case3:q.display();
break;
case4:exit(0);
break;
}
}
getch();
}
OUTPUT :
1. Insert
2. Delete
3. Display
4. Exit
Enter Your Choice : 1
Enter The Element : 12
Inserted 12
1.Insert
2.Delete
3.Display
4.Exit
Enter Your Choice : 1
Entr The Element : 23
Inserted 23
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 3
23 12
1. Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 2
Deleted 23
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 3
12
1.Push
2.Pop
3.Display
4.Exit
Enter Your Choice : 4

RESULT :

Thus the program was executed successfully.


EX NO : 5
QUEUE USING LINKED LIST
AIM:

To write a c++ program for queue using linked list.

SOURCE CODE :

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
Struct node
{
Int data;
Struct node*next;
};
Struct node*rear,*front;
Public:
queue()
{
rear=NULL;
front=NULL;
}
Void insert();
Void delete();
Void show();
};
Void queue::insert()
{
Int value;
Struct node*ptr;
Cou<<”INSERT OPERATION”;
Cin>>value;

Ptr=new node;
Ptr->data=value;
Pytr->next=NULL;
If(front==NULL)
front=ptr;
else
rear->next=ptr;
rear=ptr;
Cout<<”\n new item is inserted to the queue”;
}
Void queue::deleted()
{
Struct node*temp;
If(front==NULL)
{
Cout<<”\n the queue is empty!!”;
}
Else
{
Temp=front;
front=temp->next;
cout<<”DELETE OPERATION\n”;
Cout<<”delete value is “<<temp->data;
Delete temp;
}
Void queue::show()
{
Struct node*ptr1=front;
Cout<<”\n the queue is:\n”;
While(ptr1!=NULL)

{
Cout<<”ptr1->data<<”->”;
Ptr1=ptr1->next;
}
Cout<<”NULL\n”;
}
Void main()
{
Int ch;
Queue q;
Clrscr();
While(1)
{
Cout<<”\n 1.INSERT\n 2.DELETE\n 3.DISPLAY\n 4.EXIT\n”;
Cout<<”enter your choice”;
Cin>>ch;
Switch(ch)
{
Case1:q.insert();
break;
case2:q.delete();
break;
case3:q.display();
break;
case4:exit(0);
break;
}
}
getch();
}

OUTPUT :
1.insert

2.delete

3.show

4.exit

Enter your choice : 1

INSERT OPERATION

Enter the element to insert : 22

New item is inserted in the queue

1.insert

2.delete

3.show

4.exit

Enter your choice : 2

DELETE OPERATION

Delete value is : 22

1.insert

2.delete

3.show

4.exit

Enter your choice : 3

SHOW OPERATION
The queue is NULL

1.insert

2.delete

3.show

4.exit

Enter your choice : 4

RESULT :

Thus the program was executed successfully.


EX NO : 6
HEAP TREE
AIM :
To write a python program for heap tree.

SOURCE CODE :

#include<iostdio.h>
#include<conio.h>
void heapsort(iont[],int);
void buildheap(int[],int);
void satidsfyheap(int[],int,int);
void main()
{
clrscr();
int a[10],i,size;
cout<<"Enter the size of List\n";
cin>>size;
cout<<"Enter"<<size<<"element \n";
for(i=0;i<size;i++)
{
cin>>a[i];
}
heapsort
getch();
void heapsort(int a[],int length)
{
buildheap(a,length);
int heapsize,i,temp;
heapsize=length-1;
for(i=heapsize;i>=0;i--)
{
tem=1[0]
a[0]=a[heapsize];
a[heapsize]=temp;
heapsize--;
satisfyheap(a,o,heapsize);
}
for(i=0;i<length;i++)
{
cout<<"\t'<<a[i];
}
}
void buildheap(int a[],int length)
{
int i,heapsize;
heapsize=length-1;
for(i=(length/2);i>=0;i--)
{
satisfyheap(a,i,heapsize);
}
}
void satisfyheap(int a[],int i,int heapsize)
{
int l,r,largerst,temp;
L=2*i;
r=2*i=1;
if(1<heapsize && a[1]>a[i])
{
Largerst=1;
}
else
{
Largerst=i;
}
if(r<=heapsize && a[r]>a[Largert])
{
Largerst=r;
}
if(Largerst!=i)
{
temp=a[i];
a[i]=a[Largest];
a[Largerst]=temp;
satisfyheap(a,Largerst,heapsize);
}
}
OUTPUT :
Enter the sze of list

Enter 6 elements

34

65

54

78

23

12

78 65 54 34 23 12

RESULT :

Thus the program was executed successfully.


EX NO : 7

TREE TRAVERSAL
AIM :
To write a python program for tree traversal.

SOURCE CODE :
EX NO : 10
FRIEND FUNCTION

AIM :
To write a c++ program for friend function.

SOURCE CODE :

#include<iostream.h>
#include<conio.h>
class mean
{
int x,y;
public:
void assign(int a,int b);
friend int output(mean m);
};
void mean :: assign(int a,int b)
{
x=a;
y=b;
}
int output(mean m)
{
int z;
z=(m.x+m.y)/2;
return(z);
}
void main()
{
int a. b;
mean m;
clrscr();
cout<<endl<<”Enter two value to find mean”<<endl;
cout<<endl<<”Number 1”<<endl;
cin>>a;
cout<<endl<<”Number 2”<<endl;
cin>>b;
m.assign(a,b);
cout<<”mean of two numbers:”<<a<<”and”<<b<<”=”<<output(m);
getch();
}
OUTPUT :

Enter two value to find mean


Number 1
23
Number 2
34
mean of two numbers :23 and 34=28

RESULT :

Thus the program was executed successfully.


EX NO :11
INLINE FUNCTION

AIM :

To write a c++ program for inline function.

SOURCE CODE :

#include<iostream.h>
#include<conio.h>
inline int add(int a1,int b1)
{
return(a1+b1);
}
inline float mul(float x1,float y1)
{
return(x1*y1);
}
inline doublediv(double p1,double q1)
{
return(p1/q1);
}
void main()
{
int a,b;
float x,y;
double p,q;
clrscr();
cout<<”Enter the value of addition\n”;
cin>>a>>b;
cout<<”Enter the value of multiplication\n”;
cin>>x>>y;
cout<<”Enter the value of division\n”;
cin>>p>>q;
cout<<”Addition=”<<add(a,b)<<”\n”;
cout<<”Multiplication=”<<mul(x,y)<<”\n”;
cout<<”Division=”<<div(p,q)<<”\n”;
getch();
}
OUTPUT :

Enter the value of addition

10 40

Enter the value of multiplication

45.6 70.4

Enter the value of division

12345 56789

Addition=50

Multiplication=3210.23999

Division=0.217384

RESULT :

Thus the program was executed successfully.


EX NO :12
VRITUAL FUNCTION

AIM :

To write a c++ program for inline function.

SOURCE CODE :

#include<iostream.h>
#include<conio.h>
class colour
{
public:
vritual void print()
{
cout<<”blue color”<<endl;
}
};
class d;public color
{
public:
void print()
{
cout<<”pinck color”<<endl;
}
};
int main()
{
clrscr();
color*b1;
D d;
b1=&d;
b1->print();
getch();
return 0;
}
OUTPUT :

Pinck color

RESULT :

Thus the program was executed successfully.


EX NO :13

FUNCTION OVERLOADING
AIM :

To write a c++ program for inline function.

SOURCE CODE :
#include<iostream.h>
#include<conio.h>
float area(int a)
{
return a*a;
}
float area(float rad)
{
return 3.14*(rad*rad);
}
float area(int l,int b)
{
return l*b;
}
void main()
{
int ch,a,l,b;
float r;
clrscr();
cout<<”1.square\n2.circle\n3.rectangle\n”;
cout<<”Enter choice:”;
cin>>ch;
if(ch==1)

{
cout<<”enter the value:”;
cin>>a;
cout<<”\nArea of square:”<<area(a);
}
else if(ch==2)
{
cout<<”Enter radius for circle:”;
cin>>r;
cout<<”\nArea of circle is:”<<area(r);
}
else
{
cout<<”Enter length:”;
cin>>l;
cout<<”Enter breadth:”;
cin>>b;
cout<<”Area of rectangle is:”<<area(l,b);
}
getch();
}
OUTPUT:

1.square

2.circle

3.rectangle

Enter choice:1

Enter a value:10

Area of square:100

RESULT :

Thus the program was executed successfully.


EX NO :14

SINGLE INHERITANCE
AIM :

To write a c++ program for single inheritance

SOURCE CODE :

#include<iostream.h>
#include<conio.h>
class base
{
int reg no;
public:
char course[10];
int no;
void assign();
};
class derived: public base
{
int m1,m2,m3,tot;
public:
void input();
void display();
};
void base::assign()
{
cout<<”enter reg no”<<endl;
cin>>reg no;
no= reg no;
cout<<”enter course”<<endl;

cin>>course;
}
void derived::input()
{
cout<<”enter the marks”;
cin>>m1>>m2>>m3;
}
void derived::display()
{
cout<<”reg.no=”<<no<<endl;
cout<<”course=”<<course<<endl;
cout<<”mark1=”<<m1<<endl;
cout<<”mark2=”<<m2<<endl;
cout<<”mark3=”<<m3<<endl;
cout<<”total=”<<m1+m2+m3;
}
void main()
{
clrscr();
derived d;
d.assign();
d.input();
d.display();
getch();
}
OUTPUT :
Enter reg no:

Enter course:

Enter the mark=98

99

100

reg no=3

course=6

mark1=98

mark2=99

mark3=100

total=287

RESULT :
Thus the program was executed successfully.
EX NO :15

MULTILEVEL INHERITANCE
AIM :

To write a c++ program for multilevel inheritance

SOURCE CODE :

#include<iostream.h>
#include<conio.h>
class invent
{
public:
int ino,pri,qty;
char ina[20];
void get()
{
cout<<”\n item number”;
cin>>ino;
cout<<”\n item name”;
cin>>ina;
cout<<”\n item price”;
cin>>pri;
cout<<”\n quantity”;
cin>>qty;
}
};
class discount:public invent
{
public:
int dis,amt,p;

void calc()
{
if(pri<100)
dis=(pri*10)/100;
else
dis=(pri*20)/100;
p=pri-dis;
amt=p*qty;
}
};
class display:public discount
{
public:
void print()
{
cout<<”\t item number \t price\t quantity\t discount\t amount\n\n”;
cout<<”\t”<<ino<<”\t\t”<<ina<<”\t\t”<<pri<<”\t\t”<<qty<<”\t\t”<<(dis*qty)<<”\t\t”<<amt;
}
};
void main()
{
display d;
clrscr();
d.get();
d.calc();
d.print();
getch();
}
OUTPUT :

Item number 1

Item name berry

Item prices 20

Quantity 500

Item number Name Price Quantity Discount Amount

1 berry 20 500 1000 900

RESULT :

Thus the program was executed successfully.


EX NO :16

MULTIPLE INHERITANCE

AIM :

To write a c++ program for multilevel inheritance.

SOURCE CODE :
#include<iostream.h>
#include<conio.h>
class base1
{
public:
int reg;
char name[10];
void get()
{
cout<<”\n register number:’;
cin>>reg;
cout<<”\n name:”;
cin>>name;
}
};
class base2
{
public:
int m1,m2;
void read()
{
cout<<”\n enter marks:\n”;
cin>>m1>>m2;

}
};
class derived:public base1,public base2
{
int tot,avg;
public:
void process()
{
tot=m1+m2;
avg=tot/2;
}
void display()
}
cout<<”\n register number:”<<reg;
cout<<”\n name:”<<name;
cout<<”\n mark1:”<<m1;
cout<<”\n mark2:”<<m2;
cout<<”\n total:”<<tot;
cout<<”\n average:”<<avg;
}
};
void main()
{
derived d;
clrscr();
d.get():
d.read();
d.process();
d.display();

getch();
}
OUTPUT :

register number: 1

name : rathi

enter mark: 90

99

Register number : 1

Name : rathi

Mark1 : 90

Mark2 : 99

Total : 189

Average : 94

RESULT :
Thus the program was executed successfully.
EX NO :17

HIERARCHICAL INHERITANCE.
AIM :
To write a c++ program for employee details using hierarchical inheritance.

SOURCE CODE :
#include<iostream.h>
#include<conio.h>
long int g,d;
class base-emp
{
protected:
long int empno,basic;
char empna[10];
public:
void get()
{
cout<<endl<<”employee number:”;
cin>>empno;
cout<<endl<<”employee name:”;
cin>>empna;
cout<<endl<<”basic pay:”;
cin>>basic;
}
};
class pay-calc:public base-emp
{
public:
long int da,hra,pf,b;

void calc()
{
b=basic;
da=(b*60)/100;
hra=(b*40)/100;
pf=(b*20)/100;
g=b+da+hra;
d=g-bf;
}
};
class grs::public pay-calc
{
public:
void put()
{
cout<<”\n\n gross pay”<<g;
}
};
class net:public pay-calc
{
public:
void display()
{
cout<<”\n\n net pay”<<d;
}
};
class emp-ded:public pay-calc
{
public:

void display()
{
cout<<endl<<”\n\n employee number:”<<empno;
cout<<endl<<”\n\n employee name:”<<empna;
cout<<endl<<”\n\n basic salary:”<<basic;
cout<<endl<<”\n\n dearness allowaness:”<<da;
cout<<endl<<”\n\n house rebt allowaness:”<<hra;
cout<<endl<<”\n\n provident fund:”<<pf;
}
};
void main()
{
grs g;
net n;
emp-det e;
clrscr();
e.get():
e.calc();
e.display():
g.put();
n.display();
getch():
}
OUTPUT :
Employee number : 4

Employee name : padma

Basic pay : 20,000

Employee number : 4

Employee name : padma

Basic salary : 20,000

Dearness allowance : 12,000

House rent allowance : 8000

Provident fund : 4000

Gross pay : 40,000

Net pay : 36000

RESULT :
Thus the program was executed successfully.
Ex No: 18

CONSTRUCTOR
AIM :
To write a c++ program for constructor.

SOURCE CODE :
#include<iostream.h>
#include<conio.h>
int cnt=0;
class demo
{
public:
demo()
{
cnt++;
cout<”\n constructor”<<cnt;
getch();
}
~demo()
{
cout<<”\n destructor”<<cnt;
cnt--;
getch();
}
};
void main()
{
clrscr();
demo d()

demo d2;
demo d3;
demo d4;
getch();
}
OUTPUT :

Constructor 1

Constructor 2

Constructor 3

Constructor 4

destructor 4

destructor 3

destructor 2

destructor 1

RESULT :
Thus the program was executed successfully.
EX. NO: 19

ELECTRICITY BILL
AIM:
To write a c++ program for electricity bill using binary file concept.

SOURCE CODE :
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<iomanip.h>
#include<string.h>
class electric
{
char cat[20];
int eb-no,cr,pr,unit;
float amt;
public:
void readdata(void);
void calc(void);
void display(void);
};
void electric::readdata();
{
cout<<”\n”<<”enter Eb number:”;
cin>>eb-no;
cout<<”\n”<<”enter category:”;
cin>>cat;
cout<<”\n “<<”enter current reading:”;
cin>>cr;

cout<<”\n”<<”enter previous reading:”;


cin>>pr;
}
void electric::calc()
{
unit=cr-pr;
if(str cmp(cat,”domestic”)==0)
{
if(unit<100)
amt=unit*1.00;
else
amt=unit*2.00;
}
else if(strcmp(cat,”{industry”)==0)
{
if(unit<100)
amt=unit*4.00;
else
amt=unit*8.00;
}
}
void electric::display()
{
cout<<”\n”<<”Eb number:”<<Eb-no<<”\n”;
cout<<”\n”<<”current reading:”<<cr<<”\n”;
cout<<”\n”<<”previous reading:”<<pr<<”\n”;
cout<<”\n”<<”amount:”<<amt<<”\n”;
cout<<”\n”<<”unit:”<<unit<<”\n”;
}

void main()
{
electric e;
clrscr();
ofstream outfile;
out file.open(“BILL.Dat”,ios::binary);
e.readdata();
e.calc();
out file.write((char*)&e,sizeof(e));
outfile.close();
ofstream infile;
infile.open(“BILL.Dat”,ios::binary);
infile.write((char*)&e,sizeof(e));
e.display();
infile.close();
getch();
}
OUTPUT :

Enter Eb number : 100

Enter category : industry

Enter current reading : 600

Enter previous reading : 400

Eb number : 100

Current reading :600

Previous reading : 400

Amount : 1600

Unit : 200

RESULT :
Thus the program was executed successfully.

You might also like