0% found this document useful (0 votes)
39 views55 pages

Nikhil Pachauri

This document is a practical file submitted by Nikhil Pachauri to Mr. Vikas Chandra for the course Data Structure and C++. It contains 12 topics that will be demonstrated through programs including the use of new and delete operators, constructors, destructors, inheritance, and data structures like stacks and binary trees.

Uploaded by

Mithan Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views55 pages

Nikhil Pachauri

This document is a practical file submitted by Nikhil Pachauri to Mr. Vikas Chandra for the course Data Structure and C++. It contains 12 topics that will be demonstrated through programs including the use of new and delete operators, constructors, destructors, inheritance, and data structures like stacks and binary trees.

Uploaded by

Mithan Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Delhi Hwy, Near Naveen SabjiMandi, Sikandra, Agra, Uttar Pradesh, 282007

PRACTICAL FILE
FOR
Data Structure, C++
BCA
First Year (Session: 2021- 2022)

Submitted by: Submitted To:


Nikhil Pachauri Mr. Vikas Chandra

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.

New and Delete operator


#include <iostream>

using namespace std;

int main() {

// declare an int pointer

int* pointInt;

// declare a float pointer

float* pointFloat;

// dynamically allocate memory

pointInt = new int;

pointFloat = new float;

// assigning value to the memory

*pointInt = 45;

*pointFloat = 45.45f;

cout << *pointInt << endl;

cout << *pointFloat << endl;


cout <<"Nikhil Pachauri"<<endl;

// deallocate the memory

delete pointInt;

delete pointFloat;

return 0;

output

TO create a class and access its members using objects


#include <bits/stdc++.h>

using namespace std;

class Name

// Access specifier

public:

// Data Members

string Name;

// Member Functions()

void printName()

cout << " My Name is: " << Name;

};

int main() {

Name obj1;

obj1.Name = "Nikhil Pachauri";


// accessing member function

obj1.printName();

return 0;

Output

Use of Constructors and destructors


#include <iostream>

using namespace std;

class Demo{

private:

int num1,num2;

public:

Demo(int n1, int n2) {

cout<<"Inside Conductor"<<endl;

num1 = n1;

num2 = n2;

void display(){

cout<<"num1 ="<<num1 <<endl;

cout<<"num2 ="<<num2 <<endl;

cout<<"Nikhil Pachauri"<<endl;

Demo(){

cout<<"Inside Destructor";

}
};

int main(){

Demo obj1(10, 20);

obj1.display();

return 0;

Output

Copy Constructors.
#include <iostream>

using namespace std;

class Wall {

private:

double length;

double height;

public:

Wall(double len, double hgt) {

length = len;

height = hgt;

Wall(Wall &obj) {

length = obj.length;

height = obj.height;

double calculateArea() {

return length * height;

}
};

int main()

Wall wall1(10.5, 8.6);

Wall wall2 = wall1;

cout<<"Area of Wall 1: " << wall1.calculateArea() <<endl;

cout<<"Area of Wall 2: " << wall2.calculateArea() <<endl;

cout<<"Nikhil Pachauri" <<endl;

return 0;

Output
Constructor overloading
#include <iostream>

using namespace std;


class Room {

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() {

return length * breadth;

};

int main() {

Room room1, room2(8.2, 6.6), room3(8.2);

cout<<"When no argument is passed: " <<endl;

cout<<"Area of room: " <<room1.calculateArea() <<endl;

cout<<"\nWhen (8.2, 6.6) is passed: " <<endl;

cout<<"Area of room: " <<room2.calculateArea() <<endl;

cout<<"\nWhen breadth is fixed to 7.2 and 8.2 is passed: "<<endl;

cout<<"Area of room: " <<room3.calculateArea() <<endl;

cout<<"\nMy Name is Nikhil Pachauri" <<endl;


return 0;

Output

Access specifiers in C++


There are three type of specifier in C++
 Public Specifier
 Private Specifier
 Protected Specifier

 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>

using namespace std;


class Sample{

private:

int age;

public:

void displayAge(int a) {

age = a;

cout<<"Age = " <<age <<endl;

};

int main() {

int ageInput;

Sample obj1;

cout<<"Enter your age: ";

cin>> ageInput;

cout<<"Nikhil Pachauri";
obj1.displayAge(ageInput);

return 0;

Output

 Protected Specifier
#include <iostream>

using namespace std;

class Sample{

protected:
int age;

};

class samplechild : public Sample {

public:

void displayAge(int a) {

age =a;

cout<< " Age = " << age <<endl;

};

int main() {

int ageInput;

samplechild child;

cout<<"Enter your age: ";

cin>> ageInput;

cout<<"Nikhil Pachauri ";


child.displayAge(ageInput);

return 0;

Output

Various type of Inheritance


There are various type of inheritance:
 Single Inheritance
#include <iostream>

using namespace std;


class Name{

public:

Name()

cout<<"My
Name is Nikhil Pachauri\n";

};

class student : public Name{

};

int main()

student obj;

return 0;

Output
 Multilevel Inheritance
#include <iostream>

using namespace std;

class sum

protected:

int n1;
};

class child1 : public sum

protected:

int n2;

};

class child2 : public child1

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>

using namespace std;

class A

public:

A()

{
cout<<"HIS NAME IS NIKHIL"<<endl;

};

class B

public:

B()

cout<<"HIS SURNAME IS PACHAURI"<<endl;

};

class C:public A ,public B

public:

C()

cout<<"HIS FULL NAME IS NIKHIL PACHAURI"<<endl;

};
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;

cout<<"\n a from ClassC :"<<obj.ClassC::a;


cout<<"\n b :"<<obj.b;
cout<<"\n c :"<<obj.c;
cout<<"\n d :"<<obj.d<<'\n';
}

Output

 Hierarchical Inheritance
#include<iostream>
using namespace std;

class Men{

public:

void info(){

cout<<"I AM STUDENT."<<endl;

};

class Name : public Men{

public:

void nm(){

cout<<"MY NAME IS NIKHIL PACHAURI."<<endl;

};

class Rollno : public Men{

public:

void rn(){

cout<<"MY ROOLLNO IS 63"<<endl;

};

int main(){
Name name1;

cout<<"Name Class:"<<endl;

name1.info();

name1.nm();

Rollno rollno1;

cout<<"\n Rollno Class:"<<endl;

rollno1.rn();

return 0;

Output

 Hybrid Inheritance
// C++ program for Hybrid Inheritance
#include <iostream>

using namespace std;

// base class

class Vehicle {

public:

Vehicle() { cout << "This is a Vehicle\n"; }

};

// base class

class Fare {

public:

Fare() { cout << "Fare of Vehicle\n";

cout<<"\nNikhil Pachauri\n"<<endl;

};

// first sub class

class Car : public Vehicle {


};

// second sub class

class Bus : public Vehicle, public Fare {

};

// main function

int main()

// Creating object of sub class will

// invoke the constructor of base class.

Bus obj2;

return 0;

Output
Friend function and Friend class
 Friend function
#include <iostream>
using namespace std;

class Distance {
private:
int meter;

friend int addFive(Distance);

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>

using namespace std;

// forward declaration

class ClassB;

class ClassA {

private:

int numA;

// friend class declaration

friend class ClassB;

public:

// constructor to initialize numA to 12

ClassA() : numA(12) {}

};

class ClassB {

private:
int numB;

public:

// constructor to initialize numB to 1

ClassB() : numB(1) {}

// member function to add numA

// from ClassA and numB from ClassB

int add() {

ClassA objectA;

return objectA.numA + numB;

};

int main() {

ClassB objectB;

cout << "Sum: " << objectB.add();

cout<<"\nNikhil Pachauri\n";

return 0;

}
Output

Operator overloading
#include <iostream>
using namespace std;

class Count {

private:

int value;

public:

Count() : value(5) {}

// Overload ++ when used as prefix

void operator ++ () {

++value;

// Overload ++ when used as postfix

void operator ++ (int) {

value++;

}
void display() {

cout << "\nCount: " << value << endl;

cout<<"\nNikhil Pachauri";

};

int main() {

Count count1;

// Call the "void operator ++ (int)" function

count1++;

count1.display();

// Call the "void operator ++ ()" function

++count1;

count1.display();

return 0

}
Output

Function Overload
#include <iostream>
using namespace std;

// function with 2 parameters

void display(int var1, double var2) {

cout << "Integer number: " << var1;

cout << " and double number: " << var2 << endl;

// function with double type single parameter

void display(double var) {

cout << "\nDouble number: " << var << endl;

cout << "Nikhil Pachauri\n";

// function with int type single parameter

void display(int var) {

cout << "Integer number: " << var << endl;

}
int main() {

int a = 5;

double b = 5.5;

// call function with int type parameter

display(a);

// call function with double type parameter

display(b);

// call function with 2 parameters

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;

printf("\n Enter the size of STACK[MAX=100]:");

scanf("%d",&n);

printf("\n\t STACK OPERATIONS USING ARRAY");

printf("\n\t--------------------------------");

printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do

printf("\n Enter the Choice:");

scanf("%d",&choice);

switch(choice)

{
case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("\n\t EXIT POINT ");

break;

}
default:

printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");

while(choice!=4);

return 0;

void push()

if(top>=n-1)

printf("\n\tSTACK is over flow");

else

printf(" Enter a value to be pushed:");


scanf("%d",&x);

top++;

stack[top]=x;

void pop()

if(top<=-1)

printf("\n\t Stack is under flow");

else

printf("\n\t The popped elements is %d",stack[top]);

top--;

void display()

if(top>=0)
{

printf("\n The elements in STACK \n");

for(i=top; i>=0; i--)

printf("\n%d",stack[i]);

printf("\n Press Next Choice");

else

printf("\n The STACK is empty");

printf("\n Nikhil Pachauri");

Output
Create a binary search tree
Insert a node in binary search tree

You might also like