Oop 6 Last
Oop 6 Last
Problem Statement: In the following input i is the index of array ax[]. The program prints
ax[i]. Then write catch block if i is out of range of ax[]. Write three catch blocks to fullfill
the purpose
#include <iostream>
int main()
int i;
int ax[5]={10,20,60,40,30};
cout<<"enter index:";
cin>>i;
cout<<"ax["<<i<<"]="<<ax[i]<<endl;
Code:
#include <iostream>
class ArrayHandler {
public:
void printElement(int i) {
try {
if (i < 0 || i >= 5) {
throw i;
cout << "ax[" << i << "] = " << ax[i] << endl;
cout << "Index " << index << " is out of range!" << endl;
catch (...) {
};
int main() {
int i;
ArrayHandler handler;
cin >> i;
handler.printElement(i);
return 0;
}
Topic 2 [class Template]:
Problem Statement: In the following class data members x and y are integers and the method
Sum() adds x and y. However, we need to perform the sum of int+int, int+double,
class A{
private:
int x;
int y;
public:
this->x=x;
this->y=y;
int Sum(){
int s;
s=x+y;
return s;
};
int main(){
Code:
#include <iostream>
class A {
private:
T x;
T y;
public:
void setData(T x, T y) {
this->x = x;
this->y = y;
T Sum() {
return x + y;
};
int main() {
A<int> intObj;
intObj.setData(5, 10);
cout << "Sum of int + int: " << intObj.Sum() << endl;
A<double> intDoubleObj;
intDoubleObj.setData(5, 10.5);
cout << "Sum of int + double: " << intDoubleObj.Sum() << endl;
A<double> doubleIntObj;
doubleIntObj.setData(10.5, 5);
cout << "Sum of double + int: " << doubleIntObj.Sum() << endl;
A<double> doubleDoubleObj;
doubleDoubleObj.setData(10.5, 20.5);
cout << "Sum of double + double: " << doubleDoubleObj.Sum() << endl;
return 0; }
Topic 3 [STL:Array class]
Problem statement: Declare a STL array object ax with 6 elements and do the following:
#include <iostream>
#include <bits/stdc++.h>
int main(){
array<int,6>ax;
//write statements
Code:
#include <iostream>
#include <array>
class ArrayHandler {
private:
public:
ArrayHandler() : ax{10, 60, 30, 70, 20, 0} {}
ax.fill(value);
cout << "Is the array empty? " << (ax.empty() ? "Yes" : "No") << endl;
cout << "Maximum size of array: " << ax.max_size() << endl;
cout << "Address of first element: " << &ax.begin()[0] << endl;
cout << "Address of last element: " << &ax.end()[-1] << endl;
};
int main() {
ArrayHandler handler;
handler.printThirdElement();
handler.printFirstElement();
handler.printLastElement();
handler.fillArray(5);
handler.checkIfEmpty();
handler.printSize();
handler.printMaxSize();
handler.printAddresses();
return 0;
}
Topic 4[STL: pair class]
Problem statement: Define a pair class object px with int and string elements. Write
#include <iostream>
#include <bits/stdc++.h>
int main(){
pair<int,string>px;
//write statements
Code:
#include <iostream>
#include <utility>
class PairHandler {
private:
public:
px = make_pair(num, str);
}
void printFirst() const {
px.first = newValue;}
swap(px, bx);
cout << "px: " << px.first << ", " << px.second << endl;
};
int main() {
PairHandler handler;
handler.setData(10, "Rajshahi");
handler.printFirst();
handler.printSecond();
handler.modifyFirst(20);
handler.swapPairs(bx);
handler.printPair();
cout << "bx: " << bx.first << ", " << bx.second << endl;
return 0;}
Topic 5 [STL: tuple class]
Problem statement: Define a tuple class object tx with int.string and double elements.
vi) Declare another tuple bx and assign values to bx and swap it with ax
#include <iostream>
#include <bits/stdc++.h>
int main(){
tuple<int,string,double>tx;
//write statements
Code:
#include <iostream>
#include <tuple>
class TupleHandler {
private:
public:
cout << "Int data member: " << get<0>(tx) << endl;
cout << "String data member: " << get<1>(tx) << endl;
cout << "Double data member: " << get<2>(tx) << endl;
get<2>(tx) = newValue;
swap(tx, bx);
cout << "tx: (" << get<0>(tx) << ", " << get<1>(tx) << ", " << get<2>(tx) << ")" << endl;
};
int main() {
TupleHandler handler;
handler.printInt();
handler.printString();
handler.printDouble();
handler.modifyDouble(3.7);
handler.swapWith(bx);
handler.printTuple();
cout << "bx: (" << get<0>(bx) << ", " << get<1>(bx) << ", " << get<2>(bx) << ")" << endl;
return 0;
Problem statement: Create a menu operated manipulation for linked list using vector
class. Follow the following table for the details of each item of the following menu
Item Functions
Delete Delete an specific element input by the user from the linked list ax
Display all Display all the existing elements of the linked list ax
#include <iostream>
#include <bits/stdc++.h>
vector<int>ax;
//write functions
int main(){
//write statements
1. Insert
2. Delete
3. Search
4. Display list
5. Exit
1. Insert at First
2. Insert at Last
3. Insert Before
4. Insert After
5. Exit
Code:
#include <iostream>
#include <vector>
private:
vector<int> ax;
public:
ax.insert(ax.begin(), value);
ax.push_back(value);
cout << (it != ax.end() ? "Element found." : "Element not found.") << endl;
if (ax.empty()) {
return;
for (int elem : ax) cout << elem << " ";
};
int main() {
VectorHandler handler;
while (true) {
cout << "**** Main Menu ****\n1. Insert\n2. Delete\n3. Search\n4. Display list\n5. Exit\nEnter
your option: ";
if (option == 5) break;
switch (option) {
case 1:
cout << "**** Insert Sub Menu ****\n1. Insert at First\n2. Insert at Last\n3. Insert Before\n4.
Insert After\n5. Exit\nEnter your option: ";
if (subOption == 5) break;
if (subOption == 1) handler.insertAtFirst(value);
else if (subOption == 3) {
handler.insertBefore(existingValue, value);
} else if (subOption == 4) {
handler.insertAfter(existingValue, value);
break;
case 2:
handler.deleteElement(value);
break;
case 3:
cout << "Enter value to search: ";
handler.searchElement(value);
break;
case 4:
handler.displayList();
break;
default:
return 0;
Problem statement: Write a program to create and manipulate Stack using stack class
#include <iostream>
#include <bits/stdc++.h>
int main(){
stack<int>st;
//write statements}
Code
#include <iostream>
#include <stack>
class StackHandler {
private:
stack<int> st;
public:
st.push(value);
void popData() {
if (!st.empty()) st.pop();
if (!st.empty()) cout << "Top element: " << st.top() << endl;
cout << "Stack is " << (st.empty() ? "empty" : "not empty") << endl;
};
int main() {
StackHandler handler;
int option, value;
while (true) {
cout << "**** Menu ****\n1. Push\n2. Pop\n3. Top\n4. Check Empty\n5. Exit\nEnter your option:
";
if (option == 5) break;
switch (option) {
case 1:
handler.pushData(value);
break;
case 2:
handler.popData();
break;
case 3:
handler.displayTop();
break;
case 4:
handler.checkEmpty();
break;
default:
return 0;}
Topic 8 [STL: queue class]
Problem statement: Write a program to create and manipulate Queue using queue
#include <iostream>
#include <bits/stdc++.h>
int main(){
queue<int>qu;
//write statements
Code
#include <iostream>
#include <queue>
class QueueManager {
private:
std::queue<int> qu;
public:
qu.push(value);
void pop() {
if (!qu.empty()) {
qu.pop();
if (!qu.empty()) {
if (!qu.empty()) {
if (qu.empty()) {
} else {
};
int main() {
QueueManager qm;
qm.push(10);
qm.push(20);
qm.push(30);
qm.displayFront();
qm.displayBack();
qm.pop();
qm.displayFront();
qm.displayBack();
qm.checkEmpty();
return 0;
Problem statement: Write a program to create and manipulate linked list using list
iii) display all the elements of the list in forward direction with user-defined
iv) display all the elements of the list in reverse direction with a user-defined
xi) insert a new element x after an existing element y using insert() method
predicate function
#include <iostream>
#include <bits/stdc++.h>
#include <iterator>
#include <algorithm>
int main(){
list<int>li;
//write statements
}
Code:
#include <iostream>
#include <list>
#include <algorithm>
class ListManager {
private:
std::list<int> li;
public:
for (auto it = li.begin(); it != li.end(); ++it) std::cout << *it << " ";
for (auto it = li.rbegin(); it != li.rend(); ++it) std::cout << *it << " ";
if (!li.empty()) std::cout << "Front element: " << li.front() << std::endl;
if (!li.empty()) std::cout << "Back element: " << li.back() << std::endl;
std::cout << (it != li.end() ? "Element found." : "Element not found.") << std::endl;
std::cout << "Count of element " << x << ": " << count << std::endl;
std::cout << "Count with condition: " << count << std::endl;
void deleteFirstN(int n) {
};
bool isEven(int x) { return x % 2 == 0; }
int main() {
ListManager lm;
lm.insertFront(0);
lm.insertFront(-1);
lm.display();
lm.displayRev();
lm.displayFront();
lm.displayBack();
lm.deleteFront();
lm.deleteBack();
lm.display();
lm.findAndDisplay(5);
lm.insertBefore(3, 10);
lm.insertAfter(3, 20);
lm.display();
lm.countElement(3);
lm.countWithCondition(isEven);
lm.deleteElement(20);
lm.deleteFirstN(4);
lm.display();
lm.deleteIf(isEven);
lm.display();
lm.assignFromList(otherList);
lm.display();
lm.assignFromArray(arr, 5);
lm.display();
lm.sortList();
lm.display();
lm.uniqueList();
lm.display();
return 0;