You are on page 1of 44

EXERCISE-1a)

Aim: Implementing C++ program for illustrating variable scope


Description
A scope is a region of the program and broadly speaking there are three places, where variables can be declared

 Inside a function or a block which is called local variables,
 In the definition of function parameters which is called formal parameters.
 Outside of all functions which is called global variables.
Source Code
#include<iostream>
using namespace std;
int g=100;// g has global scope
int main()
{
cout<<”g value=”<<g<<endl;
g=10;// g has local scope
cout<<”g value=”<<g<<endl;
return 0;
}
OUTPUT
g value=100
g value=10

1
EXERCISE-1b)
Aim: Implementing C++ program illustrating swap integer values by
reference Description
The call by reference method of passing arguments to a function copies the address of an argument into the
formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means
the changes made to the parameter affect the passed argument.
To pass a value by reference, argument pointers are passed to the functions just like any other value. So
accordingly we need to declare the function parameters as pointer types as in the following function swap(),
which exchanges the values of the two integer variables pointed to, by their arguments.
Source Code
#include<iostream>
using namespace std;
void swap(int *,int *);
int main()
{
int a,b;
cout<<”Enter a,b values:”;
cin>>a>>b;
cout<<”\n Before swap “<< “a=”<<a<<” “<<”b=”<<b;
swap(&a,&b);
cout<<”\n After swap “<< “a=”<<a<<” “<<”b=”<<b;
return 0;
}
void swap(int *x,int *y);
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
OUTPUT

2
EXERCISE-1c)
Aim: Implementing C++ program illustrating checking whether number is even or odd using
ternary operator.
Description
Ternary operator
Syntax of ternary operator is −
(expression-1) ? expression-2 : expression-3
This operator returns one of two values depending on the result of an expression. If "expression-1" is
evaluated to Boolean true, then expression-2 is evaluated and its value is returned as a final result
otherwise expression-3 is evaluated and its value is returned as a final result.
Source Code
#include<iostream>
using namespace
std; int main()
{
int n;
cout<<”Enter n
value:”; cin>>n;
n%2==0?cout<<n<<”is even”: cout<<n<<”is
odd”; return 0;
}
OUTPUT

3
Exercise 2
Aim: Create a Distance class with
Feet and inches as data members
Member function to input distance
Member function to output distance
Member function to add two distance objects
Write a main function to create objects of DISTANCE class. Input two distances and output the
sum.
Description
The 12 inches is equal to 1 feet. While entering the input read inches of distance object less than 12 i.e
atmost value 11.
Source Code
#include<iostream>
using namespace
std; class dist
{
private:
int feet,inch;
public:
void input()
{
cout<<”\n Enter feet and
inches:”; cin>>feet>>inch;
}
void output()
{
cout<<”The resulting distance is”;
cout<<feet<<”feet”<<inch<<”inches”
;
}
void sum(dist x,dist y)
{
feet=x.feet+y.feet;
inch=x.inch+y.inch;
feet=feet+inch/12;
inch=inch+inch
%12;
}
};

int main()
{
dist
x,y,z;
x.input();
y.input();
z.sum(x,y);
z.output()
; return 0;
}
OUTPUT

4
Exercise-3
Aim:-Implementing a C++ program demonstrating a BankAccount with necessary methods and
variables.
Source Code
#include<iostream>
using namespace
std; class bank
{
private:
char
*accountno;
char *name;
float balance;
public:
bank(char *,char
*,float); void display();
void
credit();
void debit();
};
bank::bank(char *a,char *n,float b)
{
accountno=a
; name=n;
balance=b;
}
void bank::display()
{
cout<<”\n
Accountno:”<<accountno; cout<<”\
n Name:”<<name; cout<<”\n
Balance:”<<balance;
}
void bank::credit()
{
float
amount;
display();
cout<<”\n Enter depositing
money:”; cin>>amount;
balance=balance+amount;
display();
}

void bank::debit()
{
float
amount;
display();
cout<<”\n Enter withdraw
money:”; cin>>amount;
if(balance<amount) cout<<”\
n Insufficient funds”; else
balance=balance-
amount; display();

5
}

5
int main()
{
bank b1(“31362721880”,”Ravi”,10000.25);
int ch;
cout<<”\n 1.Account no \n 2.Credit \n 3.Debit \n”;
cout<<”\n Enter the choice:”;
cin>>ch; if(ch==1||
ch==2||ch==3)
{
switch(ch)
{
case 1:
b1.display();
break;
case 2:
b1.credit();
break;
case 3:
b1.debit();
break;
}
}
else
cout<<”Invalid
choice”; return 0;
}
OUTPUT

6
Exercise 4a)
Aim:- Implementing a C++ program to illustrate the use of constructors and destructors in adding
distance between objects.
Description
Constructor and Destructor
 Constructor is a member function that has same name as that of the class name.
 Destructor is a member function that has same name as that of the class name preceded by a
tilde(~),
 The constructor constructs the objects and destructor destroys the object.

Source Code
#include<iostream>
using namespace
std; class dist
{
private:
int feet;
int
inches;
public
: dist()
{
feet=0;
inches=0
;
}
dist(int f, int
i); void
display(); dist
add(dist);
~dist();
};

dist::dist(int f, int i)
{
feet = f;
inches = i;
}
void dist::display()
{

cout<<feet<<"feet"<<inches<<"inches";
}
dist dist::add(dist d)
{
dist temp;
temp.feet=feet+d.feet;
temp.inches=inches+d.inches
;
temp.feet=temp.feet+(temp.inches/
12); temp.inches=temp.inches%12;
return temp;
}
dist::~dist()
{
cout<<"\n destructor is invoked \n"<<endl;
}
7
int main()
{
dist d1(6,8),d2(5,7),d3;
cout<<"\n For object d1
distance:"; d1.display();
cout<<"\n For object d2
distance:"; d2.display();
d3=d1.add(d2);
cout<<"\n The sum of two objects
is:"; d3.display();
return 0;
}
OUTPUT

8
Exercise 4b)
Aim:- Implementing a C++ program for illustrating function overloading in adding the distance
between objects
Description
Function overloading is a feature of object oriented programming where two or more functions can
have the same name but different parameters.
When a function name is overloaded with different jobs it is called Function Overloading.
In Function Overloading “Function” name should be the same and the arguments should be different.
Source Code
#include <iostream>
using namespace
std; class dist
{
private:
int feet;
int
inches;
public
: void getDist();
void showDist();
void addDist(dist,dist);
void addDist(dist *, dist
*);
};
void dist::getDist()
{
cout<<"Enter feet: ";
cin>>feet;
cout<<"Enter inches:
"; cin>>inches;
}
void dist::showDist()
{
cout<<"dist is feet= "<<feet<<", inches= "<<inches<<endl;
}
void dist::addDist(dist d1, dist d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches +
d2.inches; feet = feet + (inches
/ 12); inches = inches % 12;
}
void dist::addDist(dist *d1, dist *d2)
{
feet = d1->feet + d2->feet;
inches = d1->inches + d2-
>inches; feet = feet + (inches /
12);
inches = inches % 12;
}

9
int main()
{
dist d1, d2,
d3,d4;
d1.getDist();
d2.getDist();
d3.addDist(d1, d2);
cout<<"\n For Object D3\
n"; d3.showDist();
cout<<"\n For Object D4\
n"; d4.addDist(&d1, &d2);
d4.showDist();
return 0;
}
OUTPUT

10
Exercise 5a)
Aim:- Implementing a C++ program for illustrating Access specifiers
private,protected,public. Description
In C++, there are three access specifiers:
public - members are accessible from outside the class
private - members cannot be accessed (or viewed) from outside the class
protected - members cannot be accessed from outside the class, however, they can be accessed in inherited
classes

Source Code
#include<iostream>
using namespace
std; class sample
{
private:
int num1;
void show1()
{
cout<<"\n Inside the private
section"; cout<<"\n Enter a
number:"; cin>>num1;
cout<<"Number is:"<<num1;
}
protected:
int num2;
void show2()
{
cout<<"\n Inside the protected
section"; cout<<"\n Enter a number:";
cin>>num2;
cout<<"Number is:"<<num2;
}
public:
int num;
void show()
{
show1();
show2();
cout<<"\n Inside the public
section"; cout<<"\n Enter a
number:"; cin>>num;
cout<<"Number is:"<<num;
}
};
int
main()
{
sample
s;
s.show();
return 0;
}
OUTPUT

11
Exercise 5b)
Aim:- Implementing a C++ program for Inline
function Description
C++ provides an inline functions to reduce the function call overhead. Inline function is a function that is
expanded in line when it is called. When the inline function is called whole code of the inline function gets
inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler
at compile time. Inline function may increase efficiency if it is small.
Source Code
#include<iostream>
using namespace
std; class operation
{
int
a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void
difference();
void product();
void division();
};
inline void operation :: get()
{
cout << "Enter first
value:"; cin >> a;
cout << "Enter second
value:"; cin >> b;
}
inline void operation :: sum()
{
add = a+b;
cout << "Addition of two numbers: " << a+b << "\n";
}
inline void operation :: difference()
{
sub = a-b;
cout << "Difference of two numbers: " << a-b << "\n";
}
inline void operation :: product()
{
mul = a*b;
cout << "Product of two numbers: " << a*b << "\n";
}
inline void operation ::division()
{
div=a/b;
cout<<"Division of two numbers: "<<a/b<<"\n" ;
}

12
int main()
{
cout << "Program using inline function\
n"; operation s;
s.get();
s.sum();
s.difference()
; s.product();
s.division()
; return 0;
}
OUTPUT

13
Exercise 5c)
Aim:- Implementing a C++ program for friend
function Description
 If a function is defined as a friend function in C++, then the protected and private data of a class can be
accessed using the function.
 By using the keyword friend compiler knows the given function is a friend function.
 For accessing the data, the declaration of a friend function should be done inside the body of a class
starting with the keyword friend.

Source Code
#include<iostream>
using namespace
std; class Test
{
private:
int x, y, z;
public:
void input();
void display();
friend void add(Test &t);
};
void Test::input()
{
cout << "Enter two
numbers:"; cin >> x>>y;
}
void Test::display()
{
cout << "sum is:" << z;
}
void add(Test &t)
{
t.z = t.x + t.y;
}
int main()
{
Test t;
t.input()
;
add(t);
t.display()
; return 0;
}
OUTPUT

14
Exercise 6a)
Aim:-Implement a C++ program to overload Unary operator as member
function Description
If an operator is applied on single variable or single operand then that operator is called unary operator.
e.g. +,-,++,--
+a, -a, ++a, - -a,
Source code
#include<iostream>
using namespace
std; class num
{
private:
int
a,b;
public
:
num(int i,int j)
{
a=i
;
b=j;
}
void show(void);
void operator ++(
);
};
void num::show()
{
cout<<"\n a
value="<<a; cout<<"\n
b value"<<b;
}
void num::operator ++( )
{
++a;
++b;
}
int main()
{
num n(10,20);
cout<<"\n The values before
overloading"; n.show();
cout<<"\n The values after overloading";
++n;
n.show();
}

OUTPUT

15
Reg. No:

Exercise 6b)
Aim:-Implement a C++ program to overload Binary operator as non-member function.
Description
If an operator is applied between two operands then that operator is called binary operator.
e.g
+,-,*,/,%
a+b , a-b, a*b, a/b, a%b
Source code
#include<iostream>
using namespace std;
class complex
{
private:
float real;
float imag;
public:
complex(){}
complex(float r, float i)
{
real = r;
imag = i;
}
void display()
{
cout<<real<<"+i"<<imag;
}
friend complex operator +(complex &, complex &);
};
complex operator +(complex &c1, complex &c2)
{
complex temp;
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}
int main()
{
complex c1(3, 4);
complex c2(4, 6);
complex c3 = c1+c2;
c3.display();
return 0;
}
OUTPUT

16
Exercise 6c)
Aim:-Implement a C++ program to overloading assignment
operator Description
The = operator is called assignment operator.
Source code
#include <iostream>
using namespace
std; class number
{
private:
int x;
public:
number(int p)
{
x = p;
}
number operator =(Number &n)
{
return number(n.x);
}
void display()
{
cout<<"x value= "<<x;
}
};
int main()
{
number n1(10);
number n2 =
n1; n2.display();
return 0;
}
OUTPUT

17
Exercise 7 a i)
Aim:- Implement a C++ program for Single
inheritance Description
Single inheritance
If only one base class is used for the derivation of a derived class then that inheritance is called
single inheritance.

Fig: Single Inheritance.

Source code
#include<iostream>
using namespace
std; class A
{
protected:
int a;
};
class B:public A
{
protected:
int b;
public:
void input()
{
cout<<"Enter a,b
values:"; cin>>a>>b;
}
void show()
{
cout<<"\n a value="<<a;
cout<<"\n b value="<<b;
}
};
int main()
{
B b1;
b1.input();
b1.show()
; return 0;
}
OUTPUT

18
Reg. No:

Exercise 7 a ii)
Aim:- Implement a C++ program for Hierarchical inheritance.
Description
When a single base class is used for the derivation of two or more classes, it is known as hierarchical inheritance.

Fig: Hierarchical inheritance


Source code
#include<iostream>
using namespace std;
class A
{
protected:
int a;
};

class B:public A
{
protected:
int b;
public:
void input()
{
cout<<"\n Enter a,b values:";
cin>>a>>b;
}
void show()
{
cout<<"\n a value="<<a;
cout<<"\n b value="<<b;
}
};
class C:public A
{
protected:
int c;
public:
void input()
{
cout<<"\n Enter a,c values:";
cin>>a>>c;
}
void show()
{
cout<<"\n a value="<<a;
cout<<"\n c value="<<c;
}
};

19
int main()
{
B b1;
b1.input();
b1.show()
; C c1;
c1.input();
c1.show()
; return 0;
}
OUTPUT

20
Reg. No:

Exercise 7 a iii)
Aim:- Implement a C++ program for Multiple inheritance
Description
Multiple inheritance
When two or more base classes are used for the derivation of a class, it is called multiple inheritance.

Fig: Multiple Inheritance

Source code
#include<iostream>
using namespace std;
class A
{
protected:
int a;
};
class B
{
protected:
int b;
};
class C:public A,public B
{
protected:
int c;
public:
void input()
{
cout<<"\n Enter a,b,c values:";
cin>>a>>b>
}
void show()
{
cout<<"\n a value="<<a;
cout<<"\n b value="<<b;
cout<<"\n c value="<<c;
}
};
int main()
{
C c1;
c1.input();
c1.show();
return 0;
}
OUTPUT

21
Exercise 7 a iv)
Aim:- Implement a C++ program for Multi-level
inheritance Description
When a new class is derived from another derived class, it is known as Multi-level inheritance.

Fig: Multi-level inheritance

Source code
#include<iostream>
using namespace
std; class A
{
protected:
int a;
};
class B:public A
{
protected:
int b;
};
class C:public B
{
protected:
int c;
public:
void input()
{
cout<<"\n Enter a,b,c
values:"; cin>>a>>b>>c;
}
void show()
{
cout<<"\n a value="<<a;
cout<<"\n b value="<<b;
cout<<"\n c value="<<c;
}
};
int main()
{
C c1;
c1.input();
c1.show()
; return 0;
}
OUTPUT

22
Exercise 7 b)
Aim:- Implement a C++ program to illustrate the order of execution of constructors and destructors in
inheritance
Source code
#include <iostream>
using namespace
std; class A
{
public
: A()
{
cout<<"A's Constructor"<<endl;
}
~A()
{
cout<<"A's Destructor"<<endl;
}
};
class B : A
{
public
: B()
{
cout<<"B's Constructor"<<endl;
}
~B()
{
cout<<"B's Destructor"<<endl;
}
};
class C : B
{
public
: C()
{
cout<<"C's Constructor"<<endl;
}

~C()
{
cout<<"C's Destructor"<<endl;
}
};
int main()
{
C c;
return 0;
}
OUTPUT

23
Reg. No:

Exercise 7 c)
Aim:- Implement a C++ program to show how constructors are invoked in derived class
Source code
#include <iostream>
using namespace std;
class A
{
protected:
int x;
public:
A(int p)
{
x = p;
}
};
class B : public A
{
private:
int y;
public:
B(int p, int q) : A(p)
{
y = q;
}
void display()
{
cout<<"x = "<<x<<endl;
cout<<"y = "<<y;
}
};
int main()
{
B b(10, 20);
b.display();
return 0;
}

24
Reg. No:

Exercise 8 a)
Aim: Implement a C++ program to show Virtual Base Class
Description
Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given class
appearing in an inheritance hierarchy when using multiple inheritances.
Source code
#include<iostream>
using namespace std;
class A
{
protected:
int x;
};
class B: virtual public A
{
protected:
int y;
};
class C:virtual public A
{
protected:
int z;
};
class D:public B,C
{
protected:
int u;
public:
void get()
{
cout<<"\n Enter values of x,y,z and u:";
cin>>x>>y>>z>>u;
}
void put()
{
cout<<"x value="<<x<<"y value="<<y<<"z value="<<z<<"u value="<<u;
}
};
int main()
{
D d1;
d1.get();
d1.put();
return 0;
}
OUTPUT

25
Exercise 8 b)
Aim: Write a case study on using virtual
classes Case Study
Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given class
appearing in an inheritance hierarchy when using multiple inheritances.
Need for Virtual Base Classes:
Consider the situation where we have one class A .This class is A is inherited by two other classes B and C.
Both these class are inherited into another in a new class D as shown in figure below.

As we can see from the figure that data members/function of class A are inherited twice to class D. One
through class B and second through class C. When any data / function member of class A is accessed by an
object of class D, ambiguity arises as to which data/function member would be called? One inherited
through B or the other inherited through C. This confuses compiler and it displays error.

26
Reg. No:

Exercise 9 a)
Aim: Implement a C++ program to illustrate runtime
polymorphism Source code
#include <iostream>
using namespace
std; class Animal
{
public:
virtual void eat()
{
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{
cout<<"Eating bread...";
}
};
int main( )
{
Animal *a;
a=new
Dog(); a-
>eat(); return
0;
}
OUTPUT

27
Reg. No:

Exercise 9 b)
Aim: Implement a C++ program to illustrate this pointer
Source code
#include <iostream>
using namespace std;
class employee
{
public:
int id;
string name;
float salary;
employee(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main()
{
employee e1(101, "Raju", 80000); //creating an object of Employee
employee e2(102, "Ravi", 50000); //creating an object of Employee
e1.display();
e2.display();
return 0;
}
OUTPUT

28
Exercise 9 c)
Aim: Implement a C++ program to illustrate pure virtual function and calculate the area of different
shapes by using abstract class.
Source code
#include<iostream>
using namespace
std; class shape
{
public:
virtual void area() = 0;
};
class rectangle : public shape
{
private:
int l;
int b;
public:
rectangle(int x, int y)
{
l = x;
b = y;
}
void area()
{
cout<<"Area of rectangle is: "<<(l*b)<<endl;
}
};
class circle : public shape
{
private:
int
r; public:
circle(int x)
{
r = x;
}
void area()
{
cout<<"Area of circle is: "<<(3.142*r*r)<<endl;
}
};
int main()
{
shape *s;
s = new rectangle(10,
20); s->area();
s = new
circle(2); s-
>area();
return 0;
}

OUTPUT

29
Reg. No:

Exercise 10 a)
Aim: Implement a C++ program to illustrate template
class Source code
#include <iostream>
using namespace std;
// Class template
template <class
T> class data
{
public:
data( T c)
{
cout<<”\n c value=”<<c;
}
};
int main()
{
data <char> h(‘A’);
data <int> i(100);
data <float> j(50.65);
return 0;
}
OUTPUT

30
Reg. No:

Exercise 10 b)
Aim: Implement a C++ program to illustrate class templates with multiple parameters.
Source code
#include<iostream>
using namespace std;
template<class T1,class T2>
class data
{
public:
data(T1 a,T2 b)
{
cout<<"a= "<<a<<"\n b= "<<b<<endl;
}
};
int main()
{
data<int,float>h(2,2.5f);
data<int,char>i(15,'M');
data<float,int>j(3.12f,50);
}

31
Reg. No:

Exercise 10 c)
Aim: Implement a C++ program to illustrate member function templates
Source code
#include <iostream>
using namespace std;
template <class T>
class data
{
public:
data (T c);
};
template <class T>
data <T>:: data (T c)
{
cout<<”\n c=”<<c;
}
int main()
{
data <char> h(‘A’);
data <int> i(100);
data <float> j(50,65);
return 0;
}
OUTPUT

32
Reg. No:

Exercise 11 a)
Aim: Implement a C++ program for Exception handling Divide by zero
Source code
#include<iostream>
using namespace std;
int main()
{
int a, b, c;
float d;
cout << "Enter the value of a:";
cin>>a;
cout << "Enter the value of b:";
cin>>b;
cout << "Enter the value of c:";
cin>>c;
try
{
if ((a - b) != 0)
{
d = c / (a - b);
cout << "Result is:" << d;
}
else
throw (a - b);
}
catch (int i)
{
cout << "Answer is infinite because a-b is:" << i;
}
return 0;
}
OUTPUT

33
Reg. No:

Exercise 11 b)
Aim: Implement a C++ program to rethrow an exception
Description
Rethrowing an expression from within an exception handler can be done by calling throw, by itself, with no
exception. This causes current exception to be passed on to an outer try/catch sequence. An exception can only be
rethrown from within a catch block. When an exception is rethrown, it is propagated outward to the next catch
block.
Source code
#include<iostream>
using namespace std;
int main()
{
try
{
int a, b;
cout<<"Enter two integer values: ";
cin>>a>>b;
try
{
if(b == 0)
{
throw b;
}
else
{
cout<<(a/b);
}
}
catch(...)
{
throw; //rethrowing the exception
}
}
catch(int)
{
cout<<"Second value cannot be zero";
}
return 0;
}
OUTPUT

34
Reg. No:
Exercise 12 a)
Aim: Implement a C++ program to implement List and List Operations
Source code
#include<iostream>
#include <list>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
list<int> l;
list<int>::iterator it;
int c, i;
cout<<"1.Insert Element at the Front"<<endl;
cout<<"2.Insert Element at the End"<<endl;
cout<<"3.Delete Element at the Front"<<endl;
cout<<"4.Delete Element at the End"<<endl;
cout<<"5.Front Element of List"<<endl;
cout<<"6.Last Element of the List"<<endl;
cout<<"7.Size of the List"<<endl;
cout<<"8.Resize List"<<endl;
cout<<"9.Remove Elements with Specific Values"<<endl;
cout<<"10.Remove Duplicate Values"<<endl;
cout<<"11.Reverse the order of elements"<<endl;
cout<<"12.Display the List"<<endl;
cout<<"13.Exit"<<endl;
while (1)
{
cout<<"Enter your Choice: ";
cin>>c;
switch(c) {
case 1:
cout<<"Enter value to be inserted at the front: ";
cin>>i;
l.push_front(i);
break;
case 2:
cout<<"Enter value to be inserted at the end: ";
cin>>i;
l.push_back(i);
break;
case 3:
i= l.front();
l.pop_front();
cout<<"Element "<<i<<" deleted"<<endl;
break;
case 4:
i= l.back();
l.pop_back();
cout<<"Element "<<i<<" deleted"<<endl;
break;
case 5:
cout<<"Front Element of the List: ";
cout<<l.front()<<endl;
break;

35
case
6: cout<<"Last Element of the List:
"; cout<<l.back()<<endl;
break;

case 7:
cout<<"Size of the List:
"<<l.size()<<endl; break;
case 8:
cout<<"Enter new size of the List:
"; cin>>i;
if (i <= l.size())
l.resize(i)
; else
l.resize(i,
0); break;
case 9:
cout<<"Enter element to be deleted:
"; cin>>i;
l.remove(i)
; break;
case 10:
l.unique();
cout<<"Duplicate Items
Deleted"<<endl; break;
case 11:
l.reverse();
cout<<"List
reversed"<<endl; break;
case 12:
cout<<"Elements of the List: ";
for (it = l.begin(); it != l.end(); it+
+) cout<<*it<<" ";
cout<<endl
; break;
case 13:
exit(1)
; break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
OUTPUT

36
Reg. No:

Exercise 12 b)
Aim: Implement a C++ program to implement Vector and Vector Operations
Source code
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
vector<int> ss;
vector<int>::iterator it;
int choice, item;
cout<<"1.Insert Element into the Vector"<<endl;
cout<<"2.Delete Last Element of the Vector"<<endl;
cout<<"3.Size of the Vector"<<endl;
cout<<"4.Display by Index"<<endl;
cout<<"5.Dislplay by Iterator"<<endl;
cout<<"6.Clear the Vector"<<endl;
cout<<"7.Exit"<<endl;
while (1)
{
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
ss.push_back(item);
break;
case 2:
cout<<"Delete Last Element Inserted:"<<endl;
ss.pop_back();
break;
case 3:
cout<<"Size of Vector: ";
cout<<ss.size()<<endl;
break;
case 4:
cout<<"Displaying Vector by Index: ";
for (int i = 0; i < ss.size(); i++)
{
cout<<ss[i]<<" ";
}
cout<<endl;
break;
case 5:
cout<<"Displaying Vector by Iterator: ";
for (it = ss.begin(); it != ss.end(); it++)
{
cout<<*it<<" ";
}
cout<<endl;
break;
37
case 6:
ss.clear();
cout<<"Vector
Cleared"<<endl; break;
case 7:
exit(1)
;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

OUTPUT

38
Reg. No:

Exercise 13 a)
Aim: Implement a C++ program to implement Deque and Deque Operations
Source code
#include<iostream>
using namespace std;
#define SIZE 10
class dequeue
{
int a[20],f,r;
public:
dequeue();
void insert_at_beg(int);
void insert_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void show();
};
dequeue::dequeue()
{
f=-1;
r=-1;
}
void dequeue::insert_at_end(int i)
{
if(r>=SIZE-1)
cout<<"\n insertion is not possible, overflow!!!!";
else
{
if(f==-1)
{
f++;
r++;
}
else
r=r+1;
a[r]=i;
cout<<"\nInserted item is"<<a[r];
}
}
void dequeue::insert_at_beg(int i)
{
if(f==-1)
{
f=0;
a[++r]=i;
cout<<"\n inserted element is:"<<i;
}
else if(f!=0)
{
a[--f]=i;
cout<<"\n inserted element is:"<<i;
}
else
cout<<"\n insertion is not possible, overflow";
}
39
void dequeue::delete_fr_front()
{
if(f==-1)
{
cout<<"deletion is not possible::dequeue is
empty"; return;
}
else
{
cout<<"the deleted element
is:"<<a[f]; if(f==r)
{
f=r=-1;
return;
}
else
f=f+1;
}
}
void dequeue::delete_fr_rear()
{
if(f==-1)
{
cout<<"deletion is not possible::dequeue is
empty"; return;
}
else
{
cout<<"the deleted element
is:"<<a[r]; if(f==r)
{
f=r=-1;
}
else
r=r-1;
}
}
void dequeue::show()
{
if(f==-1)
cout<<"Dequeue is
empty"; else
{
for(int i=f;i<=r;i+
+) cout<<a[i]<<"
";
}
}

40
int main()
{
int c,i;
dequeue d;
cout<<"\n 1.insert at
beginning"; cout<<"\n 2.insert
at end"; cout<<"\n 3.show";
cout<<"\n 4.deletion from
front"; cout<<"\n 5.deletion
from rear"; cout<<"\n 6.exit";
do
{
cout<<"\n enter your
choice:"; cin>>c;
switch(c)
{
case 1:
cout<<"enter the element to be
inserted"; cin>>i;
d.insert_at_beg(i)
; break;
case 2:
cout<<"enter the element to be
inserted"; cin>>i;
d.insert_at_end(i)
; break;
case 3:
d.show()
;
break;
case 4:
d.delete_fr_front()
; break;
case 5:
d.delete_fr_rear()
;
break;
case 6:
exit(1)
; break;
default:
cout<<"invalid
choice"; break;
}
} while(c!=7);
}
OUTPUT

41
Exercise 13 b)
Aim: Implement a C++ program to implement Map and Map
Operations Source code
#include<iostream>
#include <map>
#include <string>
using namespace
std; int main () {
map<char, int> m;
map<char, int>::iterator it;
m.insert (pair<char, int>('a', 10));
m.insert (pair<char, int>('b', 20));
m.insert (pair<char, int>('c', 30));
m.insert (pair<char, int>('d', 40));
cout<<"Size of the map: "<< m.size()
<<endl; cout << "map contains:\n";
for (it = m.begin(); it != m.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\
n'; for (char c = 'a'; c <= 'd'; c++) {
cout << "There are " << m.count(c) << " element(s) with key " << c <<
":"; map<char, int>::iterator it;
for (it = m.equal_range(c).first; it != m.equal_range(c).second; +
+it) cout << ' ' << (*it).second;
cout << endl;
}
if (m.count('a'))
cout << "The key a is present\
n"; else
cout << "The key a is not present\
n"; if (m.count('f'))
cout << "The key f is present\
n"; else
cout << "The key f is not present\
n"; it = m.find('b');
m.erase (it);
cout<<"Size of the map:
"<<m.size()<<endl; cout << "map contains:\
n";
for (it = m.begin(); it != m.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\
n'; return 0;
}

OUTPUT

42

You might also like