0% found this document useful (0 votes)
12 views28 pages

Oop 6 Last

it is

Uploaded by

deyafabliha
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)
12 views28 pages

Oop 6 Last

it is

Uploaded by

deyafabliha
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/ 28

Topic 1[Exception Handling]

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

i) a catch block receives the value of i

ii) a catch block receives string “Out of Range Error”

iii) a default catch() if above two catch block doesn’t match

#include <iostream>

using namespace std;

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>

using namespace std;

class ArrayHandler {

int ax[5] = {10, 20, 60, 40, 30};

public:

void printElement(int i) {

try {

if (i < 0 || i >= 5) {
throw i;

cout << "ax[" << i << "] = " << ax[i] << endl;

catch (int index) {

cout << "Index " << index << " is out of range!" << endl;

catch (const char* msg) {

cout << msg << endl;

catch (...) {

cout << "An unknown error occurred." << endl;

};

int main() {

int i;

ArrayHandler handler;

cout << "Enter index: ";

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,

douuble+int and double+double . To achieve it, change the definition of

x,y,setData() and Sum() accordingly.

class A{

private:

int x;

int y;

public:

void setData(int x,int y){

this->x=x;

this->y=y;

int Sum(){

int s;

s=x+y;

return s;

};

int main(){

//write required statements to call SetData() & Sum()

Code:
#include <iostream>

using namespace std;

template <typename T>

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:

i) Assign 10,60,30,70,20 to ax using single statement

ii) Print third element of ax using at() function

iii) Print first element of ax using front() function

iv) Print last element of ax using back() function

v) Fill the elements of ax using fill() function

vi) Test whether ax is empty or not using empty() function

vii) Print size of ax

viii) Print maximum size of ax using max_size() function

ix) Print address of first element of ax using begin() function

x) Print address of last element of ax using end() function

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main(){

array<int,6>ax;

//write statements

Code:
#include <iostream>

#include <array>

using namespace std;

class ArrayHandler {

private:

array<int, 6> ax;

public:
ArrayHandler() : ax{10, 60, 30, 70, 20, 0} {}

void printThirdElement() const {

cout << "Third element: " << ax.at(2) << endl;

void printFirstElement() const {

cout << "First element: " << ax.front() << endl;

void printLastElement() const {

cout << "Last element: " << ax.back() << endl;

void fillArray(int value) {

ax.fill(value);

cout << "Array after fill(): ";

for (const auto& elem : ax) {

cout << elem << " ";

cout << endl;

void checkIfEmpty() const {

cout << "Is the array empty? " << (ax.empty() ? "Yes" : "No") << endl;

void printSize() const {

cout << "Size of array: " << ax.size() << endl;


}

void printMaxSize() const {

cout << "Maximum size of array: " << ax.max_size() << endl;

void printAddresses() const {

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

statements to do the following

i) Assign 10 to int and “Rajshahi” to px using make_pair() function

ii) Print int data member by first

iii) Print string data member by second

iv) Modify first data member to 20 using get<>() function

v) Declare another pair bx and assign values to bx and swap it with ax

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main(){

pair<int,string>px;

//write statements

Code:
#include <iostream>

#include <utility>

using namespace std;

class PairHandler {

private:

pair<int, string> px;

public:

void setData(int num, const string& str) {

px = make_pair(num, str);

}
void printFirst() const {

cout << "First element: " << px.first << endl;}

void printSecond() const {

cout << "Second element: " << px.second << endl;}

void modifyFirst(int newValue) {

px.first = newValue;}

void swapPairs(pair<int, string>& bx) {

swap(px, bx);

void printPair() const {

cout << "px: " << px.first << ", " << px.second << endl;

};

int main() {

PairHandler handler;

handler.setData(10, "Rajshahi");

handler.printFirst();

handler.printSecond();

handler.modifyFirst(20);

pair<int, string> bx = make_pair(30, "Dhaka");

handler.swapPairs(bx);

cout << "After swap:" << endl;

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.

Write statements to do the following

i) Assign <100,”Kamal”,3.5> to tx using make_tuple() function

ii) Print int data member by get() function

iii) Print string data member by get() function

iv) Print double data member by get() function

v) Modify third data member to 3.7 using get<>() function

vi) Declare another tuple bx and assign values to bx and swap it with ax

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main(){

tuple<int,string,double>tx;

//write statements

Code:
#include <iostream>

#include <tuple>

using namespace std;

class TupleHandler {

private:

tuple<int, string, double> tx;

public:

void setData(int num, const string& str, double dbl) {


tx = make_tuple(num, str, dbl);

void printInt() const {

cout << "Int data member: " << get<0>(tx) << endl;

void printString() const {

cout << "String data member: " << get<1>(tx) << endl;

void printDouble() const {

cout << "Double data member: " << get<2>(tx) << endl;

void modifyDouble(double newValue) {

get<2>(tx) = newValue;

void swapWith(tuple<int, string, double>& bx) {

swap(tx, bx);

void printTuple() const {

cout << "tx: (" << get<0>(tx) << ", " << get<1>(tx) << ", " << get<2>(tx) << ")" << endl;

};

int main() {
TupleHandler handler;

handler.setData(100, "Kamal", 3.5);

handler.printInt();

handler.printString();

handler.printDouble();

handler.modifyDouble(3.7);

tuple<int, string, double> bx = make_tuple(200, "Ravi", 4.2);

handler.swapWith(bx);

cout << "After swap:" << endl;

handler.printTuple();

cout << "bx: (" << get<0>(bx) << ", " << get<1>(bx) << ", " << get<2>(bx) << ")" << endl;

return 0;

Topic 6 [STL: vector class]

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

Insert Insert a new element at first/last/after an existing element/before an

existing element in the linked list ax

Delete Delete an specific element input by the user from the linked list ax

Search Search a specific element given by the user from ax

Display all Display all the existing elements of the linked list ax

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

vector<int>ax;

//write functions

int main(){

//write statements

**** Main Menu ****

1. Insert

2. Delete

3. Search

4. Display list

5. Exit

Enter your option:

**** Insert Sub Menu ****

1. Insert at First

2. Insert at Last

3. Insert Before

4. Insert After

5. Exit

Enter your option:

Code:
#include <iostream>

#include <vector>

using namespace std;


class VectorHandler {

private:

vector<int> ax;

public:

void insertAtFirst(int value) {

ax.insert(ax.begin(), value);

void insertAtLast(int value) {

ax.push_back(value);

void insertBefore(int existingValue, int newValue) {

auto it = find(ax.begin(), ax.end(), existingValue);

if (it != ax.end()) ax.insert(it, newValue);

void insertAfter(int existingValue, int newValue) {

auto it = find(ax.begin(), ax.end(), existingValue);

if (it != ax.end()) ax.insert(it + 1, newValue);

void deleteElement(int value) {

auto it = find(ax.begin(), ax.end(), value);

if (it != ax.end()) ax.erase(it);

void searchElement(int value) const {


auto it = find(ax.begin(), ax.end(), value);

cout << (it != ax.end() ? "Element found." : "Element not found.") << endl;

void displayList() const {

if (ax.empty()) {

cout << "List is empty." << endl;

return;

cout << "List elements: ";

for (int elem : ax) cout << elem << " ";

cout << endl;

};

int main() {

VectorHandler handler;

int option, subOption, value, existingValue;

while (true) {

cout << "**** Main Menu ****\n1. Insert\n2. Delete\n3. Search\n4. Display list\n5. Exit\nEnter
your option: ";

cin >> 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: ";

cin >> subOption;

if (subOption == 5) break;

cout << "Enter value: ";

cin >> value;

if (subOption == 1) handler.insertAtFirst(value);

else if (subOption == 2) handler.insertAtLast(value);

else if (subOption == 3) {

cout << "Enter existing value to insert before: ";

cin >> existingValue;

handler.insertBefore(existingValue, value);

} else if (subOption == 4) {

cout << "Enter existing value to insert after: ";

cin >> existingValue;

handler.insertAfter(existingValue, value);

break;

case 2:

cout << "Enter value to delete: ";

cin >> value;

handler.deleteElement(value);

break;

case 3:
cout << "Enter value to search: ";

cin >> value;

handler.searchElement(value);

break;

case 4:

handler.displayList();

break;

default:

cout << "Invalid option." << endl;

return 0;

Topic 7 [STL: stack class]

Problem statement: Write a program to create and manipulate Stack using stack class

and Perform the following operations using the specified method.

i) use push() method to push data

ii) use pop() method to pop data

iii) use top() method to display top element

iv) use empty() method to check whether stack is empty or not

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main(){

stack<int>st;

//write statements}
Code
#include <iostream>

#include <stack>

using namespace std;

class StackHandler {

private:

stack<int> st;

public:

void pushData(int value) {

st.push(value);

void popData() {

if (!st.empty()) st.pop();

void displayTop() const {

if (!st.empty()) cout << "Top element: " << st.top() << endl;

void checkEmpty() const {

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:
";

cin >> option;

if (option == 5) break;

switch (option) {

case 1:

cout << "Enter value to push: ";

cin >> value;

handler.pushData(value);

break;

case 2:

handler.popData();

break;

case 3:

handler.displayTop();

break;

case 4:

handler.checkEmpty();

break;

default:

cout << "Invalid option." << endl;

return 0;}
Topic 8 [STL: queue class]

Problem statement: Write a program to create and manipulate Queue using queue

class. Perform the following operations using the specified method.

i) use push() method to push data

ii) use pop() method to pop data

iii) use front() method to display front element

iv) use back() method to display rear element

v) use empty() method to check whether queue is empty or not

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main(){

queue<int>qu;

//write statements

Code
#include <iostream>

#include <queue>

class QueueManager {

private:

std::queue<int> qu;

public:

void push(int value) {

qu.push(value);

void pop() {
if (!qu.empty()) {

qu.pop();

void displayFront() const {

if (!qu.empty()) {

std::cout << "Front element: " << qu.front() << std::endl;

void displayBack() const {

if (!qu.empty()) {

std::cout << "Rear element: " << qu.back() << std::endl;

void checkEmpty() const {

if (qu.empty()) {

std::cout << "Queue is empty." << std::endl;

} else {

std::cout << "Queue is not empty." << std::endl;

};

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;

Topic 9 [STL: list class]

Problem statement: Write a program to create and manipulate linked list using list

class and its following methods to

i) insert 8 integers using push_back() method

ii) insert two elements using push_front() method

iii) display all the elements of the list in forward direction with user-defined

Display() method using begin() and end() methods and iterator

iv) display all the elements of the list in reverse direction with a user-defined

DisplayRev() method using rbegin() and rend() method and iterator

v) display front element using front() method

vi) display back element using back() method


vii) delete front element using pop_back() method

viii) delete front element using pop_front() method

ix) search an element x using find() method

x) insert a new element x before an existing element y using insert() method

xi) insert a new element x after an existing element y using insert() method

xii) count a particular element x

xiii) count a elements with condition using predicate function

xiv) delete a particular element x with erase() method

xv) delete first 4 elements with erase() method

xvi) delete a particular element x with remove() method

xvii) delete a elements with condition using remove_if() method using

predicate function

xviii) assign elements from another list using assign() method

xix) assign elements from an array using assign() method

xx) sort the list using sort() method

xxi) Delete consecutive similar elements using unique() method

#include <iostream>

#include <bits/stdc++.h>

#include <iterator>

#include <algorithm>

using namespace std;

int main(){

list<int>li;

//write statements

}
Code:
#include <iostream>

#include <list>

#include <algorithm>

class ListManager {

private:

std::list<int> li;

public:

void insertBack(int value) { li.push_back(value); }

void insertFront(int value) { li.push_front(value); }

void display() const {

for (auto it = li.begin(); it != li.end(); ++it) std::cout << *it << " ";

std::cout << std::endl;

void displayRev() const {

for (auto it = li.rbegin(); it != li.rend(); ++it) std::cout << *it << " ";

std::cout << std::endl;

void displayFront() const {

if (!li.empty()) std::cout << "Front element: " << li.front() << std::endl;

void displayBack() const {

if (!li.empty()) std::cout << "Back element: " << li.back() << std::endl;

void deleteFront() { if (!li.empty()) li.pop_front(); }

void deleteBack() { if (!li.empty()) li.pop_back(); }

void findAndDisplay(int x) const {


auto it = std::find(li.begin(), li.end(), x);

std::cout << (it != li.end() ? "Element found." : "Element not found.") << std::endl;

void insertBefore(int existing, int newValue) {

auto it = std::find(li.begin(), li.end(), existing);

if (it != li.end()) li.insert(it, newValue);

void insertAfter(int existing, int newValue) {

auto it = std::find(li.begin(), li.end(), existing);

if (it != li.end()) li.insert(std::next(it), newValue);

void countElement(int x) const {

int count = std::count(li.begin(), li.end(), x);

std::cout << "Count of element " << x << ": " << count << std::endl;

void countWithCondition(bool (*predicate)(int)) const {

int count = std::count_if(li.begin(), li.end(), predicate);

std::cout << "Count with condition: " << count << std::endl;

void deleteElement(int x) { li.remove(x); }

void deleteFirstN(int n) {

for (int i = 0; i < n && !li.empty(); ++i) li.pop_front();

void deleteIf(bool (*predicate)(int)) { li.remove_if(predicate); }

void assignFromList(const std::list<int>& other) { li.assign(other.begin(), other.end()); }

void assignFromArray(const int arr[], size_t size) { li.assign(arr, arr + size); }

void sortList() { li.sort(); }

void uniqueList() { li.unique(); }

};
bool isEven(int x) { return x % 2 == 0; }

int main() {

ListManager lm;

for (int i = 1; i <= 8; ++i) lm.insertBack(i);

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();

std::list<int> otherList = {100, 200, 300};

lm.assignFromList(otherList);

lm.display();

int arr[] = {1, 2, 3, 4, 5};

lm.assignFromArray(arr, 5);

lm.display();

lm.sortList();

lm.display();

lm.uniqueList();

lm.display();

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy