You are on page 1of 50

C++ LAB MANUAL

R16 - REGULATION
Prepared By

DR D.Haritha
Exercise – 1 (Basics)
Write a Simple Program on printing “Hello World” and “Hello Name” where name is the input
from the user
a) Convert any two programs that are written in C into C++
b) Write a description of using g++ (150 Words)
Exercise – 2 (Expressions Control Flow)
a) Write a Program that computes the simple interest and compound interest payable on
principalamount(inRs.)ofloanborrowedbythecustomerfromabankforagiverperiodof time (in
years) at specific rate of interest. Further determine whether the bank will benefit by charging
simple interest or compound interest.
b) WriteaProgramtocalculatethefareforthepassengerstravelinginabus. WhenaPassenger enters the
bus, the conductor asks “What distance will you travel?” On knowing distance from passenger
(as an approximate integer), the conductor mentions the fare to the passenger according
to following criteria.
Exercise – 3 (Variables, Scope, Allocation)
a) Write a program to implement call by value and call by reference using reference variable.
b) Write a program to illustrate scope resolution, new and delete Operators. (Dyanamic Memory
Allocation)
c) Write a program to illustrate Storage classes
d) Write a program to illustrate Enumerations
I Year - II Semester
LTPC
0032
OBJECT-ORIENTED PROGRAMMING LAB
Exercises –4 (Functions)
Write a program illustrating Inline Functions
a) Write a program illustrate function overloading. Write 2 overloading functions for power.
b) Write a program illustrate the use of default arguments for simple interest function.
Exercise -5 (Functions –Exercise Continued)
a) Write a program to illustrate function overloading. Write 2 overloading functions for adding
two
numbers
b) Write a program illustrate function template for power of a number.
c) Write a program to illustrate function template for swapping of two numbers.
Exercise -6 (Classes Objects)
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
a). Write a main function to create objects of DISTANCE class. Input two distances and output
the
sum.
b). Write a C++ Program to illustrate the use of Constructors and Destructors (use the
above program.)
c) Write a program for illustrating function overloading in adding the distance between objects
(use
the above problem)
d). Write a C++ program demonstrating a BankAccount with necessary methods and variables
Exercise – 7 (Access)
Write a program for illustratingAccess Specifiers public, private, protected
a) Write a program implementing Friend Function
b) Write a program to illustrate this pointer
c) Write a Program to illustrate pointer to a class
d)
Exercise -8 (Operator Overloading)
a). Write a program to Overload Unary, and Binary Operators as Member Function, and Non
Member Function.
i. Unary operator as member function
ii. Binary operator as nonmember function
b). Write a c ++ program to implement the overloading assignment = operator
c).Write a case study on Overloading Operators and Overloading Functions (150 Words)
Exercise -9 (Inheritance)
a) Write C++ Programs and incorporating various forms of Inheritance
i) Single Inheritance
ii) Hierarchical Inheritance
iii) Multiple Inheritances
iv) Multi-level inheritance
v) Hybrid inheritance
b) Write a program to show Virtual Base Class
c) Write a case study on using virtual classes (150 Words)
Exercise-10 (Inheritance –Continued)
a) Write a Program in C++ to illustrate the order of execution of constructors and destructors in
inheritance
b) Write a Program to show how constructors are invoked in derived class
Exercise -11 (Polymorphism)
a) Write a program to illustrate runtime polymorphism
b) Write a program to illustrate this pointer
c) Write a program illustrates pure virtual function and calculate the area of different shapes by
using abstract class.
d) Write a case study on virtual functions (150 Words)
Exercise -12(Templates)
a) Write a C++ Program to illustrate template class
b) Write a Program to illustrate class templates with multiple parameters
c) Write a Program to illustrate member function templates
Exercise -13 (Exception Handling)
a).Write a Program for Exception Handling Divide by zero
b). Write a Program to rethrow an Exception
Exercise -14 (STL)
a) Write a Program to implement List and List Operations
b) Write a Program to implementVector andVector Operations
Exercise -15 (STLContinued)
a) Write a Program to implement Deque and Deque Operations
b) Write a Program to implement Map and Map Operations
EXERCISE-1
Write a Simple Program on printing “Hello World” and “Hello Name” where name is the input
from the user
a) Convert any two programs that are written in C into C++
b) Write a description of using g++ (150 Words)

Algorithm:
Step1: Declare header file iostream.h
Step2: Display the message “Hello World” using cout
Step3: Display message to enter the name using cout

Step4: Read the name using cin


Step5 : Display the message “Hello Name” using cout

Exercise: Check whether the name entered begins with alphabet. If not display the error until
user enters name with alphabet

a) Convert any two programs that are written in C into C++

1:Convert program to find factorial

Algorithm:
Step1: Display message to enter the number using cout.
Step2: Find factorial using loop statement
Step3: Display the factorial of the given number

Exercise: Check whether the number is positive number. If not display the error until user enters
positive number.

2:Find the sum of first n integers

Algorithm:
Step1: Display message to enter the number using cout.
Step2: Find the sum of numbers from 1 to n using loop statement
Step3: Display the sum of the given number

Exercise: Check whether the number is positive number. If not display the error until user enters
positive number.

b) Write a description of using g++ (150 Words)

The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, and
Go, as well as libraries for these languages (libstdc++,...). GCC was originally written as the
compiler for the GNU operating system. The GNU system was developed to be 100% free
software, free in the sense that it respects the user's freedom.
Released by the Free Software Foundation, g++ is a *nix-based C++ compiler usually operated
via the command line. It often comes distributed with a *nix installation, so if you are running
Unix or a Linux variant you likely have it on your system. You can invoke g++ on a source code
file simply by typing
g++ filename
The default executable output of g++ is "a.out". It is also possible to specify a name for the
executable file at the command line by using the syntax
-o outputfile
, as shown in the following example:
g++ filename -o outputfile

Exercise:Repeat the above programs using g++ compiler


EXERCISE-1
Write a Simple Program on printing “Hello World” and “Hello Name” where name is the input
from the user
a) Convert any two programs that are written in C into C++
b) Write a description of using g++ (150 Words)

#include<iostream.h>

void main()

cout<<”Hello World\n”;

cout<<”enter name\n”;

cin>>name;

cout<<”Hello”<<name;

a)1:
#include<iostream.h>

void main()

cout<<”enter the number\n”;

cin>>n;

int i, fact=1;

for(i=1;i=n;i++)

fact=fact*i;

cout<<”factorial of ”<<n<<” is ”<<fact;

}
2:
#include<iostream.h>

void main()

cout<<”enter the number\n”;

cin>>n;

int i, sum=0;

for(i=1;i=n;i++)

sum+=I;

cout<<”sum of first ”<<n<<” integers is ”<<sum;

}
EXERCISE-2 (Expression control flow)
a. Write a Program that computes the simple interest and compound interest payable on
principal amount (in Rs.) of loan borrowed by the customer from a bank for a giver
period of time (in years) at specific rate of interest. Further determine whether the
bank will benefit by charging simple interest or compound interest.

CODE:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
Int p,t,r;
Void main()
{
Clrscr();
Float si();//function prototype
Float ci();
Float s,c;
Cout<<”enter p, t,r”;
Cin>>p>>t>>r;
S=si();
C=ci();
Cout<<”\n simple interest =”<<s;
Cout<<”\n compound interest=”<<c;
Cout<<”\n diff ci& si=”<<c-s;
Getch();
}
Float si()
{
Return((p*t*r)/100);
}
Float ci();
{
Return(p*pow((1+r/100),t)-p);//function definition
}

Output:

Enter p,t,r 2 4 50
Simple interest=4
Compound interest=8.125
Diff ci&si=4.125

b. Write a Program to calculate the fare for the passengers travelling in a bus. When a
Passenger enters the bus, the conductor asks “What distance will you travel?” On
knowing distance from passenger (as an approximate integer), the conductor
mentions the fare to the passenger according to following criteria.

CODE:-
EXERCISE-3 (Variables, scope, allocation)
AIM:
a. Write a program to implement call by value and call by reference using reference
variable.

CODE:
b. Write a program to illustrate scope resolution, new and delete Operators.
(Dyanamic Memory Allocation)

CODE:- FOR SCOPE


CODE:- NEW ,DELETE

c. Write a program to illustrate Storage classes

CODE:-
#include<iostream.h>
#include<conio.h>
#include"f2.cpp"
void main()
{
clrscr();
void f1();
f1();
f1();
f1();
getch();
}
void f1()
{
extern int e;
register r;
auto i;
static s;
e++;
r++;
i++;
s++;
cout<<"\n extern:"<<e;
cout<<"\n auto:"<<i;
cout<<"\n register:"<<r;
cout<<"\n static:"<<s;
}

d. Write a program to illustrate Enumerations


EXERCISE-4(functions)
Write a program illustrating Inline Functions
a. Write a program illustrate function overloading. Write 2 overloading functions for
power.
#include<iostream.h>
inline void p1()
{
cout<<"sample program";
}
void main()
{
p1();
}
output:
sample program

CODE:-
#include<conio.h>
#include<math.h>
double power(int,int);
double power(double,int);
int main()
{
clrscr();
double d;
d=power(10,3);
cout<<"the result is:"<<d;
double d1;
d1=power(4.3,2);
cout<<"the result is :"<<d1;
getch();
return(0);
}
double power(int m,int n)
{
double p;
p=pow(m,n);
return(p);
}
double power(double k,int n)
{
double q;
q=pow(k,n);
return(q);
}

Output:
The result is 1000
The result is 18.49

b. Write a program illustrate the use of default arguments for simple interest function.

Code:-

EXERCISE-5
a. Write a program to illustrate function overloading. Write 2 overloading functions for
adding two numbers
#include<iostream.h>
class test
{
public:
void add(int i,int j)
{
cout<<"\n the integer i,j is"<<i+j;
}
void add(float k,float l)
{
cout<<"\n the float k,l is"<<k+l;
}
};
void main()
{
int i=10,j=20;
float k=1.0,l=2.0;
test ob1;
ob1.add(i,j);
ob2.add(k,l);
}
output:
the integer i,j is 30
the float k,l is 3.0

b. Write a program illustrate function template for power of a number.


c. Write a program to illustrate function template for swapping of two numbers.

EXERCISE-6
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

a. Write a main function to create objects of DISTANCE class. Input two distances and
output the sum.

#include<iostream.h>
#include<conio.h>
class distance
{
private: //data members
int feet;
float inch;
public:
distance(); //constructors without arguements
distance(int,float); //constructors with arguements
void setdistance();
void displaydistance();
void distancesum(distance d); //object
void distancesum(distance e,distance f);
}; //end of the class
distance::distance()
{
inch=0;
feet=0;
}
distance::distance(int n,float m)
{
feet=n;
inch=m;
}
void distance::setdistance()
{
cout<<"enter distance in feet";
cin>>feet;
cout<<"enter distance in inch";
cin>>inch;
}
void distance::displaydistance()
{
cout<<"\n feet:"<<feet;
cout<<"\n inch:"<<inch;
}
void distance::distancesum( distance d)
{
cout<<"feet="<<d.feet+feet;
cout<<"inch="<<d.inch+inch;
}
Void distance::distancesum(distance e,distance f)
{
Cout<<”feet=”<<e.feet+f.feet;
Cout<<”inch=”<<e.inch+f.inch;
}
int main()
{
clrscr();
distance d1,d2(10,2.3),d3(2, 4);
d1.setdistance();
d2.displaydistance();
d1.distancesum(d2);
d1.distancesum(d2,d3);
getch();
return(0);
}
Output:

b. Write a C++ Program to illustrate the use of Constructors and Destructors (use the
above program.)
#include<iostream.h>
class distance
{
public:
distance()
{
cout<<"constructor";
}
~distance()
{
cout<<"destructor";
}
};
void main()
{
distance ob1;
}

output:

constructor
destructor

d. Write a C++ program demonstrating a BankAccount with necessary methods and variables

#include<iostream.h>
#include<conio.h>
class customer //class name
{
private:
char name[20]; //data members
int acno;
float accbal;
public:
void addcust() //function definition
{
cout<<"\n enter customer name"; //member functions
cin>>name;
cout<<"\n enter account no";
cin>>acno;
cout<<"\n enter accountbalance";
cin>>accbal;
}
void deposit(float bal)
{
accbal+=bal;
}
void withdraw(float bal)
{
if(accbal>=bal)
accbal=accbal-bal;
else
cout<<"insufficient";
}
void showdata()
{
cout<<"\n the customer name is:"<<name;
cout<<"\n the account no is:"<<acno;
cout<<"\n the account balance is:"<<accbal;
}
}; //end of class
int main()
{
clrscr();
customer c1; //object creation
c1.addcust();
c1.deposit(1000);
c1.showdata(); //function calling
c1.withdraw(500);
c1.showdata();
getch();
return(0);
}

Output:

EXERCISE-7
Write a program for illustrating Access Specifiers public, private, protected

#include<iostream.h>
#include<conio.h>
Class sowmya
{
Private:
Int pr;
Public:
Int pu;
Void read()
{
Cout<<”enter the value of pr,pu,po:”;
Cin>>pr>>pu>>po;
}
Void show()
{
Cout<<”\n the value of pr ,pu,po:”<<pr<<pu<<po;
}
Protected:
Int po;
};
Class n:public sowmya
{
public:
Int po1;
Void read1()
{
Cout<<enter the value;
Cin>>po1>>po;
}
};
Void main()
{
Clrscr();
Sowmya ob1,ob2;
Ob1.read();
//ob1.pr=4 private variable cannot be accesed outside class
//ob1.po=5 protected cannot be accessed
//Ob1.pu=4; public variable can be accessed
Ob1.show();
Ob2.read1();
Getch();
}

Output:
Enter the value pr,pu,po : 1 2 3
The value is pr,pu,po :123

a. Write a program implementing Friend Function

#include<iostream.h>
Class number
{
int n1;
Public:
number()
{
n1=0;
}
number(int x)
{
n1=x;
cout<<”the n1 value is”<<n1;
}
friend number min(number ob1,number ob2)
{
if(ob1.n1<ob2.n1)
return ob1;
else
return ob2;
}
number max(number d1)
{
if(d1.n1<this->n1)
return *this;
else
return d1;
}
Void show()
{
Cout<<”\n the value of n1 is”<<n1;
}
};
Void main()
{
number ob2;
number ob3(20);
number ob4(40);
ob2=min(ob4,ob3);
ob2.show();
ob2=ob3.max(ob4);
ob2.show();
}

Output:

The n1 value is 20
The n1 value is 40
The value of n1 is 20
The value of n1 is 40

c. Write a Program to illustrate pointer to a class


#include<iostream.h>
#include<conio.h>
Class sowmya
{
Private:
Int pr;
Public:
Int pu;
Void read()
{
Cout<<”enter the value of pr,pu,po:”;
Cin>>pr>>pu>>po;
}
Void show()
{
Cout<<”\n the value of pr ,pu,po:”<<pr<<pu<<po;
}
Protected:
Int po;
};
Void main()
{
Clrscr();
Sowmya *ob1;
Ob1->read();
Ob1->show();
Getch();
}
Output:
Enter the value pr,pu,po : 1 2 3
The value is pr,pu,po :123

EXERCISE-8
a. Write a program to Overload Unary, and Binary Operators as Member Function, and Non
Member Function.
i. Unary operator as member function
ii. Binary operator as non-member function
#include<iostream.h>
#include<conio.h>
class distance
{
private:
int feet;
int inch;
public:
void read();
void show();
friend distance operator+(distance d1,distance d2);
void operator++();
void operator=(distance ob);
};
void distance::read()
{
cout<< "\n enter feet value is:";
cin>>feet;
cout<<"\n enter inch value is:";
cin>>inch;
}
void distance::show()
{
cout<<"\n the feet is:"<< feet;
cout<<"\nthe inch is:"<<inch;
}
distance operator+(distance d1,distance d2)
{
distance temp;
temp.inch=d1.inch+d2.inch;
temp.feet=d1.feet+d2.feet;
return(temp);
}
void distance::operator++()
{
inch++;
feet++;
}
void distance::operator=(distance ob)
{
feet=ob.feet;
inch=ob.inch;
}
void main()
{
clrscr();
distance d1,d2,d3;
d1.read();
d2.read();
cout<<"\n the operator + is" ;
d3=d1+d2;
d3.show();
cout<<"\n the operator++ is";
d1++;
d1.show();
cout<<"\n the operator =is";
d3.operator=(d2);
d3.show();
getch();
}

EXERCISE-9
Write C++ Programs and incorporating various forms of Inheritance

i) Single Inheritance

#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a1;
};
classB:public A
{
protected:
int b1;
public:
void read()
{
cout<<"enter values of a1,b1";
cin>>a1>>b1;
}
void show()
{
cout<<"\n the values of a1,b1 are:"<<a1<<b1;
}
};
void main()
{
clrscr();
B ob1;
ob1.read();
ob1.show();
getch();
}

output:

enter values a1,b1 1 2


the values of a1,b1 are:12

ii) HIERARCHICAL INHERITANCE:-


#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a1;
};
class B:public A
{
protected:
int b1;
public:
void read()
{
cout<<"\n enter values :",
cin>>a1>>b1;
}
void show()
{
cout<<"\n the values is"<<a1<<,<<b1;
}
};
class C:public A
{
public:
int c1;
void read()
{
cout<<"\n enter the values:";
cin>>a1>>c1;
}
void show()
{
cout<<"\n the values is:"<<a1<<,<<c1;
}
};
void main()
{
clrscr();
C ob1;
B ob2;
ob1.read();
ob1.show();
ob2.read();
ob2.show();
getch();
}
iii) MULTIPLE INHERITANCES
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a1;
};
class B
{
protected:
int b1;
};
class C:public A,public B
{
public:
int c1;
void read()
{
cout<<"\n enter the values";
cin>>a1>>b1>>c1;
}
void show()
{
cout<<"\n the values is a1,b1,c1:"<<a1<<b1<<c1;
}
};
void main()
{
clrscr();
C ob1;
ob1.read();
ob1.show();
getch();
}

IV) MULTI-LEVEL INHERITANCE:


#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a1;
};
class B:public A
{
protected:
int b1;
};
class C:public B
{
public:
int c1;
void read()
{
cout<<"\n enter values";
cin>>a1>>b1>>c1;
}
void show()
{
cout<<"\n the values is:"<<a1<<b1<<c1;
}
};
void main()
{
clrscr();
C ob1;
ob1.read();
ob1.show();
getch();
}

V) HYBRID INHERITANCE:
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a1;
};
class B:public A
{
protected:
int b1;
};
class C
{
protected:
int c1;
};
class D:public B,public C
{
private:
int d1;
public:
void read()
{
cout<<"\n enter the values";
cin>>a1>>b1>>c1>>d1;
}
void show()
{
cout<<"\n the values is"<<a1<<b1<<c1<<d1;
}
};
void main()
{
clrscr();
D ob1;
ob1.read();
ob1.show();
}

b. program for VIRUTAL BASE class or MULTIPATH


#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a1;
};
class B:public virtual A
{
protected:
int b1;
};
class C:public virtual A
{
protected:
int c1;
};
class D:public B,public C
{
protected:
int d1;
public:
void read()
{
cout<<"\n enter the values:";
cin>>a1>>b1>>c1>>d1;
}
void show()
{
cout<<"\n the values is"<<a1<<b1<<c1<<d1;
}
};
void main()
{
clrscr();
D ob1;
ob1.read();
ob1.show();
getch();
}

EXERCISE-10
a. Write a Program in C++ to illustrate the order of execution of constructors and
destructors in inheritance.
#include<iostream.h>
Class A
{
Public:
A()
{
Cout<<”\n this is class a constructor”;
}
~A()
{
Cout<<”\n this is class a destructor”;
}
};
Class B:public A
{
Public:
B()
{
Cout<<”\n this is class b constructor”;
}
~B()
{
Cout<<”\n this is class b destructor”;
}
};
Class C:public B
{
Public:
C()
{
Cout<<”\n this is class c constructor”;
}
~C()
{
Cout<<”\n this is class c destructor”;
}
};
Void main()
{
C ob1;
}
b. Write a Program to show how constructors are invoked in derived class

#include<iostream.h>
class A
{
int a1;
public:
A()
{
a1=0;
cout<<"\n base constructor a1"<<a1;
}
A(int x)
{
a1=x;
cout<<"\n parameterised constructor a1"<<a1;
}
};
class B:public A
{
int b1;
public:
B(int x,int y):A(y)
{
b1=x;
cout<<"\n parameterised constructor b1"<<b1;
}
};
void main()
{
B ob1(50,60);
}
output:
parameterised constructor b1=50
parameterised constructor a1=60
EXERCISE-11

a. Write a program to illustrate runtime polymorphism

#include<iostream.h>
#include<conio.h>
class A
{
protected:
int i;
public:
virtual void show()
{
i=5;
cout<<"\n the value of i is :"<<i;
}
};
class B:public A
{
int j;
public:
void show()
{
j=2;
cout<<"\n the value of j is:"<<j;
}
};
void main()
{
clrscr();
A ob1,*ptr;
B ob2;
ptr=&ob1;
ptr->show();
ptr=&ob2;
ptr->show();
getch();
}
c. Write a program illustrates pure virtual function and calculate the area of different
shapes by using abstract class.

#include<iostream.h>
#include<conio.h>
class shape
{
protected:
int height,width;
public:
void read()
{
cout<<"\n enter height and width";
cin>>height>>width;
}
virtual void area()=0;
};
class rectangle:public shape
{
public:
void area()
{
cout<<"\n the area of rectangle is"<<width*height;
}
};
class triangle:public shape
{
public:
void area()
{
cout<<"\n the area of triangle is"<<0.5*width*height;
}
};
void main()
{
clrscr();
rectangle ob1;
triangle ob2;
ob1.read();
ob1.area();
ob2.read();
ob2.area();
getch();
}
EXERCISE-12
a. Write a C++ Program to illustrate template class
b. Write a Program to illustrate class templates with multiple parameters
c. Write a Program to illustrate member function templates
EXERCISE-13
a. Write a Program for Exception Handling Divide by zero

#include<iostream>
using namespace std;
void main()
{
int x,y;
char i;
cout<<"enter the x, y vaule";
cin>>x>>y;
try
{
if(y==0)
throw(0);
else
cout<<"result="<<x/y;
}
catch(int msg)
{
cout<<"divide error";
}
}
output:
enter x,y value 10,0
divide error.

b. Write a Program to rethrow an Exception

#include<iostream>
using namespace std;
void div1(int x,int y)
{
try
{
if(y==0)
throw(0);
else
cout<<"\n division"<<x/y;
}
catch(int msg)
{
cout<<"\n caught in functon";
throw;
}
}
void main()
{
char i;
try
{
div1(20,4);
div1(4,0);
}
catch(int msg)
{
cout<<"\n caught in main";
}
cin>>i;
}

output:
result=5
caught in function
caught in main
Exercise -14 (STL)
Ex 14 a): PROGRAM TO implement list and list operations

#include<iostream>
#include<list>
using namespace std;
void show(list<int> l1)
{
list<int>::iterator i;
cout<<"\n";
for(i=l1.begin();i!=l1.end();i++)
cout<<*i<<" ";
}
void main()
{
list<int> li;
list<int>::iterator it;
int i,j;
for(i=0;i<5;i++)
li.push_back(i+1);
cout<<"list operations:\n";
cout<<"\nafter pushback.";
show(li);
it=li.begin();
li.insert(it,20);
cout<<"\nafter inserting.";
show(li);
li.pop_back();
cout<<"\nafter popback.";
show(li);
li.push_front(10);
cout<<"\nafter pushfront.";
show(li);
li.pop_front();
cout<<"\nafter popfront.";
show(li);
li.sort();
cout<<"\nafter sorting.";
show(li);
li.reverse();
cout<<"\nafter reverse.";
show(li);
cout<<" before erase list size="<<li.size();
li.erase(it=li.begin());
cout<<"\nafter erase.";
show(li);
cout<<" after erase list size="<<li.size();
cin>>j;
}
Output:
Ex 14 b): PROGRAM TO implement vector and vector operations

#include<iostream>
#include<vector>
using namespace std;
void show(vector<int> l1)
{
vector<int>::iterator i;
cout<<"\n";
for(i=l1.begin();i!=l1.end();i++)
cout<<*i<<" ";
}
void main()
{
vector<int> li;
vector<int>::iterator it;
int i,j;

cout<<"vector operations:\n";
for(i=0;i<5;i++)
li.push_back(i+1);
cout<<"\nafter pushback.";
show(li);
it=li.begin();
li.insert(it,20);
cout<<"\nafter inserting.";
show(li);
li.pop_back();
cout<<"\nafter popback.";
show(li);
cout<<" before erase vector size="<<li.size();
li.erase(it=li.begin());
cout<<"\nafter erase.";
show(li);
cout<<" after erase vector size="<<li.size();
cin>>j;
}
Output:
Ex 15 a): Implement deque and deque operations

b) Implement map and map operations

a): program Implement deque and deque operations

#include<iostream>
#include<deque>
using namespace std;
void show(deque<int> dq)
{
deque<int>::iterator i;
cout<<"deque contains\n";
for(i=dq.begin();i!=dq.end();i++)
cout<<*i<<" ";
}

int main ()
{
deque<int> mydeque;
int i;

cout<<"DEQUE OPERATIONS:\n";
cout<<" set some values (from 1 to 10)";
for ( i=1; i<=10; i++)
mydeque.push_back(i);
show(mydeque);

cout<<"\nerase the 6th element:";


mydeque.erase (mydeque.begin()+5);
show(mydeque);

cout<<" \nerase the first 3 elements:\n";


mydeque.erase (mydeque.begin(),mydeque.begin()+3);
show(mydeque);

cin>>i;

return 0;
}
Output:
b) program Implement map and map operations

#include <iostream>
#include <map>

using namespace std;


int main ()
{
int i;
map<int,char *> m;
m[10]="charan";
m[20]="ajay";
m[30]="vinay";
m[15]="bob";
cout<<"\n prints value associated with key 10 is:";
cout <<m.at(10) ;
cout <<"\n"<<m.at(20) ;
m.at(20)="hello";
m[15] = "doodrah";
cout<<"\nenter item code:";
cin>>i;
cout<<"\nitem name is :"<<m[i]<<"\n";
map<int,char *> ::iterator it;
cout<<"displaying map elements:\n";
cout<<"key value\n";
for(it=m.begin();it!=m.end();it++)
cout<<"\n"<<(*it).first<<" "<<(*it).second<<"\n";

cout<<" \nitem at key 15 is:"<< m.at(15);

cin>>i;
}
Output:

You might also like