Object Oriented Programming With C++ LAB FILE
Object Oriented Programming With C++ LAB FILE
Object Oriented Programming With C++ LAB FILE
ROLL NO. 27
CODE:
//Header files
#include<bits/stdc++.h>
using namespace std;
// Declares a user defined data type test
class test
{
int code;
static int count;
//Access specifier
public:
//setcode function declaration for count
void setcode(void)
{
code = ++count;
}
//showcode function declaration to show object code
void showcode(void)
{
cout<<"object number:"<<code<<"\n";
}
//showcount function declaration to show count
static void showcount(void)
{
cout<<"count:"<<count<<"\n";
}
};
int test::count;
int main()
{ //declare object t1,t2 for class test
test t1, t2;
//calling function setcode to increase the count
t1.setcode();
t2.setcode();
//calling function showcount
test::showcount();
test t3;
//calling function setcode to increase the count
t3.setcode();
OUTPUT:
CODE:
//Header files
#include<bits/stdc++.h>
using namespace std;
//declaring const m for number of items
const int m=50;
class ITEMS
{ //declaring variable itemCode,itemPrice and count
int itemCode[m];
float itemPrice[m];
int count;
// Access specifier
public:
//declaring/define CNT function for count
void CNT(void) {
count=0;
}
//declaring/define all function for different operations
void getitem(void);
void displaySum(void);
void remove(void);
void displayItems(void);
};
//Function to enter items in the shopping list
void ITEMS::getitem(void)
{
int a;
cout<<"Enter Item Code:"<<endl;
cin>>a;
for (int i=0;i<count;i++)
if (itemCode[i] == a)
itemPrice[i]=0;
}
//Function to display items
void ITEMS::displayItems(void)
{
cout<<"\nCode Price\n";
for (int i=0;i<count;i++)
{
cout<<"\n"<<itemCode[i];
cout<<" "<<itemPrice[i];
}
cout<<"\n";
}
int main()
{ // Declares a user defined data type ITEMS
ITEMS order;
order.CNT();
int x;
do
{
cout<<"\nYou can do the following:"<<" "
<<"Enter appropriate number\n";
cout<<"\n1 : Add an Item";
cout<<"\n2 : Display Total Value";
cout<<"\n3 : Delete an Item";
cout<<"\n4 : Display all items";
CODE:
//Header Files
#include <bits/stdc++.h>
using namespace std;
// Declares a user defined data type employee
class employee
{ //Declare name and age variable
char name[30];
float age;
public:
//Define function getdata and putdata for operations
void getdata(void);
void putdata(void);
};
//Function to get values from user
void employee::getdata(void)
{
cout<<" Enter Name"<<" ";
cin>>name;
cout<<" Enter Age"<<" ";
cin>>age;
}
//Function to show entered value
void employee::putdata(void)
{
cout<<" Name:"<<" "<<name<<"\n";
cout<<" Age: "<<" "<<age<<"\n";
}
//Assign a const size to take value of only three manager
const int size=3;
int main()
{ //Define array of object size for class employee
employee manager[size];
for (int i=0; i<size; i++)
{
cout<<"\n Details of manager"<<i+1<<"\n";
//calling function getdata to get value from user
manager[i].getdata();
}
OUTPUT:
CODE:
//Header File
#include <bits/stdc++.h>
using namespace std;
//Declares a user defined data type ABC
class ABC;
//Declares a user defined data type XYZ
class XYZ
{
int x;
public:
//Function to take value and set it to variable
void setvalue(int i)
{
x=i;
}
friend void max(XYZ, ABC);
};
class ABC
{
int a;
public:
//Function to take value and set it to variable
void setvalue(int i)
{
a=i;
}
friend void max(XYZ, ABC);
};
//Define function max
void max(XYZ m, ABC n)
{
if (m.x>=n.a)
cout<<m.x;
else
cout<<n.a;
}
int main()
OUTPUT:
CODE:
//Header File mainly for standard input output
#include <iostream>
#include <conio.h>
using namespace std;
//Define class_2 to use in class_1
class class_2;
//Declaration of class_1
class class_1
{ //value1 variable defined in private member
int value1;
//access specifier
public:
//indata function declaration to take value_1 from user
void indata(int a)
{
value1 = a;
}
//display function declaration to show inputted value_1 to user
void display(void)
{
cout << value1 << "\n";
}
//Friend function declaration to use private members variable of class
class_1 C1;
//object C2 declaration of class_2
class_2 C2;
//input value to class_1
C1.indata(100);
//input value to class_2
C2.indata(200);
cout << "Values before exchange"
<< "\n";
//function call of class_1 to display value before swapping
C1.display();
//function call of class_2 to display value before swapping
C2.display();
//exchange function call to swap values
exchange(C1, C2);
cout << "Values after exchange"
<< "\n";
//function call of class_1 to display value after swapping
C1.display();
//function call of class_2 to display value after swapping
C2.display();
return 0;
OUTPUT:
CODE:
//Header File mainly for standard input output
#include <iostream>
#include <conio.h>
using namespace std;
//Complex class definition
class complex
{ //variable x,y declaration under private member
float x;
float y;
//Access Specifier
public:
//Input function definition to take real and imaginary value of complex number from user
void input(float real, float img)
{
x = real;
y = img;
}
//Friend function declaration to use x, y private member variable
friend complex sum(complex, complex);
//Show function declaration with complex data type
void show(complex);
};
//sum function definition to do the sum
complex sum(complex c1, complex c2)
{ //variable C3 declaration with complex data type to store sum of two complex number
complex c3;
//Sum of real value of two complex number
c3.x = c1.x + c2.x;
//Sum of imaginary value of two complex number
c3.y = c1.y + c2.y;
//returning value c3
return (c3);
}
//show function definition to show complex value in following format
void complex ::show(complex c)
OUTPUT:
CODE:
//Header file for standard input and output and necessary files
#include <iostream>
#include <conio.h>
using namespace std;
//Code Class definition
class code
{ //id variable defined in default private class
int id;
//Access specifier
public:
//default constructor code declaration
code() {}
//Paramaterised constructor code definition with int 'a' as an argument
code(int a)
{
id = a;
}
//constructor code definition with code '&x' as an argument
code(code &x)
{
id = x.id;
}
//function display to show the value of id
void display(void)
{
cout << id;
}
};
int main()
{ //Constructor call
code A(100);
code B(A)
code C = A;
code D;
//initialise D with A
D = A;
//Displaying value of ids of all four objects
cout << "\n id of A:";
A.display();
cout << "\n id of B:";
B.display();
cout << "\n id of C:";
C.display();
OUTPUT:
CODE:
//Header file for standard input and output and necessary files
#include <iostream>
#include <conio.h>
//Header file to include all string properties
#include <string.h>
using namespace std;
//Defining Class String
class String
{
//Variable defined in dafault private member
char *name;
int length;
//access specifier
public:
//String constructor definition with no argument
String()
{
length = 0;
name = new char[length + 1];
}
//String constructor definition with char pointer as argument
String(char *s)
{
//strlen "stl" to calculate length of string
length = strlen(s);
name = new char[length + 1];
//strcpy "stl" to copy s value to name
strcpy(name, s);
}
//display function to show value of name char pointer
void display(void)
{
cout << name << "\n";
}
//join function declaration
void join(String &a, String &b);
};
//definition of function join using constructor string
void String ::join(String &a, String &b)
{
length = a.length + b.length;
delete name;
OUTPUT:
CODE:
//Header file to include standard or user-defined files
#include <iostream>
//namespace to use same name variable in different namespace and scopes
using namespace std;
//Matrix class definition
class matrix
{ //default private member
//pointer of pointer p
int **p;
//d1 and d2 variable
int d1, d2;
//access specifier
public:
//declaration of paramatarized constructor with two int type argument x and y
matrix(int x, int y);
//get_element function definition to take values from user
void get_element(int i, int j, int value)
{
p[i][j] = value;
}
//put_element function definition to show desired output
int put_element(int i, int j)
{
return p[i][j];
}
};
//definition of matrix constructor
matrix ::matrix(int x, int y)
{ //assign value of x in d1
d1 = x;
//assign value of y in d2
d2 = y;
p = new int *[d1];
for (int i = 0; i < d1; i++)
p[i] = new int[d2];
}
int main()
OUTPUT:
CODE:
//Header file to include standard or user-defined files
#include <iostream>
//namespace to use same name variable in different namespace and scopes
using namespace std;
//complex class definition
class complex
{
//private access specifier
private:
//definition of float variable real and imag
float real, imag;
//public access specifier
public:
//default complex constructor
complex()
{
}
//Parameterized Constructors with argument of float type r and i
complex(float r, float i)
{ //assign value of r in real
real = r;
//assign value of i in imag
imag = i;
}
//getdata function definition to get real and imaginary values from user
void getdata()
{
float r, i;
CODE:
//Header files
#include <iostream>
#include <conio.h>
using namespace std;
//coonst variable size
const int size = 3;
//Vector class definition
class vector
{ //integer array v defined in default private mode
int v[size];
//access specifier
public:
//default constructor
vector();
//parameterized constructor with pointer argument
vector(int *x);
//friend function to overload operators '*' '<<' '>>'
friend vector operator*(int a, vector b);
friend vector operator*(vector b, int a);
friend istream &operator>>(istream &, vector &);
friend ostream &operator<<(ostream &, vector &);
};
//constructor definition to initialise values of vectors to zero
vector ::vector()
{
for (int i = 0; i < size; i++)
v[i] = 0;
}
//constructor definition to initialise values of vectors to pointer y
vector ::vector(int *y)
{
for (int i = 0; i < size; i++)
v[i] = y[i];
}
//operator overloading definition for '*' operator
vector operator*(int a, vector b)
{
vector c;
for (int i = 0; i < size; i++)
c.v[i] = a * b.v[i];
return c;
}
//operator overloading definition for '*' operator
CODE:
#include <iostream>
#include <iomanip> //It defines the manipulator functions
using namespace std;
//Matrix class definition
class matrix
{ //variable maxcol,maxrow defined in default private mode
int maxrow, maxcol;
//pointer variable defined in default private mode
int *ptr;
//access specifier
public:
//paramaterized constructor definition
matrix(int r, int c)
{ //value of r assigned to maxrow
maxrow = r;
//value of r assigned to maxcol
maxcol = c;
ptr = new int[r * c];
}
//getmat function declaration to get value in matrix
void getmat(void);
//printmat function declaration to print value of matrix
void printmat();
//delmat function declaration to find determinant value of matrix
int delmat();
//'+' operator overloading function declaration
matrix operator+(matrix b);
{
cout << endl;
for (j = 0; j < maxcol; j++)
{ //assigning consecutive integer values to mat_off variable
mat_off = i * maxcol + j;
//printing output in a matrix form using setw manipulator
int i, j, mat_off;
for (i = 0; i < maxrow; i++)
{
for (j = 0; j < maxcol; j++)
{ //assigning consecutive integer values to mat_off variable
mat_off = i * maxcol + j;
//adding values of two matrix and assign it to third matrix c
c.ptr[mat_off] = ptr[mat_off] + b.ptr[mat_off];
}
}
return (c);
}
//'*' operator overloading definition
matrix matrix ::operator*(matrix b)
{ //constructor call
matrix c(b.maxcol, maxrow);
int i, j, k, mat_off1, mat_off2, mat_off3;
for (i = 0; i < c.maxrow; i++)
{
for (j = 0; j < c.maxcol; j++)
{ //assigning consecutive integer values to mat_off variable
mat_off3 = i * c.maxcol + j;
int main()
{ //decalaring vriable to take value of row and column of a and b
int rowa, cola, rowb, colb;
cout << endl
<< "Enter dimensions of matrix A ";
cin >> rowa >> cola;
//constructor call with object a
matrix a(rowa, cola);
//getmat function call to assign inputted value using object a
a.getmat();
cout << endl
<< "Enter dimensions of matrix B";
cin >> rowb >> colb;
//constructor call with object b
matrix b(rowb, colb);
//getmat function call to assign inputted value using object b
b.getmat();
//constructor call
matrix c(rowa, cola);
//adding matrices using '+' overloaded operator
c = a + b;
cout << endl
<< "The sum of two matrices = ";
//calling printmat function to print matrix c(constitutes addition of two matrix)
c.printmat();
//constructor call
matrix d(rowa, colb);
//multipling matrix using '*' overloaded operator
d = a * b;
cout << endl
OUTPUT:
OUTPUT:
CODE:
#include <iostream>
using namespace std;
//CIRCLE class definition
class circle
{
//private access specifier
private:
//declaring variables
int radius;
float x, y;
//public access specifier
public:
//default constructor declaration
circle()
{
}
//paramaterized constructor declaration
circle(int rr, float xx, float yy)
{ //assigning value of rr to radius
radius = rr;
//assigning value of xx to x
x = xx;
//assigning value of yy to y
y = yy;
cout << endl
<< "simple constuctor called";
}
//'=' Assignment operator overloading definition
y = c.y;
}
//showdata function definition to print output
void showdata()
{
cout << endl
<< "Radius = " << radius;
cout << endl
<< "X-Coordinate=" << x;
cout << endl
<< "Y-Coordinate=" << y;
}
};
int main()
{
OUTPUT:
CODE:
//Header file for standard input and output
#include <iostream>
using namespace std;
//base alpha class definition
class alpha
{ //variable x defined in default private mode
int x;
//public access specifier
public:
//paramaterized constructor
alpha(int i)
{
x = i;
cout << "alpha initialized\n";
}
//show_x function definition
void show_x(void)
{
cout << "x=" << x << "\n";
}
};
//base beta class definition
class beta
{
//variable y defined in private default mode
float y;
//public access specifier
public:
//parameterized constructor
beta(float j)
{
y = j;
cout << "beta initialized\n";
}
// //show_y function definition
CODE:
#include <iostream>
//Header file to include string
#include <string.h>
using namespace std;
//person class definiton
class person
{
char name[20];
float age;
//public access specifier
public:
person(const char *s, float a) //const before char*-in C the type is array of char and in C++
it is constant array of char
{
strcpy(name, s);
age = a;
}
//greater function definition
person &greater(person &x)
{
if (x.age >= age)
return x;
else
return *this;
}
//display function definition
void display(void)
{
cout << "Name:" << name << "\n"
<< "Age: " << age << "\n";
}
};
int main()
{ //constructor call
person p1("John", 37.50), p2("Ahmed", 29.0), p3("Hebber", 40.5);
person p = p1.greater(p3);
OUTPUT:
CODE:
//Header file for standard input output
#include <iostream>
using namespace std;
//Base class BC definition
class BC
{
//access specifier
public:
//variale b declaration in public mode
int b;
//show function definition
void show()
{
cout << "b=" << b << "\n";
}
};
//Derived class DC definition
class DC : public BC
{
//access specifier
public:
//variable d declaration in public mode
int d;
//show function definition
void show()
{
cout << "b=" << b << "\n"
<< "d=" << d << "\n";
}
};
int main()
{
BC *bptr; //base class pointer
BC base; //base class object
//case 1:Using base class pointer to assign base class object
bptr = &base; // base object’s address assigned to base pointer
//case4: //Using base class pointer to assign derived class object with the help of
typecasting
cout << "Using ((DC *)bptr)\n";
((DC *)bptr)->d = 400; //using base pointer to call derived member variable
((DC *)bptr)->show(); // using base pointer to call derived member function
return 0;
}
OUTPUT:
CODE:
//Header file for standard input and output
#include <iostream>
using namespace std;
//Base class definition
class Base
{
//access specifier
public:
//Display function definition
void display()
{
cout << "\n Display Base";
}
//show function definition derived as virtual
virtual void show() // base class member function derived as virtual
{
cout << "\n Show Base:";
}
};
//Derived class definition derived publicly from base class
class Derived : public Base
{
//access specifier
public:
//display function definition
void display()
{
cout << "\n Display Derived";
}
//show function definition
void show()
{
cout << "\n Show Derived";
}
};
int main()
{ //base class object declaration
OUTPUT:
CODE:
#include <bits/stdc++.h>
using namespace std;
class media
{
protected:
char title[50];
float price;
public:
media(char *s, float a)
{
strcpy(title, s);
price = a;
}
virtual void display() {}
};
class book : public media
{
int pages;
public:
book(char *s, float a, int p) : media(s, a)
{
pages = p;
}
void display();
};
class tape : public media
{
float time;
public:
tape(char *s, float a, float t) : media(s, a)
media *list[2];
list[0] = &book1;
list[1] = &tape1;
cout << "\n";
cout << "Media Details"
<< "\n"
<< "\n";
cout << "<-Book->"
<< "\n"
<< "\n";
list[0]->display();
cout << "<-Tape->"
<< "\n"
<< "\n";
list[1]->display();
return 0;
}
CODE:
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
//String Class definition
class String
{
//private access specifier
private:
char str[20];
//public access specifier
public:
//Default Constructor definition
String()
{
str[0] = '\0';
}
//Parameterized Constructor's definition
//copy character array into string
String(const char *s)
{
strcpy(str, s);
}
//Parameterized Constructor's definition
//converts integer to string
String(int a)
{
itoa(a, str, 10);
}
//converts string into integer
operator int() //conversion operator,Conversion operator doesn’t have any return type
not even void.
{
int i = 0, l, ss = 0, k = 1;
l = strlen(str) - 1;
while (l >= 0)
{
ss = ss + (str[l] - 48) * k;
CODE:
//Header file for standard input and output
#include <iostream>
//Header file for "itoa" function
#include <stdlib.h>
//Header file for strings functions
#include <string.h>
using namespace std;
//date class definition
//to store str into dt array
class date
{
//private access specifier
private:
//dt array to store date in "/" format
char dt[9];
//public access specifier
public:
//default constructor
date()
{
dt[0] = '\0';
}
//parameterized constructor
date(char *s)
{
strcpy(dt, s);
}
//displaydata function to print output
void displaydata()
{
cout << dt;
}
};
//dmy class definition
//to convert separate date,month,year into "/" format
class dmy
{
//private access specifier
{
day = d;
mth = m;
yr = y;
};
//casting contructor operator functionto change data type
operator date()
{
char temp[3], str[9];
itoa(day, str, 10);
//concatinating “/” in char array
strcat(str, "/");
itoa(mth, temp, 10);
//concatinating temp value in char array
strcat(str, temp);
//concatinating “/” in char array
strcat(str, "/");
itoa(yr, temp, 10);
//concatinating temp value in char array
strcat(str, temp);
return (date(str));
}
//displaydata function to print output
void displaydata()
{
cout << day << "\t" << mth << "\t" << yr;
}
};
OUTPUT:
CODE:
//Header file for standard input and output
#include <iostream>
//Header file for string functions
#include <string.h>
//Header file for itoa function
#include <stdlib.h>
using namespace std;
//dmy class function(source class)
class dmy
{
//variable to store date,month and year respectively in default private mode
int day, mth, yr;
public:
//default constructor
dmy()
{
day = mth, yr = 0;
}
//parameterized constructor
dmy(int d, int m, int y)
{
day = d;
mth = m;
yr = y;
}
//getday function definition to return day
int getday()
{
return (day);
}
//getmth function definition to return month
int getmth()
{
return (mth);
}
//getyr function definition to return year
return 0;
}
CODE:
//Header file for standard input and output
#include <iostream>
using namespace std;
int main()
{ //varibale declaration
int i = 52;
float a = 425.0;
float b = 123.500328;
char str[] = "Dream. Then make it happend!";
cout.setf(ios::unitbuf); //When the unitbuf flag is set, the associated buffer is flushed after
each insertion operation.
//cout.setf( ios::stdio );
cout.setf(ios::showpos); //When the showpos format flag is set, a plus sign (+)
precedes every non - negative numerical value inserted into the stream(including zeros)
cout << i << endl; //output-+52
cout.setf(ios::showbase); //When the showbase format flag is set, numerical integer
values inserted into output streams are prefixed with the same prefixes used by C++ literal
constants :
cout.setf(ios::uppercase); //uppercase (capital) letters are used instead of lowercase
cout.setf(ios::hex, ios::basefield); //0x for hexadecimal values
cout << i << endl; //output-0X34
cout.setf(ios::oct, ios::basefield); //0 for octal values and no prefix for decimal-base values
cout << i << endl; //output-064
cout.setf(ios::dec, ios::basefield); // no prefix for decimal-base values
cout << i << endl; //output-+52
cout.fill('0'); // sets 0 as the new fill character and returns the fill
character used before the call. The fill character is the character used by output insertion
functions to fill spaces when padding results to the field width
cout << "Fill character " << cout.fill() << endl; // output - Fill Character 0 //cout.fill( ) here
returns the fill character.
CODE:
OUTPUT:
CODE:
CODE:
OUTPUT:
CODE:
char choice;
group g;
do
{
//clrscr();
system("cls");
{ //opening file
file.open("file.txt", ios::out | ios::binary | ios::in); // first out then binary then in, other
wise file wont open
//checking availability of file
if (!file)
{
cout << endl
<< "Unable to open file";
exit();
}
}
//add record function definition
void group::addrec()
{
char ch;
//moving pointer to end to add new record at the end
file.seekp(0L, ios::end);
do
{
cout << endl
<< "Enter emp code, name, age & salary" << endl;
cin >> p.empcode >> p.name >> p.age >> p.sal;
p.flag = ' ';
//inputting/writing data in the file
file.write((char *)&p, sizeof(p));
cout << "Add another record? (Y/N)";
cin >> ch;
} while (ch == 'Y' || ch == 'Y');
}
//list record function definition
void group::listrec()
{
int j = 0, a;
//brings the pointer to beginning
file.seekg(0L, ios::beg);
//reading data to its size
while (file.read((char *)&p, sizeof(p)))
{
getch();
}
}
//modify record function definition
void group::modirec()
{
char code[5];
int count = 0;
long int pos;
cout << "Enter employee code: ";
cin >> code;
file.seekg(0L, ios::beg);
while (file.read((char *)&p, sizeof(p)))
{ //if entered code is equal to present data then modify
if (strcmp(p.empcode, code) == 0)
{
cout << endl
<< "Enter new record" << endl;
cin >> p.empcode >> p.name >> p.age;
p.flag = ' ';
pos = count * sizeof(p);
file.seekp(pos, ios::beg);
file.write((char *)&p, sizeof(p));
return;
}
count++;
}
cout << endl
<< "No employee in file with code = " << code;
cout << endl
<< "Press any key .....";
CODE:
OUTPUT:
CODE:
s.push(14);
s.push(15);
s.push(16);
s.push(17);
s.push(18);
s.push(19);
s.push(20);
//this 21 item will not get pushed as the limit 10 of stack has reached
s.push(21);
//storing top value of stack in i variable
int i = s.pop();
//printing popped item
cout << endl
<< "Item popped=" << i;
i = s.pop();
cout << endl
<< "Item popped=" << i;
i = s.pop();
cout << endl
<< "Item popped=" << i;
OUTPUT:
CODE:
#include <iostream>
//Defining 10 to MAX
#define MAX 10
class queue
private:
int arr[MAX];
int front,
rear;
public:
//default constructor
queue()
front = -1;
rear = -1;
if (rear == MAX - 1)
return;
rear++;
arr[rear] = item;
if (front == -1)
front = 0;
int delq()
int data;
if (front == -1)
return 0;
data = arr[front];
else
front++;
return data;
};
int main()
{ //object creation
queue a;
a.addq(11);
a.addq(12);
a.addq(13);
a.addq(14);
a.addq(15);
a.addq(16);
a.addq(17);
a.addq(18);
a.addq(19);
a.addq(20);
a.addq(21);
//deleting element
int i = a.delq();
i = a.delq();
//deleting element
i = a.delq();
return 0;
OUTPUT:
CODE:
#include <iostream>
//defining MAX to be 10
#define MAX 10
class queue
private:
int arr[MAX];
public:
//default constructor
queue()
front = -1;
rear = -1;
return;
if (rear == MAX - 1)
rear = 0;
else
if (front == -1)
front = 0;
int delq()
int data;
if (front == -1)
return 0;
else
data = arr[front];
if (front == rear)
rear = -1;
else
if (front == MAX - 1)
front = 0;
else
front = front + 1;
return data;
};
int main()
queue a;
a.addq(11);
a.addq(12);
a.addq(13);
a.addq(14);
a.addq(15);
a.addq(16);
a.addq(17);
a.addq(18);
a.addq(19);
a.addq(20);
//deleting elements
int i = a.delq();
//deleting elements
i = a.delq();
//deleting elements
i = a.delq();
return 0;
OUTPUT:
t = new node;
t->data = num;
t->link = NULL;
q->link = t;
}
}
OUTPUT:
public:
stack() // Constructor initializes top to NULL
{
top = NULL;
}
void push(int item) // push function take one integer argument
{
node *temp; // Pointer declare of node type name temp
temp = new node; // To allocate memory in heap
if (temp == NULL) // It happens when Ram is full
cout << endl
<< "Stack is full";
temp->data = item; // To assign value of item in data part of temp
public:
//default constructor
queue()
{
front = rear = NULL;
}
//addq function definition to add element
void addq(int item)
{
i = a.delq();
cout << endl
<< "Item extracted=" << i;
return 0;
}
OUTPUT:
//test function definition with restriction of any other argument type instead of int and dou
ble
void test(int x) throw(int, double)
{
//if input x=0 then throw character x
if (x == 0)
throw 'x';
//else if input x==1 throw integer x
else if (x == 1)
throw x;
//else if input x==-1 throw double 1.0
else if (x == -1)
throw 1.0;
cout << "End of Function Block\n";
}
int main()
{
//try block starts
try
{
cout << "Testing Throw Restrictions\n";
cout << "x == 0\n";
//calling test for value 0