C++ Lab Programs - With output-FINAL

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

/* Lab 1 : Program using class, object, member function, constructors and Destructors for calculating area and

perimeter of a circle */

#include <iostream.h>
#include <conio.h>

class circle
{
private:
float radius, area, perimeter;

public:
circle() { }

circle(float r)
{
radius = r;
area = 0;
perimeter = 0;
}

circle(float a, float p, float r=25)


{
radius = r;
area = a;
perimeter=p;
}

circle(circle & x)
{
radius = x.radius;
area = x.area;
perimeter= x.perimeter;
}

void read();

void display()
{
cout<<"\n\n Given radius is :";
cout<<radius;
cout<<"\n Area of the circle is :";
cout<<3.14 * radius * radius;
cout<<"\n Perimeter of the circle is :";
cout<<2 * 3.14 * radius << endl;
}

~circle() { }
};

void circle::read()
{
cout<<"\n Enter the radius of circle :";
cin>>radius;
}
int main()
{
clrscr();
cout<<"\n Program for calculating area and perimeter of a circle";
cout<<"\n-------------------------------------------------------";

float n;

circle C1;
C1.read();
C1.display();

cout<<"\n Enter radius value :";


cin>>n;

circle C2 = circle(n);
C2.display();

circle C3(0,0);
C3.display();

circle C4[10];
for(int i=0;i<2;i++)
{
C4[i].read();
C4[i].display();
}

circle C5 = C1;
circle C6(C2);

C5.display();
C6.display();
getch();
return 0;
}
Output :
/* Lab-2 : Program to implement function overloading*/

#include<iostream.h>
#include<conio.h>
int volume ( int& );
float volume (float );
double volume(double, double );
long volume( long, int, int );
inline float volume( const float &,float );
int main()
{
int a;
clrscr();
cout << "Function Overloading Program to find the volume of";
cout<<"4.Geometric Primitives \n \n";
cout << "-------------------------------------------------- \n";
cout << "Enter side value for cube : \n";
cin >> a;
cout << "Volume of a cone : " << volume(a) << "\n \n";
cout << "Volume of a circle : “ << volume(7)<<”\n \n";
cout << "Volume of cylinder : " << volume(2.5, 8.0) << "\n \n";
cout << "Volume of Rectangular box:"<<volume(100L,75,10)<<"\n \n";
cout << "Volume of a Sphere : " << volume(1.33, 5.0) << "\n \n";
getch();
return 0;
}
int volume(int & s)
{
return(s * s * s);
}
float volume (float r)
{
return(3.14 * r * r);
}
double volume (double r, double h)
{
return(3.14519 * r * r * h);
}
long volume (long l, int b, int h)
{
return (l * b * h);
}
inline float volume(const float &c, float r=10.0)
{
return (c * 3.14519 * r * r * r);
}
Output
/* Lab - 3 : Matrix addition program using operator overloading */

#include<iostream.h>
#include<conio.h>
class Matrix {
int rows, cols;
int **data;
public:
Matrix(int r, int c) {
rows = r;
cols = c;
data = new int*[rows];
for(int i = 0; i < rows; i++)
data[i] = new int[cols];
}
void input() {
cout << "Enter matrix elements:" << endl;
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
cin >> data[i][j];
}
void display() {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++)
cout << data[i][j] << " ";
cout << endl;
}
}
Matrix operator+(Matrix m) {
Matrix res(rows, cols);
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
res.data[i][j] = data[i][j] + m.data[i][j];
return res;
}
};
int main() {
clrscr();
int r, c;
cout << "Enter rows and columns of matrix:" << endl;
cin >> r >> c;
Matrix m1(r, c), m2(r, c);
cout << "Enter first matrix:" << endl;
m1.input();
cout << "Enter second matrix:" << endl;
m2.input();
Matrix res = m1 + m2;
cout << "Resultant matrix is:" << endl;
res.display();
getch();
return 0;
}
Output :
/* Lab 4(a): Example Program using Single Inheritance */

#include <iostream.h>
#include <conio.h>

class base
{ int x;
public:
void setx(int n)
{
x = n;
}
void showx ( )
{
cout<<"X = "<<x<<"\n";
}
};
class derived : public base
{ int y;
public:
void sety(int n)
{
y = n;
}
void showy ( )
{
cout<<"Y = "<<y<<"\n";
}
};
void main()
{
derived D;
D.setx(10);
D.sety(20);
clrscr();
cout<<"Single Inheritance program \n";
D.showx();
D.showy();
getch();
}

Output
/* Lab 4(b) : Program using Multiple Inheritance */

#include <iostream.h>
#include <conio.h>
class base1
{
int x;
public:
void setx(int n)
{
x = n;
}
void showx ()
{
cout<<"X = "<<x<<"\n";
}
};

class base2
{
int y;

public:
void sety(int n)
{
y = n;
}
void showy ()
{
cout<<"Y = "<<y<<"\n";
}
};

class derived : public base1, public base2


{
int z;
public:
void setz(int n)
{
z = n;
}
void showz ()
{
cout<<"Z = "<<z<<"\n";
}
};

void main()
{
derived D;
D.setx(10);
D.sety(20);
D.setz(30);
clrscr();
cout<<"Multiple Inheritance program \n";
D.showx();
D.showy();
D.showz();
getch();
}
Output
/* Lab 4(c) : Program using Hierarchical Inheritance */

#include <iostream.h>
#include <conio.h>

class A //single base class


{
public:
int x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n"; cin >> x >> y;
}
};
class B : public A //B is derived from class base
{
public:
void product()
{
cout << "\nProduct= " << x * y;
}
};
class C : public A //C is also derived from class base
{
public:
void sum()
{
cout << "\nSum= " << x + y;
}
};
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
clrscr():
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
getch();
}

OUTPUT:
/* Lab 5 : Program for create, read and write in files */

#include <iostream.h>
#include <fstream.h>
int main()
{
ofstream outFile; // output file stream object
ifstream inFile; // input file stream object
char fileName[50], text[100];
// Create a new file and write to it
cout << "Enter the name of the file to create: ";
cin.getline(fileName, 50);
outFile.open(fileName);
if (!outFile) {
cout << "Failed to create file." << endl;
return 1;
}
cout << "Enter text to write to the file: ";
cin.getline(text, 100);
outFile << text << endl;
outFile.close();
// Read from an existing file
cout << "Enter the name of the file to read: ";
cin.getline(fileName, 50);
inFile.open(fileName);
if (!inFile) {
cout << "Failed to open file." << endl;
return 1;
}
cout << "Contents of " << fileName << ":" << endl;
while (inFile.getline(text, 100)) {
cout << text << endl;
}
inFile.close();
return 0;

Output:
/* Lab 6 : Stack using Arrays */

#include <iostream.h>
#include <conio.h>
#define MAX 10
class stack
{
private:

int arr[ MAX ];


int top;

public:

stack()
{
top=-1;
}

void push(int a)
{
top++;

if(top<MAX)
{
arr[top]=a;
}

else
{
cout<<"STACK FULL!!"<<endl;
top--;
}
}

int pop()
{
if(top==-1)
{
cout<<"STACK IS EMPTY!!!"<<endl;
return NULL;
}

else
{
int data=arr[top];
arr[top]=NULL;
top--;
return data;
}
}
};
int main()
{
stack a;
clrscr();

a.push(3);
cout<<"3 is Pushed\n";

a.push(10);
cout<<"10 is Pushed\n";

a.push(1);
cout<<"1 is Pushed\n\n";

cout<<a.pop()<<" is Popped\n";
cout<<a.pop()<<" is Popped\n";
cout<<a.pop()<<" is Popped\n";

getch();
return 0;
}

Output
/* Lab 7 : Queue using arrays */

#include <iostream.h>
#include <conio.h>
#define MAX 5
class queue
{
private:
int t[MAX];
int al;
int dl;
public:
queue()
{
dl=-1;
al=-1;
}
void del()
{
int tmp;
if(dl==-1)
{
cout<<"Queue is Empty";
}
else
{

for(int j=0;j<=al;j++)
{
if((j+1)<=al)
{
tmp=t[j+1];
t[j]=tmp;
}
else
{
al--;

if(al==-1)
dl=-1;
else
dl=0;
}
}
}
}

void add(int item)


{
if(dl==-1 && al==-1)
{
dl++;
al++;
}
else
{
al++;
if(al==MAX)
{
cout<<"Queue is Full\n";
al--;
return;
}
}
t[al]=item;

void display()
{
if(dl!=-1)
{
for(int iter=0 ; iter<=al ; iter++)
cout<<t[iter]<<" ";
}
else
cout<<"EMPTY";
}

};

int main()
{
queue a;
int data[5]={32,23,45,99,24};
clrscr();
cout<<"Queue before adding Elements: ";
a.display();
cout<<endl<<endl;

for(int iter = 0 ; iter < 5 ; iter++)


{
a.add(data[iter]);
cout<<"Addition Number : "<<(iter+1)<<" : ";
a.display();
cout<<endl;
}
cout<<endl;
cout<<"Queue after adding Elements: ";
a.display();
cout<<endl<<endl;

for(iter=0 ; iter < 5 ; iter++)


{
a.del();
cout<<"Deletion Number : "<<(iter+1)<<" : ";
a.display();
cout<<endl;
}
getch();
return 0;
}

Output
/*Lab 8 : Infix and postfix conversion of ana expression of using stack */

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define MAX 50
char stack[MAX];
int top = -1;
void push(char ch)
{
if (top == MAX - 1)
cout << "Stack overflow!";
else
{
top++;
stack[top] = ch;
}
}
char pop()
{
if (top == -1)
cout << "Stack underflow!";
else
{
char ch = stack[top];
top--;
return ch;
}
}

int priority(char ch)


{
if (ch == '*' || ch == '/')
return 2;
else if (ch == '+' || ch == '-')
return 1;
else
return 0;
}
void infixToPostfix(char infix[], char postfix[])
{
int i, j;
char ch;
for (i = 0, j = 0; infix[i] != '\0'; i++)
{
if (isalnum(infix[i]))
postfix[j++] = infix[i];
else if (infix[i] == '(')
push(infix[i]);
else if (infix[i] == ')')
{
while ((ch = pop()) != '(')
postfix[j++] = ch;
}
else
{
while (priority(stack[top]) >= priority(infix[i]))
postfix[j++] = pop();
push(infix[i]);
}
}
while (top != -1)
postfix[j++] = pop();
postfix[j] = '\0';
}
int main()
{
clrscr();
char infix[MAX], postfix[MAX];
cout << "Enter the infix expression: ";
cin >> infix;
infixToPostfix(infix, postfix);
cout << "The postfix expression is: " << postfix;
getch();
return 0;
}
OUTPUT:
/* Lab 9 : Binary search tree traversals */

#include <iostream.h>
#include <conio.h>
#define YES 1
#define NO 0
class tree
{
private:
struct leaf
{ int data;
leaf *l;
leaf *r;
};
struct leaf *p;
public:
tree();
~tree();
void destruct(leaf *q);
tree(tree& a);
void findparent(int n,int &found,leaf* &parent);
void findfordel(int n,int &found,leaf *&parent,leaf* &x);
void add(int n);
void transverse();
void in(leaf *q);
void pre(leaf *q);
void post(leaf *q);
void del(int n);
};

tree::tree( )
{ p=NULL;
}

tree::~tree( )
{ destruct(p);
}

void tree::destruct(leaf *q)


{ if(q!=NULL)
{ destruct(q->l);
del(q->data);
destruct(q->r);
}
}
void tree::findparent(int n,int &found,leaf *&parent)
{
leaf *q;
found=NO;
parent=NULL;
if(p==NULL)
return;
q=p;
while(q!=NULL)
{ if(q->data==n)
{ found=YES;
return;
}

if(q->data>n)
{
parent=q;
q=q->l;
}
else
{
parent=q;
q=q->r;
}
}
}

void tree::add(int n)
{
int found;
leaf *t,*parent;
findparent(n,found,parent);
if(found==YES)
cout<<"\nSuch a Node Exists";
else
{
t=new leaf;
t->data=n;
t->l=NULL;
t->r=NULL;

if(parent==NULL)
p=t;
else
parent->data > n ? parent->l=t : parent->r=t;
}
}
void tree::transverse()
{
int c;
cout<<"\n1.InOrder\n2.Preorder\n3.Postorder\nChoice: ";
cin>>c;
switch(c)
{
case 1:
in(p);
break;

case 2:
pre(p);
break;

case 3:
post(p);
break;
}
}

void tree::in(leaf *q)


{
if(q!=NULL)
{
in(q->l);
cout<<"\t"<<q->data<<endl;
in(q->r);
}

void tree::pre(leaf *q)


{
if(q!=NULL)
{
cout<<"\t"<<q->data<<endl;
pre(q->l);
pre(q->r);
}

void tree::post(leaf *q)


{
if(q!=NULL)
{
post(q->l);
post(q->r);
cout<<"\t"<<q->data<<endl;
}

}
void tree::findfordel(int n,int &found,leaf *&parent,leaf *&x)
{
leaf *q;
found=0;
parent=NULL;
if(p==NULL)
return;

q=p;
while(q!=NULL)
{ if(q->data==n)
{ found=1;
x=q;
return;
}
if(q->data>n)
{ parent=q;
q=q->l;
}
else
{
parent=q;
q=q->r;
}
}
}
void tree::del(int num)
{
leaf *parent,*x,*xsucc;
int found;

// If EMPTY TREE
if(p==NULL)
{
cout<<"\nTree is Empty";
return;
}
parent=x=NULL;
findfordel(num,found,parent,x);
if(found==0)
{
cout<<"\nNode to be deleted NOT FOUND";
return;
}

// If the node to be deleted has 2 leaves


if(x->l != NULL && x->r != NULL)
{
parent=x;
xsucc=x->r;

while(xsucc->l != NULL)
{
parent=xsucc;
xsucc=xsucc->l;
}
x->data=xsucc->data;
x=xsucc;
}

// if the node to be deleted has no child


if(x->l == NULL && x->r == NULL)
{
if(parent->r == x)
parent->r=NULL;
else
parent->l=NULL;

delete x;
return;
}

// if node has only right leaf


if(x->l == NULL && x->r != NULL )
{
if(parent->l == x)
parent->l=x->r;
else
parent->r=x->r;

delete x;
return;
}

// if node to be deleted has only left child


if(x->l != NULL && x->r == NULL)
{
if(parent->l == x)
parent->l=x->l;
else
parent->r=x->l;

delete x;
return;
}
}

int main()
{
tree t;
clrscr();

int data[]={32,16,34,1,87,13,7,18,14,19,23,24,41,5,53};
for(int i=0;i<15;i++)
t.add(data[i]);

t.transverse();
t.del(16);
t.transverse();
t.del(41);
t.transverse();
getch();
return 0;
}

Output
/* Lab 10 : Polynomial Addition */

#include<iostream.h>
#include<conio.h>

class Poly {
int coef[100], exp[100], count;
public:
Poly() { count = 0; }
void readPoly();
void displayPoly();
Poly addPoly(Poly);
};

void Poly::readPoly() {
cout<<"Enter the number of terms: ";
cin>>count;
for(int i = 0; i < count; i++) {
cout<<"Enter coefficient and exponent of term "<<i+1<<": ";
cin>>coef[i]>>exp[i];
}
}

void Poly::displayPoly() {
for(int i = 0; i < count; i++) {
cout<<coef[i]<<"x^"<<exp[i];
if(i < count-1)
cout<<" + ";
}
cout<<endl;
}

Poly Poly::addPoly(Poly p) {
Poly result;
int i = 0, j = 0;
while(i < count && j < p.count) {
if(exp[i] > p.exp[j]) {
result.coef[result.count] = coef[i];
result.exp[result.count] = exp[i];
i++;
}
else if(exp[i] < p.exp[j]) {
result.coef[result.count] = p.coef[j];
result.exp[result.count] = p.exp[j];
j++;
}
else {
result.coef[result.count] = coef[i] + p.coef[j];
result.exp[result.count] = exp[i];
i++; j++;
}
result.count++;
}
while(i < count) {
result.coef[result.count] = coef[i];
result.exp[result.count] = exp[i];
i++; result.count++;
}
while(j < p.count) {
result.coef[result.count] = p.coef[j];
result.exp[result.count] = p.exp[j];
j++; result.count++;
}
return result;
}

int main() {
clrscr();
Poly p1, p2, p3;
cout<<"Enter the first polynomial:\n";
p1.readPoly();
cout<<"Enter the second polynomial:\n";
p2.readPoly();
p3 = p1.addPoly(p2);
cout<<"\nFirst polynomial is: ";
p1.displayPoly();
cout<<"Second polynomial is: ";
p2.displayPoly();
cout<<"Sum of the two polynomials is: ";
p3.displayPoly();
getch();
return 0;
}

Output:

You might also like