C++ Programs
C++ Programs
C++ Programs
#include <iostream.h>
class Rectangle
{
public:
float length, breadth;
public:
Rectangle() {
cout << "\n\n****** Inside the Constructor ******* \n\n";
length = 8;
breadth = 6;
}
public:
~Rectangle()
{
cout << "\n\n****** Inside the Destructor ******* \n\n";
}
};
main()
{
cout << " Program to demonstrate the concept of Constructor and Destructor in CPP \n\n";
cout << "\nCalling the default Constructor of the Rectangle class to initialize the object.\n\n";
Rectangle rect;
cout << "\nThe Length of the Rectangle set by the Constructor is = " << rect.length << "\n\n";
cout << "\nThe Breadth of the Rectangle set by the Constructor is = " << rect.breadth << "\n\n";
}
Output
#include<iostream.h>
class SCC
{
private:
int x, y;
public:
SCC(int x1, int y1)
{
x = x1;
y = y1;
}
/* Copy constructor */
SCC (const SCC &sam)
{
x = sam.x;
y = sam.y;
}
void display()
{
cout<<x<<" "<<y<<endl;
}
};
main()
{
SCC obj1(100, 50); // Normal constructor
SCC obj2 = obj1; // Copy constructor
cout<<"Normal constructor : ";
obj1.display();
cout<<"Copy constructor : ";
obj2.display();
}
Output
Normal constructor : 100 50
Copy constructor : 100 50
3 Program to implement the Friend Class and Friend Function in C++
#include <iostream.h>
class ClassB;
class ClassA
{
public:
// constructor to initialize numA to 120
ClassA() : numA(120) {}
private:
int numA;
// friend function declaration
friend int add(ClassA, ClassB);
};
class ClassB
{
public:
// constructor to initialize numB to 10
ClassB() : numB(10) {}
private:
int numB;
// friend function declaration
friend int add(ClassA, ClassB);
};
main()
{
ClassA objectA;
ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
}
Output
Sum: 130
4 Program to implement the Multilevel Inheritance in C++
#include <iostream.h>
class base //single base class
{ public:
int x;
void getdata()
{
cout << "Enter value of x= "; cin >> x;
}
};
class derive1 : public base // derived class from base class
{ public:
int y;
void readdata()
{ cout << "\nEnter value of y= "; cin >> y;
}
};
class derive2 : public derive1 // derived from class derive1
{ private:
int z;
public:
void indata()
{ cout << "\nEnter value of z= "; cin >> z;
}
void product()
{ cout << "\nProduct= " << x * y * z;
}
};
main()
{
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
}
Output
Enter value of x= 5
Enter value of y= 6
Enter value of z= 3
Product= 90
5 Program to implement the Multiple Inheritance in C++
#include<iostream.h>
class A
{
public:
int x;
void getx()
{
cout << "Enter value of x: "; cin >> x;
}
};
class B
{
public:
int y;
void gety()
{
cout << "Enter value of y: "; cin >> y;
}
};
class C : public A, public B //C is derived from class A and class B
{
public:
void sum()
{
cout << "Sum = " << x + y;
}
};
main()
{
C obj1; //object of derived class C
obj1.getx();
obj1.gety();
obj1.sum();
}
Output
Enter value of x: 10
Enter value of y: 20
Sum = 30
6 Program to implement the Function Overloading in C++
#include <iostream.h>
main()
{
// Call function with 2 int parameters
cout << "Sum 1 = " << sum(15, 12) << endl;
Output
Sum 1 = 27
Sum 2 = 28.1
Sum 3 = 48
7 Program to implement the Function Overriding in C++
#include <iostream.h>
class Base
{
public:
void print()
{
cout << "Base Function" << endl;
}
};
main()
{
Base base1;
base1.print();
Output
Base Function
Derived Function
8 Program to implement the Virtual Function in C++
#include<iostream.h>
class base
{
public:
virtual void print()
{
cout << "print base class\n";
}
void show()
{
cout << "show base class\n";
}
};
void show()
{
cout << "show derived class\n";
}
};
main()
{
base *bptr;
derived d;
bptr = &d;
Output
print derived class
show base class
9 Program to implement the Function Overloading in C++
#include <iostream.h>
class Count
{
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
void display()
{
cout << "Count: " << value << endl;
}
};
main()
{
Count count1;
count1.display();
}
Output
Count: 4
10 Program to handle the Exception in C++
#include <iostream.h>
double div(int x, int y)
{
if (y == 0)
{
throw "Division by Zero!";
}
return (x / y);
}
main()
{
int a = 11;
int b = 0;
double c = 0;
try
{
c = div(a, b);
cout << c << endl;
}
catch (const char* message)
{
cerr << message << endl;
}
}
Output
Division by Zero!
11. Program to implement Stack operations using Array
#include <iostream.h>
void pop()
{
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else
{
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
main()
{
int ch, val;
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: "<<endl;
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
cout<<"Exit"<<endl;
break;
}
default:
{
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
}
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit
Enter choice:
1
Enter value to be pushed:
10
Enter choice:
1
Enter value to be pushed:
20
Enter choice:
1
Enter value to be pushed:
30
Enter choice:
1
Enter value to be pushed:
40
Enter choice:
3
Stack elements are:40 30 20 10
Enter choice:
2
The popped element is 40
Enter choice:
3
Stack elements are:30 20 10
Enter choice:
2
The popped element is 30
Enter choice:
3
Stack elements are:20 10
Enter choice:
4
Exit
12. Program to implement Queue operations using Array
#include <iostream.h>
int queue[100], n = 100, front = - 1, rear = - 1;
void Insert()
{
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else
{
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void Delete()
{
if (front == - 1 || front > rear)
{
cout<<"Queue Underflow ";
return ;
}
else
{
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void Display()
{
if (front == - 1)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
main()
{
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do
{
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
}
Output
#include<iostream.h>
#include<stack.h>
#include<locale.h> //for function isalnum()
while(stk.top() != '#')
{
postfix += stk.top(); //store and pop until stack is not empty.
stk.pop();
}
return postfix;
}
main()
{
string infix = "(a+b)*(c-d)/e";
cout << "Postfix Form Is: " << inToPost(infix) << endl;
}
Output
void polyadd(struct Node *p1, struct Node *p2, struct Node *result)
{
while(p1->next && p2->next)
{
if(p1->pow > p2->pow){
result->pow = p1->pow;
result->coeff = p1->coeff;
p1 = p1->next;
}
else if(p1->pow < p2->pow)
{
result->pow = p2->pow;
result->coeff = p2->coeff;
p2 = p2->next;
}
else
{
result->pow = p1->pow;
result->coeff = p1->coeff+p2->coeff;
p1 = p1->next;
p2 = p2->next;
}
result->next = (struct Node *)malloc(sizeof(struct Node));
result = result->next;
result->next = NULL;
}
while(p1->next || p2->next)
{
if(p1->next){
result->pow = p1->pow;
result->coeff = p1->coeff;
p1 = p1->next;
}
if(p2->next)
{
result->pow = p2->pow;
result->coeff = p2->coeff;
p2 = p2->next;
}
result->next = (struct Node *)malloc(sizeof(struct Node));
result = result->next;
result->next = NULL;
}
}
Output
struct Node
{
int data;
struct Node *left, *right;
Node(int data)
{
this->data = data;
left = right = NULL;
}
};
// Preorder traversal
void preorderTraversal(struct Node* node)
{
if (node == NULL)
return;
// Postorder traversal
void postorderTraversal(struct Node* node)
{
if (node == NULL)
return;
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->data << "->";
}
// Inorder traversal
void inorderTraversal(struct Node* node)
{
if (node == NULL)
return;
inorderTraversal(node->left);
cout << node->data << "->";
inorderTraversal(node->right);
}
main()
{
struct Node* root = new Node(1);
root->left = new Node(12);
root->right = new Node(9);
root->left->left = new Node(5);
root->left->right = new Node(6);
Output
#include <iostream.h>
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output
// If element is smaller than mid, then it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Driver code
main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? cout << "Element is not present in array" :
cout << "Element is present at index " << result;
}
Output
Element is present at index 3
18 Program to sort using Bubble sort
#include<iostream.h>
main ()
{
int i, j,temp,pass=0;
int a[10] = {10,2,0,14,43,25,18,1,5,45};
cout <<"Input list ...\n";
for(i = 0; i<10; i++)
{
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
pass++;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<10; i++)
{
cout <<a[i]<<"\t";
}
cout<<"\nNumber of passes taken to sort the list:"<<pass<<endl;
}
#include <iostream.h>
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r)
{
// m is the point where the array is divided into two subarrays
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Driver program
main()
{
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int shortest(int ,int);
int cost[10][10],dist[20],i,j,n,k,m,S[20],v,totcost,path[20],p;
main()
{
int c;
cout <<"enter no of vertices";
cin >> n;
cout <<"enter no of edges";
cin >>m;
cout <<"\nenter\nEDGE Cost\n";
for(k=1;k<=m;k++)
{
cin >> i >> j >>c;
cost[i][j]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=31999;
cout <<"enter initial vertex";
cin >>v;
cout << v<<"\n";
shortest(v,n);
}
INPUT
enter no of vertices 6
enter no of edges 11
enter
EDGE Cost
1 2 50
1 3 45
1 4 10
2 3 10
2 4 15
3 5 30
4 1 10
4 5 15
5 2 20
5 3 35
653
enter initial vertex 1
OUTPUT
1
14
145
1452
13