Академический Документы
Профессиональный Документы
Культура Документы
LAB MANUAL
Prepared by:
MS. K.SATHIYAPRIYA
AP / CSE
Ex. No: 1a DEFAULT ARGUMENTS
Aim:
Algorithm:
1. Declare the default function.
2. Invoke the default function.
3. Display the result
Program:
#include<iostream.h>
void printLine(char =’_’,int =70);
void main()
{
printLine();
printLine(‘/’);
printLine(‘*’,40);
printLine(‘R’,55);
}
void printLine(char ch, int Repeatcount)
{
int i;
cout<<endl;
for(i=0;i<Repeatcount;i++)
cout<<ch;
}
Output:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
///////////////////////////////////////
****************************
RRRRRRRRRRRRRRRRRRRRRRR
Result:
Aim:
Program:
#include<iostream.h>
class item
{
static int count;
int num;
public:
void getdata(int a)
{
num=a;
count++;
cout<<”Number”<<num;
}
void showcount()
{
cout<<”count”;
cout<<count<<”\n”;
}
};
int item::count;
int main()
{
item a,b,c;
a.showcount();
b.showcount();
c.showcount();
a.getdata(20);
b.getdata(30);
c.getdata(40);
a.showcount();
count 0
count 0
count 0
Number 20
Number 30
Number 40
count 3
count 3
count 3
Result:
Aim:
To implement static member function in class.
Algorithm:
1. Create class ITEM with static data member as count.
2. Create a member function to increment the count.
3. Declare the static data member using scope resolution operator.
4. Display the count value.
Program:
#include<iostream.h>
class test
{
int code;
static int count;
public:
void setcode()
{
cout<<”Object Number:”<<code<<”\n”;
}};
int test::count;
int main()
{
test t1,t2;
t1.setcount();
t2.setcount();
test::showcount();
test t3;
t1.showcode();
t2.showcode();
t3.showcode();
return(0);
}
Output:
count 2
count 3
Object Number 1
Object Number 2
Object Number 3
Aim:
To write a program for object as argument and returning an object using complex
number addition.
Algorithm:
1. The class complex contains two member variables real and imaginary.
2. Assign the value for real and imaginary part.
3. Declare a temporary variable temp.
4. Add real part with real of other object and store it in temp’s real.
5. Add imaginary part with imaginary of other object and store it in temp’s
imaginary.
6. Display temp.
Program:
#include<iostream.h>
template<class T>
class complex
{
private:
T real;
T imag;
Public:
complex()
{
real=imag=0;
}
void getdata()
{
cout<<”real part?”;
cin>>real;
cout<<”imag part?”;
cin>>imag;
}
complex operator +(complex c2);
void outdata(char *msg)
{
Template<class T>
complex<T> complex<T>::operator +(complex<T>c2)
{
Complex<T>temp;
temp.real=real+c2.real;
temp.img=imag+c2.imag;
return(temp);
}
void main()
{
complex<int>c1,c2,c3;
cout<<”Addition of integercomplexobjects….”<<endl;
cout<<”Enter complex number c1..”<<endl;
c1.getdata();
cout<<”Enterthecomplexnumberc2”<<endl;
c2.getdata();
c3=c1+c2;
c3.outdata(“c3=c1+c2:”);
complex<float>c4,c5,c6;
cout<<”Additionof float complexobjects….”<<endl;
cout<<”Enter complex number c4..”<<endl;
c4.getdata();
cout<<”Enterthecomplexnumberc5”<<endl;
c5.getdata();
c6=c4+c5;
c6.outdata(“c6=c4+c5:”);
}
Result:
Aim:
To write a c++ program for friend function.
Algorithm:
Program:
#include <iostream.h>
using namespace std;
class myclass {
int a, b;
public:
friend int sum(myclass x);
void set_ab(int i, int j);
};
void myclass::set_ab(int i, int j)
{
a = i;
b = j;
}
// Note: sum() is not a member function of any class.
int sum(myclass x)
{
/* Because sum() is a friend of myclass, it can
directly access a and b. */
return x.a + x.b;
}
int main()
{
myclass n;
n.set_ab(3, 4);
cout << sum(n);
return 0;
}
Output:
Aim:
To write a c++ program for employee wages calculation using class and objects.
Algorithm:
1. Employee class contains name and wage variable and member function a
putname(), putwage(),getwage() and getname().
2. Putname: Assign the valve for the character array name.
3. Getname: Retrieves the value of Variable name.
4. Putwage: Assign the valve for wage variable.
5. Getwage: Retrieves the value of wage variable.
6. Im main() Put and display both name and wages.
Program:
#include <iostream.h>
using namespace std;
class employee {
char name[80]; // private by default
public:
void putname(char *n); // these are public
void getname(char *n);
private:
double wage; // now, private again
public:
void putwage(double w); // back to public
double getwage();
};
void employee::putname(char *n)
{
strcpy(name, n);
}
void employee::getname(char *n)
{
strcpy(n, name);
}
void employee::putwage(double w)
{
Result:
Thus the Given Program for employee wages calculation using class and objects was
implemented successfully.
Aim:
To write a c++ program for demonstrating the copy constructor.
Algorithm:
1. Define the class
2. Declare the variables.
3. Define the function as constructor.
4. Define the main function and declare the variables.
5. Call the copy constructor.
Program:
#include<iostream.h>
class test
{
int x;
public:
//default constructor
test();
//parameterized constructor
test(int val)
{
x=val;
}
//copy constructor
test(test &obj)
{
x.obj.x;// entered is in value in obj.x
}
void show()
{
cout<<x;
}
};
void main()
{
int val;
cout<<”enter some number”<<endl;
cin>>val;
test old(val);
//call for copy constructor
Output:
enter value is 500
the original value is 500
the new copied value is 500
Result:
Thus the given program copy constructor was executed successfully.
Aim:
To Write a C++ program for implementing the inheritance.
Algorithm:
1. Define the base class with variables and functions.
2. Define the derived class with variables and functions.
3. Define main function
4. Declare the variables.
5. Inherits base class.
6. Access member of derived class.
Program:
#include<iostream.h>
class base
{
public:
int x;
void set_x(int n)
{x=n;
}
void show_x()
{
cout<<”\n\t Base class….”;
cout<<”\n\tx=”<<x;
}
};
class derived:public base
{
int y;
public:
void set_y(int n)
{
y=n;
}
void show_xy()
{
cout<<”\n\n\t derived class…”;
cout<<”\n\tx=”<<x;
cout<<”\n\ty=”<<y;
}
};
Output:
enter the value of x 10
enter the value of y 20
base class….
x=10
derived class…..
x=10
y=20
Result:
Thus the given Program inheritance was implemented successfully.
Aim:
To Write a C++ program for implementing the function overloading.
Algorithm:
1. Define class.
2. Define the functions with different arguments.
3. Define main function
4. Declare the variables.
5. Call the Different task of function.
6. Print the output.
Program:
#include<iostream.h>
class test
{
public:
int sum(int,int);
float sum(float,float);
double sum(double,double);
};
int test::sum(int a.int b)
{
return(a+b);
}
float test::sum(float a ,float b)
{
return(a+b);
}
double test::sum(double a,double b)
{
return(a+b);
}
void main()
{
test obj;
int choice;
int a,b;
float x,y;
double m,n;
double result=0;
cout<<”\n\t\t main menu”;
Output:
Main menu
1.Addition of two integer numbers
2.Addition of two float numbers
3.Addition of two double numbers
enter your choice 2
Program
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
class arr_list
{
/*data structure for list using array*/
private:
struct node
{
int data;
int next;
}a[10];
public:
int head;
arr_list();
int create();
void display(int);
void insert();
void delete();
void search();
};
arr_list::arr_list()
{
for(int i-0;i<10;i++)
{
a[i].data=-1;
}}
int arr_list::create()
{
int head,I;
cout<<”\n enter the index for first node”;
cin>>I;
head=i;
while(i!=-1)
{
cout<<”\n enter the data and index of the first element”;
Result:
/**************************************************************
Demonstration program to perform various operations on singly link list.
*************************************************************/
// List of include files
# include<iostream.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
//class definition
{
class sll
{
private:
struct node
{
int data;
struct node *next;
}*head;
public:
sll();
void create();
void display();
void search(int key);
void insert _head();
void insert_after();
void insert_last();
void dele();
~sll();
};
/*………………………………..
the constructor defined
………………………………….*/
sll::sll()
{
head=NULL;//initialize head to NULL
}
/*…………………………………
sll::~sll()
{
node *temp,*temp1;
temp=head->next;
delete head;
while(temp!=NULL)//freethe memory allocated
{
temp1=temp->next;
delete temp;
temp=temp1;
}
}
/*……………………………………..
the create function
………………………………………
*/
void sll::create()
{
node *temp,*new;
int val,flag;
char ans=’y’;
flag=TRUE;
do
{
cout<<”\n enter the data:”;
cin>>val;
??allocate memory to new node
new=new node;
if(new==NULL)
cout<<”unable to allocate memory\n”;
new->data=val;
new->next=NULL;
if(flag==true)//executed only for the first time
{
head=new;
temp=new;
flag=FALSE;
}
else
{
/*temp last keeps track of the most recently created node*/
temp->next=new;
temp=new;
/*
……………………………………………
the dele function
……………………………………………
*/
void sll::dele()
{
node *temp,*prev;
int key;
temp=head;
clrscr();
cout<<”\n enter the data of the node you want to delete:”;
cin>>key;
while(temp!=NULL)
{
if (temp->data==key)//traverse till required node to delete
break; //is found
prev=temp;
temp=temp->next;
}
if(temp==NULL)
cout<<”\n node not found”;
else
{
if(temp==head)//first node
head=temp->next;
else
prev->next=temp->next; //intermediate or end node
output
continue?
program to perform various operations on linked list
1.create
2. display
3.search
4.insert an element in a list
5.delete an element from list
6.quit
enter your choice(1-6) 4
continue?
program to perform various operations on linked list
1.create
2. display
3.search
4.insert an element in a list
5.delete an element from list
6.quit
enter your choice(1-6) 4
continue?
continue?
Result
Thus the given program Linked List implementation of the list ADT was executed
successfully.
Aim:
To write a C++ program for cursor implementation of list ADT.
Algorithm:
1. Start the program.
2. Create a node with two fields data and link field.
o Allocate space for the node dynamically.
o Create link between the created nodes and let the last node be with NULL
Link
o Insert the input data in the data field and press –1 to stop the same.
3. Get the choice of operations either insertion or deletion.
4. For insertion get the position in which insertion is to be done and the element to be
inserted. Check for the start, middle or end position of insertion. Insert the node and
change its link accordingly.
5. For deletion get the position in which deletion is to be done. Delete the node and
then link it to the next node. Before deletion check whether there is data in the list
to be deleted.
6. Using display option list the elements of the list.
7. Stop the program.
Program:
/* ***************************************
Program To implement cursor of List ADT
*****************************************/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
class LIST
{
private:
int list[MAX];
public:
int create();
void display(int);
void reverse(int);
int search(int);
void delet(int);
/*
*************************************
Function: create
Input: None
Output: creates the list
Calls: None
**************************************
*/
int LIST::create()
{int n,I;
clrscr();
cout<<”\n how many elements you want in the list:”;
cin>>n;
if(n>MAX)
cout<<”\n Error:Number of elements exceeds the limit”;
for(i=0;i<n;i++)
{
cout<<”\nEnter the element number”<<i+1<<”:”;
cin>>List[i];
}
cout<<”\n The List is successfully created\n”;
getch();
return(n);
}
/*
*************************************
Function: Display
Input: Length of the list
Output: prints the list
Calls: None
**************************************
*/
void LIST::display(int n)
{
int I;
clrscr();
cout<<”\n the list is…\n”;
for(i=0;i<n;i++)
cout<<”\n”<<List[i];
cout<<|n press any key to continue…\n”;
getch();
}
/*
Output
Result:
Thus the Given Program Cursor implementation of list was executed successfully.
Aim:
To write a program for stack using array implementation.
Algorithm:
Step 5. The stack represented by linked list is traversed to display its content.
Program:
/*
************************************************************************
Program for implementing a stack using arrays. it involves various operations such as
push ,pop, stack empty, stack full and display.
*************************************************************************
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
Output:
implementation of stack
main menu
1.push
2.pop
3.display
4.exit
main menu
1.push
2.pop
3.display
4.exit
main menu
1.push
2.pop
3.display
4.exit
Result:
Thus the given program stack ADT using array implemented successfully.
Aim:
To write a program for stack ADT using linked list implementation.
Algorithm:
Step1:Define a struct for each node in the stack. Each node in the stack
contains data and link to the next node. TOP pointer points to last
node inserted in the stack.
Step 5. The stack represented by linked list is traversed to display its content.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
// Declaration for data structure of linked stack
class lstack
{
private:
/*
……………………………………..
the sempty function
………………………………………
*/
int lstack::sempty(node *temp)
{
if(temp==NULL)
return 1;
else
return 0;
}
/*
………………………………………..
the pop function
…………………………………………*/
int lstack::pop(node **top)
{
int item;
node *temp;
item=(*top)->data;
temp=*top;
*top=(*top)->next;
delete temp;
return(item);
}
/*
………………………………….
the display function
………………………………….
*/
void lstack::display(node **head)
{
node *temp;
temp=*head;
if(sempty(temp))
cout<<”\n the stack is empty!”;
else
{
while(temp!=NULL)
{
cout<<””<<temp->data;
Result:
Thus the Given Program for stack ADT using linked list implementation was
executed successfully.
Step 1: Crate a header file named stack.h . in this frile we will declare the class and all the
stack operations. the implementation of stack is using arrays.
stack.h
#define size 10
class stk_class
{
private:
/*stack structure*/
struct stack{
char s[size];
int top;
}st;
public:
stk_class();
void push(char item);
int stempty();
char pop();
};
stk_class::stk_class()
{
st.top=-1;
}
/* the push function
input:item which is to be pished
output:none –simply pushes the item onto the stack
called by: main
calls: one
*/
void stk_class::push(char item)
{
st.top++;
st.s[st.top]=item;
EC 2209 Data Structures and Object Oriented Programming Lab
57
}
/*
the stempty function
input:none
output:returns 1 or 0 for stack empty or not
called by:none
*/
int stk_class::stempty()
{
inr(st.top==-1)
return 1;
else
return 0;
}
/*
the stfull function
input:none
coutput:returns 1 or 0 for stack full or not
called by :main
calls:none
*/
int stk_class::stfull()
{
if(st.top==size)
return 1;
else
return 0;
}
/*
the pop function
input:none
output:returns the item which is poped from the stack
called by: main
calls:none
*/
char stk_class::pop()
{
char item;
item=st.s[st.top];
st.top--;
return(item);
}
/************************************************************************
program for checking the well formedness of the parenthesis using stack as arrays.
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include “d:\stack.h”
#define size 10
/*
the main function
input:none
ouput:none
called by:O.S
calls:push,pop,stempty
*/
void main(void)
{
char item;
char ans,bracket[10];
stk_class obj;
int I;
clrscr();
cout<,”\n\t\t program for stack application using separate header file”;
cout<<”\n enter the expression and put $at end “;
cin>>bracket;
i=0;
if(bracket[i]==’)’)
cout<<”\n the expressin is invalid”;
else
{
do
{
while(brackt[i]==’(‘)
{
obj.push(bracket[i]);
i++;
}
while(bracket[i]==’)’)
Output(run 1)
program for stack application using separate header file
enter the expression and put $ at the end (()(()))$
the expression has well formed parenthesis
Output(run 2)
Result:
step 1:
create a header file named stack. In this file we declare the class and all the stack
operations. The implementation of stack using linked list.
/**********************************
the stack.h file
***********************************/
class stk_class
{
private:
/*data structure for the linked stack*/
typedef struct stack
{
char data;
struct stack * next;
}node;
node *top;
public:
stk_class();
void push(char item);
int sempty();
void pop();
};
stk_class ::stk_class()
{
top=NULL;
}
/* functionality performed on linked stack*/
void stk_class::push(char item)
{
node * New;
New=new node;
if(New==NULL)
cout<<”\n memory cannot be allocated \n”;
else
{
New->data=item;
new->next=top;
Step 2:
Now we will create a stack application program. We have chosen an application as
“checking well formedness of parenthesis” for this application we will use stack operations.
These operations are used from the external file stack.h. Hence we will include this file in
the include file section. Here is an application program.
/*…………………………………………………………………………………………
Program for checking the well – formedness of the parenthesis using linked stack.
……………………….…………………………………………………………………..*/
output(run1)
enter the expression and put $ at the end ()()$
the expression has well formed parenthesis
output(run 2)
enter the expression and put $ at the end (()()$
the expression is invalid.
Result:
Thus the given program checking well formedness of parenthesis stack implemented as
Linked List was executed successfully.
/********************************************
stack.h
*********************************************/
#define Max 10
class stk_class
{
/*declaration of stack data structure*/
struct stack
{
double s[MAX];
int top;
}st;
public:
stk_class();
void push(double val);
double pop();
};
stk_class::stk_class()
{
st.top=0;
}
void stk_class::push(double val)
{
if(st.top+1>=MAX)
cout<<”\n stack is full”;
st.top++;
st.s[st.top]=val;
}
double stk_class::pop()
{
double val;
STEP 2:
/************************************************************************
program to evaluate a given postfix expression .her the stack using arrays is implemented
in separate file named stack.h
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
/*included the header file as stack using array*/
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[]);
clrscr();
cout<<”enter the postfix expression\n”;
cin>>exp;
len=strlen(exp);
exp[len]=’$’;/*append $ at the end a end marker*/
result=post(exp);
cout<<”the value of the expression is”<<result;
getch();
exit(0);
}
Output:
Thus the given program Evaluation of postfix expression stack implemented as arrays was
executed successfully.
STEP 1:
/********************************************************************
the stack.h file
**********************************************************************/
class stk_class
{
/* data structure for the linked stack*/
typedef struct stack
{
char data;
struct stack *next;
}node;
public:
node *top;
stk_class();
void push(char item);
char pop();
};
stk_class::stk_class()
{
top=NULL:
}
/* functionality performed on linked linked stack*/
void stk_class::push(char item)
{
node *New;
New=new node;
New->data =Item;
New->next=top;
top=New;
}
char stk_class::pop()
{
char item;
node *temp;
item=top->data;
STEP 2:
/*********************************************************************
program to evaluate a given postfix expression using linked stack.
here stack.h is a user defined header file created for linked stack
**********************************************************************/
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
/* included the stack.h file (given below) for linked stack*/
#include”d:\stack.h”
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[])
clrscr();
cout<<”enter the postfix expression\n”;
cin>>exp;
len=strlen(exp);
cin>>exp;
len=strlen(exp);
exp[len]=’$’;/*append $at the end as a endmarker*/
result=post(exp);
cout<<”the value of the expression is”<<result;
getch();
exit(0);
}
Output:
Thus the given program Evaluation of postfix expression stack implemented as Linked List
was executed successfully.
Ex. No:7a QUEUE ADT USING ARRAY IMPLEMENTATION
Aim:
To write a program for Queue using array implementation.
Algorithm:
Step 5. The queue represented by linked list is traversed to display its content.
Program:
/******************************************************
Program for implementing the queue using arrays
*******************************************************/
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define size 5
class myq
Output:
main menu
1.insert
2.delete
3.display
Aim:
To write a C++ program for Queue using Linked implementation.
Algorithm:
Step1: Define a struct for each node in the queue. Each node in the queue
contains data and link to the next node. Front and rear pointer points to first
and last
node inserted in the queue.
Step 5. The queue represented by linked list is traversed to display its content.
Program:
/*
**********************************************
Program for implementing the queue using the linked list.
the queue full condition will never occur in this program.
**********************************************
/*
………………………………………
the Qempty function
………………………………………..
*/
int qempty(Q *front)
{
Output
Result:
/*
******************************************************************
Program for implementation of binary search tree and perform insertion
,deletion,searching,display of tree.
******************************************************************
*/
#include<iostream.h>
#include<conio.h>
class bintree
{
typedef struct bst
{
int data;
struct bst,*left,*right;
}
node;
node *root,*new,*temp,*parent;
public:
bintree()
{
root=NULL;
}
void create();
void display();
void delet();
void find();
void insert(node *,node*);
Output:
program for binary search tree
1.create
2.search
3.delete
4.display
1.create
2.search
3.delete
4.display
the tree is : 7 8 9 10 11 13
1.create
2.search
3.delete
4.display
the tree is : 7 8 9 10 13
1.create
2.search
3.delete
4.display
the tree is : 8 9 10 13
1.create
2.search
3.delete
4.display
Thus the Given Program Binary Search Tree was executed successfully.
Aim:
To write a c program to perform heap sort.
Algorithm:
1. Get the size of the array from the user.
2. Get the elements to be sorted.
3. Sorting is performed when we call the heap sort function.
4. Now the array is contained with sorted elements.
5. Display the sorted elements.
Program:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 10
class heap
{
private:
int arr[MAX];
int n;
public:
Heap();
void insert(int num);
void makespace();
void heapsort();
void display();
};
heap::heap()
{
n=0;
for(int i=0;i<MAX;i++)
arr[i]=0;
}
void heap::insert(int num)
{
if(n<Max)
{
arr[n]=num;
n++;
}
Aim:
To write a c program to perform quick sort.
Algorithm:
1. Get the value of how many no. to be sorted.
5. Quick sort algorithm works by partitioning the array to be sorted, then recursively
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 10
class quick
{
int arr[SIZE];
public:
int get_data(int);
void quicksort(int ,int);
int partition(int,int);
void swap)int ,int);
void display(int);
};
/*
this function is to input the elements
*/
int quick::get_data(int n)
Output
quick sort method
enter total numbers to sort: 5
enter element 30
enter element 50
enter element 10
enter element 20
enter element 40
sorted array is
10 20 30 40 50
Result: