Professional Documents
Culture Documents
PRACTICAL FILE
FOR
Data Structure, C++
BCA
First Year (Session: 2021- 2022)
Sharma
Table of contents
WAP to demonstrate the use of new and delete
operator.
WAP to create a class and access its members using
objects.
WAP to demonstate the use of conductors and
destructors.
WAP to demonstate the use of coppy constructors.
WAP to demonstate the use of constructor
overloading in C++.
WAP to demonstate the use of various accesss
specifiers in C++.
WAP to demonstate the various type of Inheritance.
WAP to demonstate friend function and friend class
in C++.
WAP to demonstate the operators overloading in C+
+.
WAP to demonstate the concept of function
overloading in C++.
WAP to implement stack and its operations in C.
WAP to create a binary search tree.
WAP to insert a node in Binary Tree.
WAP to create a binary tree and implement
preorder, In-order and post order traversal in it.
WAP to convert inflix expression into postfix
expression.
WAP to demonstrate insertion sort technique in C.
WAP to implement the process of Merge sort
technique.
WAP to implement bubble sort technique.
WAP to implement binary search.
WAP to demonstrate single linked list creation and
its various insertion operation in C.
int main() {
int* pointInt;
float* pointFloat;
*pointInt = 45;
*pointFloat = 45.45f;
delete pointInt;
delete pointFloat;
return 0;
output
class Name
// Access specifier
public:
// Data Members
string Name;
// Member Functions()
void printName()
};
int main() {
Name obj1;
obj1.printName();
return 0;
Output
class Demo{
private:
int num1,num2;
public:
cout<<"Inside Conductor"<<endl;
num1 = n1;
num2 = n2;
void display(){
cout<<"Nikhil Pachauri"<<endl;
Demo(){
cout<<"Inside Destructor";
}
};
int main(){
obj1.display();
return 0;
Output
Copy Constructors.
#include <iostream>
class Wall {
private:
double length;
double height;
public:
length = len;
height = hgt;
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
double calculateArea() {
}
};
int main()
return 0;
Output
Constructor overloading
#include <iostream>
private:
double length;
double breadth;
public:
Room() {
length = 6.9;
breadth = 4.2;
Room(double l, double b) {
length = l;
breadth = b;
Room(double len) {
length = len;
breadth = 7.2;
}
double calculateArea() {
};
int main() {
Output
Public Specifier
#include <iostream>
using namespace std;
class Sample{
public:
int age;
void displayAge() {
cout<<" Age = "<<age <<endl;
}
};
int main(){
Sample obj1;
cout<<"Enter your age: ";
cin >> obj1.age;
cout<<"Nikhil Pachauri";
obj1.displayAge();
return 0;
}
Output
Private
#include <iostream>
private:
int age;
public:
void displayAge(int a) {
age = a;
};
int main() {
int ageInput;
Sample obj1;
cin>> ageInput;
cout<<"Nikhil Pachauri";
obj1.displayAge(ageInput);
return 0;
Output
Protected Specifier
#include <iostream>
class Sample{
protected:
int age;
};
public:
void displayAge(int a) {
age =a;
};
int main() {
int ageInput;
samplechild child;
cin>> ageInput;
return 0;
Output
public:
Name()
cout<<"My
Name is Nikhil Pachauri\n";
};
};
int main()
student obj;
return 0;
Output
Multilevel Inheritance
#include <iostream>
class sum
protected:
int n1;
};
protected:
int n2;
};
public:int sum()
cout<<"Nikhil Pachauri"<<endl;
cout<<"enter n1"<<endl;
cin>>n1;
cout<<"enter n2"<<endl;
cin>>n2;
cout<<"sum = "<<n1+n2<<endl;
};
int main()
{
child2 myobject;
myobject.sum();
Output
Multiple Inheritance
#include <iostream>
class A
public:
A()
{
cout<<"HIS NAME IS NIKHIL"<<endl;
};
class B
public:
B()
};
public:
C()
};
int main()
C c;
return 0;
Output
Multipath Inheritance
#include <iostream>
using namespace std;
class ClassA{
public:
int a;
};
class ClassB : public ClassA{
public:
int b;
};
class ClassC : public ClassA{
public:
int c;
};
class ClassD : public ClassB, public ClassC{
public:
int d;
};
int main()
{
ClassD obj;
//obj.a =10; //Statement1,Error
//obj.a =100; //statement2,Error
obj.ClassB::a =10;
obj.ClassC::a =100;
obj.b =20;
obj.c =30;
obj.d =40;
cout<<"Nikhil Pachauri"<<endl;
cout<<"a from ClassB :"<<obj.ClassB::a;
Output
Hierarchical Inheritance
#include<iostream>
using namespace std;
class Men{
public:
void info(){
cout<<"I AM STUDENT."<<endl;
};
public:
void nm(){
};
public:
void rn(){
};
int main(){
Name name1;
cout<<"Name Class:"<<endl;
name1.info();
name1.nm();
Rollno rollno1;
rollno1.rn();
return 0;
Output
Hybrid Inheritance
// C++ program for Hybrid Inheritance
#include <iostream>
// base class
class Vehicle {
public:
};
// base class
class Fare {
public:
cout<<"\nNikhil Pachauri\n"<<endl;
};
};
// main function
int main()
Bus obj2;
return 0;
Output
Friend function and Friend class
Friend function
#include <iostream>
using namespace std;
class Distance {
private:
int meter;
public:
Distance() : meter(0) {}
};
int addFive(Distance d) {
d.meter += 5;
return d.meter;
}
int main() {
Distance D;
cout<<"Distance: " <<addFive(D);
cout<<"\nNikhil Pachauri";
return 0;
}
Output
friend class
/
#include <iostream>
// forward declaration
class ClassB;
class ClassA {
private:
int numA;
public:
ClassA() : numA(12) {}
};
class ClassB {
private:
int numB;
public:
ClassB() : numB(1) {}
int add() {
ClassA objectA;
};
int main() {
ClassB objectB;
cout<<"\nNikhil Pachauri\n";
return 0;
}
Output
Operator overloading
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
Count() : value(5) {}
void operator ++ () {
++value;
value++;
}
void display() {
cout<<"\nNikhil Pachauri";
};
int main() {
Count count1;
count1++;
count1.display();
++count1;
count1.display();
return 0
}
Output
Function Overload
#include <iostream>
using namespace std;
cout << " and double number: " << var2 << endl;
}
int main() {
int a = 5;
double b = 5.5;
display(a);
display(b);
display(a, b);
return 0;
Output
Stack and its operation in C
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
//clrscr();
top=-1;
scanf("%d",&n);
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
}
default:
while(choice!=4);
return 0;
void push()
if(top>=n-1)
else
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
top--;
void display()
if(top>=0)
{
printf("\n%d",stack[i]);
else
Output
Create a binary search tree
Insert a node in binary search tree