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

C++ and Data Structures Shortned Programs

The document contains code snippets demonstrating various object-oriented programming concepts in C++ including classes, constructors, destructors, inheritance, polymorphism, operator overloading, exception handling, stacks, queues, binary trees, file I/O, and infix to postfix conversion.

Uploaded by

Fasi
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)
36 views

C++ and Data Structures Shortned Programs

The document contains code snippets demonstrating various object-oriented programming concepts in C++ including classes, constructors, destructors, inheritance, polymorphism, operator overloading, exception handling, stacks, queues, binary trees, file I/O, and infix to postfix conversion.

Uploaded by

Fasi
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/ 20

//class

#include <iostream>

using namespace std;

class test
{
public:
void first()
{
cout << "From the CLASS to check if it works." << endl;
}
};
void main()
{
test t;
t.first();
}
//Array of objects
#include <iostream>

using namespace std;

class employee
{
private:
int id;
char name[30];
public:
void getdata();
void putdata();
};
void employee::getdata()
{
cout << "Enter ID : ";
cin >> id;
cout << "Enter Name : ";
cin >> name;
}
void employee::putdata()
{
cout << id << " ";
cout << name << " ";
cout << endl;
}
int main()
{
employee emp;
emp.getdata();
emp.putdata();
return 0;
}
//CONSTRUCTOR

#include <iostream>

using namespace std;

class testconstr
{
public:
testconstr()
{
cout << "Running from the test constructor" << endl;
}
};
void main()
{
testconstr c;
}
//DESTRUCTOR

#include<iostream>

using namespace std;

class test
{
public:
test()
{
cout << "constructor executed." << endl;
}
~test()
{
cout << "Distructor Executed." << endl;
}
};
void main()
{
test t;
}
//FUNCTION OVERLOADING

#include <iostream>

using namespace std;

void add(int a, int b)


{
cout << "SUM = " << (a + b);
}
void add(double a, double b)
{
cout << "SUM = " << (a + b);
}
int main()
{
add(5, 6);
cout << endl;
add(5.2, 6.5);
cout << endl;
return 0;
}
//OPERATOR OVERLOADING

#include<iostream>

using namespace std;

class temp
{
private:
int count;
public:
temp():count(5){}
void operator++()
{
count = count + 1;
}
void display()
{
cout << "count " << count<<endl;
}
};
int main()
{
temp t;
++t;
t.display();
return 0;
}
//single inheritance

#include<iostream>
#include<string>

using namespace std;

class organization
{
private:
string name;
string sector;
public:
organization()
{
name = "Junaid";
sector = "IT";
}
void showdata()
{
cout << "Name is :" << name<<endl;
cout << "Sector is :" << sector<<endl;
}
};
class employee:public organization
{
private:
string name;
int ID;
public:
employee()
{
name = "JUNAID";
ID = 3019;
}
void showname()
{
cout << "Name is :" << name << endl;
cout << "ID id :" << ID << endl;
}
};
int main()
{
employee obj;
obj.organization::showdata();
cout << endl;
obj.employee::showname();
return 0;
}
//MULTIPLE INHERITANCE

#include<iostream>

using namespace std;

class A
{
public:
A()
{
cout << "From class A" << endl;
}
};
class B
{
public:
B()
{
cout << "From class B" << endl;
}
};
class C:public B, public A
{
public:
C()
{
cout << "From class c" << endl;
}
};
void main()
{
C c;
}
//Hierarchical Inheritance

#include<iostream>

using namespace std;

class A
{
public:
void show_A()
{
cout << "Class A" << endl;
}
};
class B:public A
{
public:
void show_B()
{
cout << "Class B" << endl;
}
};
class C :public A
{
public:
void show_C()
{
cout << "Class C" << endl;
}
};
int main()
{
B b;
cout << "Calling from class B" << endl;
b.show_B();
b.show_A();
C c;
cout << "Calling from class C" << endl;
c.show_C();
c.show_A();
return 0;
}
//I/O STREAM

#include<iostream>

using namespace std;

int main()
{
char sample[] = "Welcome ";
cout << sample << "- To the I/O stream " << endl;
return 0;
}
//FILE STREAM

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
char data[100];
ofstream outfile;
outfile.open("afile.dat");
cout << "Writing to the file :" << endl;
cout << "Enter your Name ";
cin.getline(data, 100);
cout << "Enter your age :";
cin >> data;
cin.ignore();
outfile << data << endl;
outfile.close();
ifstream infile;
infile.open("afile.dat");
cout << "Reading from the file" << endl;
infile >> data;
cout << data << endl;
infile.close();
return 0;

}
//EXCEPTION HANDLING

#include<iostream>

using namespace std;

int main()
{
int x = -1;
cout << "Before Try" << endl;
try {
cout << "Inside try" << endl;
if (x < 0)
{
throw x;
cout << "After trow (Never executed)" << endl;

}
}
catch (int x) {
cout << "Execption caught" << endl;
}
cout << "After catch (Will be executed)" << endl;
return 0;
}
//STACK using array

#include<iostream>
#define max 5
using namespace std;

int top = 0;
class stack
{
int item[max];
public:
void push(int);
void pop();
void display();
};
void stack::push(int x)
{
if (top == max)
{
cout << "\n STACK IS FULL " << endl;
}
else
{
item[top] = x;
top = top + 1;
}
}
void stack::pop()
{
if (top == 0)
{
cout << "\n STACK IS EMPTY " << endl;
}
else
{
cout << "ELEMENT POPED " << endl;
cout << "\t" << item[--top] << endl;
}
}
void stack::display()
{
int i;
for(i=top-1;i>=0;--i)
cout << "\t" << item[i] << endl;
}
void main()
{
stack t;
int x, ch;
cout << "STACK USING ARRAY" << endl;
cout << "1.push" << endl;
cout << "2.pop" << endl;
cout << "3.delete" << endl;
cout << "4.exit" << endl;

do
{
cout << "Enter your choice :";
cin >> ch;
switch (ch)
{
case 1:
cout << "Enter the Value :";
cin >> x;
t.push(x);
break;
case 2:
t.pop();
break;
case 3:
cout << "STACK using Array" << endl;
t.display();
break;
case 4:
cout << "EXIT THE PROGRAM" << endl;
break;

default:
break;
}
}while (ch != 4);
}
//QUEUE USING ARRAY

#include<iostream>
#define max 3
using namespace std;

int top = 0, front = 0;

class que
{
int i, a[max], x;
public:
void insert();
void deleted();
void display();
};
void que::insert()
{
cout << "Enter the Element : ";
cin >> x;
if (top >= max)
{
cout << "QUEUE IS FULL" << endl;
}
else
{
a[top] = x;
top++;
}
}
void que::deleted()
{
if (top <= 0)
{
cout << "QUEUE IS EMPTY" << endl;
}
else
{
cout << a[front] << " DELETED " << endl;
for (int i = 1; i <= top; i++)
{
a[i - 1] = a[i];
top--;
}
}
}
void que::display()
{
for (int i = front; i < top; i++)
{
cout << " " << a[i] << endl;
}
}
void main()
{
int ch;
que b;
cout << "\t QUEUE USING ARRAY" << endl;
cout << "\n\t 1.insert";
cout << "\n\t 2.delete";
cout << "\n\t 3.display";
cout << "\n\t 4.exit";

do
{
cout << "\n Enter your choice : ";
cin >> ch;
switch (ch)
{
case 1:
b.insert();
break;
case 2:
b.deleted();
break;
case 3:
b.display();
break;
case 4:
cout << "EXIT THE PROGRAM" << endl;
break;
default:
break;
}
} while (ch != 4);
}
//INFIX TO POSTFIX

#include<iostream>
#include<string>
#include<ctype.h>
#include<process.h>
#include<math.h>

using namespace std;

class postfix
{
char lex[50], st[50], pex[50];
int top, p;
public:
postfix(char a[])
{
strcpy_s(lex, a);
top = -1;
p = 0;
}
void convert();
void push(char);
void pop();
};
void postfix::convert()
{
for (int i = 0; i < strlen(lex); i++)
{
if (isalpha(lex[i]))
{
pex[p] = lex[i];
p++;
}
else
if ((lex[i] == '(') || (lex[i] == '+') || (lex[i] == '-') ||
(lex[i] == '/') || (lex[i] == '*') || (lex[i] == '%') || (lex[i] == '^'))
{
push(lex[i]);
}
else if (lex[i] == ')')
{
pop();
}
else
{
cout << "INCORRECT expression \n";
exit(i);
}

}
pex[p] = '\0';
cout << "\n POSTFIX expression:" << pex << endl;

}
void postfix::push(char s)
{
st[++top] = s;
}
void postfix::pop()
{
pex[p] = st[top];
p++;
top = top - 2;
}
void main()
{
char str[30];
cout << "\n INFIX TO POSTFIX EXPRESSION";
cout << "\n Enter the Expression";
cin >> str;
postfix pf(str);
pf.convert();
}
//BINARY TREE

#include<iostream>

using namespace std;

struct node
{
int data;
struct node* left, * right;
};
node* newnode(int data)
{
node* temp = new node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void printinorder(struct node* node)
{
if (node == NULL)
return;
printinorder(node->left);
cout << node->data << " ";
printinorder(node->right);
}
int main()
{
struct node* root = newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->left->left = newnode(4);
root->left->right = newnode(5);
cout << "\n Inorder traversel of Binary Tree ";
printinorder(root);
cout << endl;
return 0;
}
//ADDITION OF TWO POLYNOMIALS USING POINTERS

#include<iostream>

using namespace std;

int max(int m, int n)


{
return(m > n) ? m : n;
}
int* add(int A[], int B[], int m, int n)
{
int size = max(m, n);
int* sum = new int[size];
for (int i = 0; i < m; i++)
sum[i] = A[i];
for (int i = 0; i < n; i++)
sum[i] += B[i];
return sum;
}
void printpoly(int poly[], int n)
{
for (int i = 0; i < n; i++)
{
cout << poly[i];
if (i != 0)
cout << "x^" << i;
if (i != n - 1)
cout << " + " << i;
}
}
int main()
{
int A[] = { 5,0,10,6 };
int B[] = { 1,2,4 };
int m = sizeof(A) / sizeof(A[0]);
int n = sizeof(B) / sizeof(B[0]);
cout << "First poly is \n";
printpoly(A, m);
cout << "\n Second poly is \n";
printpoly(B, n);
int* sum = add(A, B, m, n);
int size = max(m, n);
cout << "\n Sum of poly is \n";
printpoly(sum, size);
return 0;
}

You might also like