0% found this document useful (0 votes)
26 views

c++ & data structure practical file (bridge course)

Uploaded by

zssdzwqpd9
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)
26 views

c++ & data structure practical file (bridge course)

Uploaded by

zssdzwqpd9
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/ 18

INDEX

S.NO PROGRAMS P.NO REMARKS


1 Write a Program to design a class having static 2
member function named showcount() which
has the property of displaying the number of
objects created of the class.
2 Write a Program to swap private data members 3-4
of classes named as class_1, class_2 using
friend function.
3 Write a Program using copy constructor to 5-6
copy data of an object to another object.
4 Write a C++ program to create multilevel 7-8
inheritance
5 Write a C++ program to use pointer for both 9
base and derived classes and call the member
function, Use virtual keyword.
6 Write a program to design a class representing 10-11
complex numbers and having the functionality
of performing addition & multiplication of two
complex numbers using operator overloading.
7 WAP to find implement the Linear search 12
algorithm in C++.
8 WAP to implement Bubble Sort algorithm in 13
C++.
9 WAP to implement Stack Operations in C++. 14-15
10 WAP to implement Queue Operations in C++. 16-18
Experiment No-1
Write a Program to design a class having static member function named
showcount() which has the property of displaying the number of objects created of
the class.

#include<iostream.h>
#include<conio.h>
class test
{
int code;
static int count;
public:
void setcode(void)
{
code = ++count;
}
void showcode(void)
{
cout<<"object number:"<<code<<"\n";
}
static void showcount(void)
{
cout<<"count:"<<count<<"\n";
}
};
int test :: count;int main()
{
clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test :: showcount();
test t3;
t3.setcode();
test :: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
return 0;
}

OUTPUT
Experiment No-2
Write a Program to swap private data members of classes named as class_1,
class_2 using friend function.
#include<iostream.h>
#include<conio.h>
class class_2;
class class_1
{
int value1;
public:
void indata(int a)
{
value1=a;
}
void display(void)
{
cout<<value1<<"\n";
}
friend void exchange(class_1 &, class_2 &);
};
class class_2
{
int value2; public:
void indata(int a)
{
value2=a;
}
void display(void)
{
cout<<value2<<"\n";
}
friend void exchange(class_1 &, class_2 &);
};
void exchange(class_1 &x, class_2 &y)
{
int temp = x.value1; x.value1 = y.value2; y.value2 = temp;
}
int main()
{
clrscr();
class_1 C1;
class_2 C2;

C1.indata(100);
C2.indata(200);
cout<<"Values before exchange"<<"\n";

C1.display();
C2.display(); exchange(C1, C2);
cout<<"Values after exchange"<<"\n"; C1.display();
C2.display();
getch();
return 0;
}

OUTPUT
Experiment No - 3
Write a Program using copy constructor to copy data of an object to another
object.
#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code(){}
code(int a)
{
id = a;
}
code(code & x)
{
id = x.id;
}
void display(void)
{
cout<<id;
}
};
int main()
{

clrscr();
code A(100);
code B(A);
code C = A;
code D;
D = A;
cout<<"\n id of A:";
A.display();
cout<<"\n id of B:";
B.display();
cout<<"\n id of C:";
C.display();
cout<<"\n id of D:";
D.display();
getch();
return 0;
}
OUTPUT
Experiment No – 4
Write a C++ program to create multilevel inheritance.
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
class person
{
char name[100],gender[10];
int age;
public:
void getdata()
{
cout<<"Name: ";
fflush(stdin); /*clears input stream*/
gets(name);
cout<<"Age: ";
cin>>age;
cout<<"Gender: ";
cin>>gender;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
}
};
class employee: public person
{
char company[100];
float salary;
public:
void getdata()
{
person::getdata();
cout<<"Name of Company: ";
fflush(stdin);
gets(company);
cout<<"Salary: Rs.";
cin>>salary;
}
void display()
{
person::display();
cout<<"Name of Company: "<<company<<endl;
cout<<"Salary: Rs."<<salary<<endl;
}
};
class programmer: public employee
{
int number;
public:
void getdata()
{
employee::getdata();
cout<<"Number of programming language known: ";
cin>>number;
}
void display()
{
employee::display();
cout<<"Number of programming language known: "<<number;
}
};
int main()
{
clrscr();
programmer p;
cout<<"Enter data"<<endl;
p.getdata();
cout<<endl<<"Displaying data"<<endl;
p.display();
getch();
return 0;
}

OUTPUT
Experiment No – 5
Write a C++ program to use pointer for both base and derived classes and call the
member function, Use virtual keyword.
#include<iostream.h>
#include<conio.h>
class Base
{
public:
virtual void show()
{
cout<<"Base class\n";
}
};
class Derived:public Base
{
public:
void show()
{
cout<<"Derived Class";
}
};
int main()
{
clrscr();
Base* b,ob; //Base class pointer
b=&ob;
b->show();
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Ocuurs
getch();
return 0;
}

OUTPUT
Experiment No – 6
Write a program to design a class representing complex numbers and having the
functionality of performing addition & multiplication of two complex numbers
using operator overloading.
#include<iostream.h>
#include<conio.h>
class complex
{
private:
float real,imag,r,i;
public:
complex( )
{}
complex( float r, float i )
{
real = r; imag = i;
}
void getdata( )
{
cout << endl << "Enter real and imaginary part ";
cin >> r >> i;
real = r; imag = i;
}
void setdata(float r,float i )
{
real = r; imag = i;
}
void displaydata( )
{
cout << endl << "real = " << real;
cout<<endl<<"Imaginary = "<<imag;
}
complex operator +( complex c )
{
complex t;
t.real = real + c.real;
t.imag = imag + c.imag;
return t;
}
complex operator *( complex c )
{
complex t;
t.real = real * c.real;
t.imag = real * c.imag;
return t;
}
};
void main( )
{
clrscr();
complex c1,c2 ( 1.2, 2.5 ),c3,c4;
c1.setdata( 2.0, 2.0);
cout<<"\n Adding Complex numbers\n";
c3 = c1 + c2;
c3.displaydata( );
cout<<"\nMultiply Complex numbers\n";
c4=c1*c2;
c4.displaydata( );
getch();
}

OUTPUT
Experiment No – 7
WAP to find implement the Linear search algorithm in C++.
#include<iostream.h>
#include<conio.h>
int linearSearch(int arr[], int n, int target)
{
for (int i = 0; i < n; ++i)
{
if (arr[i] == target)
return i; // Target found, return the index
}
return -1; // Target not found, return -1
}
void main()
{

int arr[100],size,i;
int target;
clrscr();
cout<<"\nEnter the array size : ";
cin>>size;
cout<<"\nEnter array elements \n";
for(i=0;i<size;i++)
cin>>arr[i];
cout << "Enter the number to search: ";
cin >> target;
int result = linearSearch(arr, size, target);
if (result != -1)
cout << "Element found at index " << result+1 <<endl;
else
cout << "Element not found in the array." <<endl;
getch();
}

OUTPUT
Experiment No – 8
WAP to implement Bubble Sort algorithm in C++.
#include <iostream.h>
#include<conio.h>
void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < n - i - 1; ++j)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void main()
{
int arr[100],size,i;
clrscr();
cout<<"\nEnter the array size : ";
cin>>size;
cout<<"\nEnter array elements \n";
for(i=0;i<size;i++)
cin>>arr[i];
cout << "\nOriginal array: ";
for (i = 0; i < size; ++i)
cout << arr[i] << " ";

bubbleSort(arr, size);
cout << "\nSorted array: ";
for (i = 0; i < size; ++i)
cout << arr[i] << " ";
getch();
}
OUTPUT
Experiment No – 9
WAP to implement Stack Operations in C++.
#include<iostream.h>
#include<conio.h>
int stack[2], n=2, top=-1;
void push(int val)
{
if(top==n-1)
cout<<"Stack Overflow"<<endl;
else
{
top++; // top = top+1
stack[top]=val;
}
}
void pop()
{
if(top==-1)
cout<<"Stack Underflow"<<endl;
else
{
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display()
{
int i; // local
if(top>=0) // stack is not empty
{
cout<<"Stack elements are:";
for(i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
}
else
cout<<"Stack is empty";
}
void main()
{
int ch, val; // local declaration
clrscr();
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do
{
cout<<"Enter choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter value to be pushed: " ;
cin>>val;
push(val);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
cout<<"Exit"<<endl;
break;
default:
cout<<"Invalid Choice"<<endl;
break;
}
}while(ch!=4);
getch();
}
OUTPUT
Experiment No – 10
WAP to implement Queue Operations in C++.
#include<stdio.h>
#include<iostream.h>
#include<process.h>
#include<conio.h>
#define n 3
int f=-1,r=-1,q[n],item;
int ins();
int dlt();
void display();
void main()
{
char ch,temp;
clrscr();
do
{
cout<<"\nselect operation to perform in queue: \n";
cout<<"1.insrt 2.dlt 3.display\n";
fflush(stdin);
cin>>ch;
switch(ch)
{
case '1':
ins();
break;
case '2':
dlt();
break;
case '3':
display();
break;
}
cout<<"\nwanna perform more operations?\ntype'Y'or'N': ";
fflush(stdin);
cin>>temp;
}while(temp == 'y'||temp == 'Y');
cout<<"type 'S'to display queue else type 'n'/'N' to terminate the pgrm : ";
fflush(stdin);
cin>>temp;
if(temp == 's'||temp == 'S')
display();
else
{
getch();
exit(0);
}
getch();
}
int ins()
{
if(r>=n-1)
{
cout<<"\nqueue ovf!!\n";
return 0;
}
cout<<"enter element to insert:";
cin>>item;
if(f==-1&&r==-1)
f=r=0;
else
r=r+1;
q[r]=item;
return 0;
}
int dlt()
{
if(f==-1&&r==-1)
{
cout<<"\nqueue undrfl!!";
return 0;
}
item = q[f];
cout<<"deleted element : "<<item;
if(f==r)
f=r=-1;
else
f=f+1;
return 0;
}
void display()
{
int i;
if(f==-1&&r==-1)
cout<<"\nQueue is empty";
else
{
cout<<"QUEUE:\n";
for(i=f;i<=r;i++)
cout<<q[i]<<" ";
}
}
OUTPUT

You might also like