Anda di halaman 1dari 63

OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE

Exercise – 1 (Basics)
Aim: Write a Simple Program on printing “Hello World” and “Hello Name” where name is
the input from the user

Source Code(Hello.cpp)
#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
cout<<"\nHello World\n";
getch();
return 0;
}
Output
Hello World

Aim: Write a simple program for printing “Hello Name” where name is the input from the
user

Source Code(Name.cpp)
#include <iostream.h>
#include <conio.h>
int main()
{
char name[25];
clrscr();
cout<<"\nEnter your name:";
cin>>name;
cout<<"Hello "<<name;
getch();
return 0;
}

Output
Enter your name: Vijay
Hello Vijay

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

Aim: Write a C++ program to check whether the given number is even or odd.
Source Code (even.cpp)
#include <iostream.h>
#include <conio.h>
int main()
{
int n;
clrscr();
cout << "Enter a number: ";
cin >> n;
if(n%2 == 0)

Ch. Vijayananda Ratnam@Dept. Of CSE 1


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
cout << n << " is a positive number";
else
cout << n << " is a negative number.";
getch();
return 0;
}

Output
Enter a number: 13
13 is a positive number

Enter a number: -1
-1 is anegative number

Aim: Write a C++ program to check whether the given number is Armstrong or not.
Source Code (armstrong.cpp)
#include <iostream.h>
#include <conio.h>
int main()
{
int n, temp, rem, sum = 0;
clrscr();
cout << "Enter a positive integer: ";
cin >> n;

temp = n;

while( n != 0)
{
rem = n % 10;
sum = sum +(rem * rem * rem);
n = n / 10;
}

if(temp == sum)
cout << temp << " is an armstrong number";
else
cout << temp << " is not an armstrong number.";
getch();
return 0;
}
Ouput
Enter a positive integer: 153
153 is an armstrong number

Enter a positive integer: 123


123 is an not armstrong number

Exercise – 2 (Expressions Control Flow)

Ch. Vijayananda Ratnam@Dept. Of CSE 2


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Aim: Write a Program that computes the simple interest and compound interest payable on
principal_amount(inRs.) of loan borrowed by the customer from a bank for a give r period of
time (in years) at specific rate of interest. Further determine whether the bank will benefit by
charging simple interest or compound interest.

Source Code (interest.cpp)


#include <iostream.h>
#include <conio.h>
#include<math.h>
int main()
{
clrscr();
float p,t,r;
double si,ci;
cout<<"Enter principle amount:";
cin>>p;
cout<<"Enter rate of interest:";
cin>>r;
cout<<"Enter period of time:";
cin>>t;
si =(p*t*r)/100;
ci=p*pow((1+r/100),t) - p;
cout<<"\nSimple interest:"<<si;
cout<<"\nCompound interest:"<<ci;
if(si>ci)
cout<<"\nBank will benefit by Simple interest";
else
cout<<"\nBank will benefit by Compound interest";
getch();
return 0;
}
Output
Enter principle amount: 1000
Enter rate of interest: 10
Enter period of time: 5
Simple interest: 500
Compound interest: 610.51
Bank will benefit by Compound interest

Aim: Write a Program to calculate the fare for the passengers traveling 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.
Distance (in KMS) Fare (per KM)
0 – 20 65 paisa
21 – 40 75 paisa
41 – 60 78 paisa
61 – 80 80 paisa
81 – 100 95 paisa
101 and above 1.05 paisa

Ch. Vijayananda Ratnam@Dept. Of CSE 3


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Source Code ( fare.cpp )
#include <iostream.h>
#include <conio.h>
#include<math.h>
int main()
{
clrscr();
int d;
float fare;
cout<<"What distance will you travel(in kms):";
cin>>d;
if(d <= 20)
cout<<"Fare:"<<d*.65;
else if(d >=21 && d <= 40)
cout<<"Fare:"<<d*.75;
else if(d >= 41 && d <= 60)
cout<<"Fare:"<<d*.78;
else if(d >= 61 && d <= 80)
cout<<"Fare:"<<d*.80;
else if(d >=81 && d <= 100)
cout<<"Fare:"<<d*.95;
else
cout<<"Fare:"<<d*1.05;
getch();
return 0;
}

Output
What distance will you travel(in kms): 30
Fare: 22.5

Exercise – 3 (Variables, Scope, Allocation)


Aim: Write a program to implement call by value and call by reference using reference
variable.

Source Code ( ppass.cpp )


#include<iostream.h>
#include<conio.h>
int cv(int a) // call by value
{
return a++;
}

int cr(int &a) // call by reference


{
return a++;
}
int main()
{
int a=0;

Ch. Vijayananda Ratnam@Dept. Of CSE 4


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
clrscr();
cout<<"Enter value of a:";
cin>>a;
cout<<"-------------------------------------";
cout<<"\nValue of 'a' before cv() calling::";
cout<<"a="<<a;
cout<<"\nValue of 'a' after cv() called ::";
cv(a);
cout<<"a="<<a;
cout<<"\n-------------------------------------";
cout<<"\nValue of 'a' before cr() calling::";
cout<<"a="<<a;
cout<<"\nValue of 'a' after cr() called::";
cr(a);
cout<<"a="<<a;
cout<<"\n-------------------------------------";
getch();
return 0;
}

Output
Enter value of a: 5
-----------------------------------------
Value of 'a' before cv() calling::5
Value of 'a' after cv() called ::5
-----------------------------------------
Value of 'a' before cr() calling::5
Value of 'a' after cr() called::6
-----------------------------------------
Aim: Write a program to illustrate scope resolution operator.
Source Code ( ppass.cpp )
#include<iostream.h>
#include<conio.h>
int a=20; //Global variable
int main()
{
int a=10; //Local variable
clrscr();
cout<<"\nLocal Variable a="<<a;
cout<<"\nGlobal Variable ::a="<<::a;
getch();
return 0;
}

Output
Local Variable a=10
Global Variable ::a= 20

Aim: Write a program to illustrate new and delete Operators. (Dynamic Memory Allocation)
Source Code ( ne w_del.cpp )
#include<iostream.h>

Ch. Vijayananda Ratnam@Dept. Of CSE 5


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
#include<conio.h>
int main()
{
int i;
int *pv = new int[3];
clrscr();
for(i=1;i<=3;i++)
{
cout<<"Enter a number:";
cin>>*pv;
pv++;
}
pv = pv-3;
cout<<"\nEntered numbers are:";
for(i=0;i<3;i++)
{
cout<<"\n"<<*pv<<"\t"<<(unsigned)pv;
pv++;
}
pv = pv-3;
delete pv;
getch();
return 0;
}
void add(int x,int y)
{
cout<<x+y;
}
void add(int a)
{
cout<<a+3;
}

Output
Enter a number: 10
Enter a number: 20
Enter a number: 30
Entered numbers are:
10 3798
20 3800
30 3802

Aim: Write a program to illustrate Storage classes


Source Code (storage.cpp)
#include<iostream.h>
#include<conio.h>
int g; //global variable, initially holds 0
void test()
{
static int s; //static variable, initially holds 0
register int r=0; //register variable, initially holds garbage value

Ch. Vijayananda Ratnam@Dept. Of CSE 6


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
cout<<"\nInside test()\n------------";
cout<<"\nGlobal variable g = "<<g;
cout<<"\nStatic variable s = "<<s;
cout<<"\nRegister variable r = "<<r;
s++; g++; r++;
}

int main()
{
clrscr();
int a=17; //automatic variable, initially holds garbage value
test();
cout<<"\nInside main\n-----------";
cout<<"\nLocal variable a = "<<a;
cout<<"\nGlobal variable g = "<<g;
test();
getch();
return 0;
}
Output
Inside test()
-----------------
Global variable g = 0
Static variable s = 0
Register variable r = 0

Inside main
--------------------";
Local variable a = 17
Global variable g = 1

Inside test()
-----------------
Global variable g = 1
Static variable s = 1
Register variable r = 0

Aim: Write a program to illustrate Enumerations


Source Code (enum.cpp)
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
enum day {x,y,z=1,a};
cout<<"\nx="<<x;
cout<<"\ny="<<y;
cout<<"\nz="<<z;
cout<<"\na="<<a;
getch();
return 0;

Ch. Vijayananda Ratnam@Dept. Of CSE 7


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
}

Output
x= 0
y=1
z=1
a=2

Exercises –4 (Functions)
Aim: Write a program illustrating Inline Functions
Source Code(inline.cpp)
#include <iostream.h>
#include <conio.h>

inline int Max(int x, int y)


{
return (x > y)? x : y;
}

int main()
{
clrscr();
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
getch();
return 0;
}

Output
Max (20,10): 20
Max (0,200): 200

Aim: Write a program illustrates function overloading. Write 2 overloading functions for
power.

Source Code(fo_powe r.cpp)


#include<iostream.h>
#include<conio.h>

int power(int n, int p) //Integer version


{
int res=1;
for(int i=1;i<=p;i++)
res=res*n;
return res;
}

double power(double n, int p) //Double version


{ double res=1.0;
for(int i=1;i<=p;i++)
res= res*n;

Ch. Vijayananda Ratnam@Dept. Of CSE 8


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
return res;
}

int main()
{
clrscr();
cout<<"\nPower(2,3):"<<power(2,3);
cout<<"\nPower(2.5,2):"<<power(2.5,2);
getch();
return 0;
}

Output:
Power(2,3): 8
Power(2.5,2): 6.25

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

Source Code (default.cpp)


#include <iostream.h>
#include <conio.h>

double interest(float p, float t, float r=8.0)


{
return (p*t*r)/100;
}

int main()
{
clrscr();
float p,t,r;
double si;
cout<<"Enter principle amount:";
cin>>p;
cout<<"Enter period of time:";
cin>>t;
si =interest(p,t)
cout<<"\nSimple interest(1000,5,8.0):"<<si;
si =interest(p,t,5.0);
cout<<"\nSimple interest(1000,5,5.0):"<<si;
getch();
return 0;
}

Output
Enter principle amount: 1000
Enter period of time: 5
Simple interest(1000,5,8.0): 400
Simple interest(1000,5,5.0): 250

Ch. Vijayananda Ratnam@Dept. Of CSE 9


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Exercise -5 (Functions –Exercise Continued)
Aim: Write a program to illustrate function overloading. Write 2 overloading functions for
adding two numbers

Source Code (fo_add.cpp)


#include<iostream.h>
#include<conio.h>

void add(int a, int b) //Integer version


{
cout<<"Sum = "<<a+b;
}

void add(double a, double b) //Double version


{
cout<<"Sum = "<<a+b;
}

int main()
{
clrscr();
int x,y;
double d,e;
cout<<"Enter two integers:";
cin>>x>>y;
add(x,y);
cout<<"\nEnter two real values:";
cin>>d>>e;
add(d,e);
getch();
return 0;
}

Output
Enter two integers:2 6
Sum =8
Enter two real values: 5.6 .7
Sum = 6.3

Aim: Write a program illustrate function template for power of a number.


Source Code(tem_pow.cpp)
#include<iostream.h>
#include<conio.h>

template <class T> //Template definition


T power(T n, T p)
{
T res=1;
for(int i=1;i<=p;i++)
res=res*n;
return res;

Ch. Vijayananda Ratnam@Dept. Of CSE 10


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
}

int main()
{
clrscr();
cout<<"\nPower(4,3):"<<power(4,3);
cout<<"\nPower(3.5,2):"<<power(3.5,2.0);
getch();
return 0;
}

Output
Power(4,3): 64
Power(3.5,2): 12.25

Aim: Write a program to illustrate function template for swapping of two numbers.
Source Code (tem_s wap.cpp)
#include<iostream.h>
#include<conio.h>
template <class T>
void power(T x, T y) //Temlate Definition
{
T temp;
temp = x;
x= y;
y = temp;
cout<<"\nA= "<<x<<"\tB= "<<y;
}

int main()
{
int a,b;
double d1,d2;
clrscr();
cout<<"Enter two integers:";
cin>>a>>b;
cout<<"Before swapping....";
cout<<"\nA= "<<a<<"\tB= "<<b;
cout<<"\nAfter Swapping....";
power(a,b);
cout<<"\nEnter two real values:";
cin>>d1>>d2;
cout<<"Before swapping....";
cout<<"\nA= "<<d1<<"\tB= "<<d2;
cout<<"\nAfter Swapping....";
power(d1,d2);
getch();
return 0;
}

Ch. Vijayananda Ratnam@Dept. Of CSE 11


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Output
Enter two integers:2 6
Before swapping...
A= 2 B= 6
After Swapping....
A= 6 B= 2
Enter two real values: 1.5 4.5
Before swapping...
A= 1.5 B= 4.5
After Swapping....
A= 4.5 B= 1.5

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

Aim: Write a main function to create objects of DISTANCE class. Input two distances and
output the sum.
Sorce Code (distance.cpp)
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inch;
public:
void input()
{
cout<<"Enter FEET : ";
cin>>feet;
cout<<"Enter INCH : ";
cin>>inch;
}
void show()
{
cout<<feet<<" FEET and "<<inch<<" INCH"<<endl;
}
void add(Distance d1,Distance d2)
{
inch=d1.inch+d2.inch;
feet=inch/12;
feet+=d1.feet+d2.feet;
inch=inch%12;
}
};

Ch. Vijayananda Ratnam@Dept. Of CSE 12


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
int main()
{
clrscr();
Distance d1;
cout<<"Distance d1"<<endl<<"---------------"<<endl;
d1.input();
Distance d2;
cout<<"Distance d2"<<endl<<"---------------"<<endl;
d2.input();
d1.add(d1,d2);
cout<<endl<<"The sum of d1 and d2 is : ";
d1.show();
getch();
return 0;
}

Output
Distance d1
---------------
Enter FEET : 5
Enter INCH : 6

Distance d2
---------------
Enter FEET : 6
Enter INCH : 7

The sum of d1 and d2 is : 12 FEET and 1 INCH

Aim: Write a C++ Program to illustrate the use of Constructors and Destructors (use the
above program.)
Sorce Code (dis_impr.cpp)
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inch;
public:
Distance( ) //Default constructor
{
feet=0;
inch=0;
}
Distance(int a,int b) //Two argument constructor

{
feet=a;
inch=b;
}

Ch. Vijayananda Ratnam@Dept. Of CSE 13


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
~Distance(){} //Destructor
void show()
{
cout<<feet<<" FEET and "<<inch<<" INCH"<<endl;
}
void add(Distance d1,Distance d2)
{
inch=d1.inch+d2.inch;
feet=inch/12;
feet+=d1.feet+d2.feet;
inch=inch%12;
}
};

int main()
{
clrscr();
Distance d1(10,4);
Distance d2(10,2);
d1.add(d1,d2);
cout<<endl<<"The sum of d1 and d2 is : ";
d1.show();
getch();
}

Output
The sum of d1 and d2 is : 20 FEET and 6 INCH

Aim: Write a C++ program demonstrating a Bank Account with necessary methods and
variables
Note: Do not use nume ric keypad.

Souce Code(bank.cpp)
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include<stdlib.h>
#include<string.h>

class bank
{
char name[20];
int acno;
char actype[20];
float bal;
public :
bank()
{
strcpy(name,"");
acno=0;
strcpy(actype,"");

Ch. Vijayananda Ratnam@Dept. Of CSE 14


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
bal=0.0;
}
void opbal(void);
void deposit(void);
void withdraw(void);
void display(void);
};

void bank :: opbal(void)


{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter A/c no.:";
cin>>acno;
cout<<"Enter A/c Type:";
cin>>actype;
cout<<"Enter Opening Balance:";
cin>>bal;
}

void bank :: deposit(void)


{
cout<<"Enter Deposit amount:";
float deposit=0.0;
cin>>deposit;
cout<<setprecision(2);
bal= bal + deposit;
cout<<"Total balance = "<<bal;
// bal=deposit;
}

void bank :: withdraw(void)


{
int withdraw;
cout<<"Balance Amount = "<<bal;
cout<<"\nEnter Withdraw Amount:";
cin>>withdraw;
if(bal<withdraw || bal == 0)
{
cout<<"Insufficient Funds";
}
else
{
bal=bal-withdraw;
cout<<"After Withdraw Balance is "<<bal;
}
}

void bank :: display(void)


{
cout<<"ACCOUNT DETAILS\n----------------"<<endl;

Ch. Vijayananda Ratnam@Dept. Of CSE 15


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
cout<<"Name : "<<name<<endl;
cout<<"A/c. No.: "<<acno<<endl;
cout<<"A/c Type: "<<actype<<endl;
cout<<"Balance : "<<bal<<endl;
cout<<"-------------------------------";
}

void main()
{
clrscr();
bank b;
int choice;
cout<<"BANK OF INDIA\n------------";
do
{

cout<<"\n1. New Account\n";


cout<<"2. Deposit\n";
cout<<"3. Withdraw\n";
cout<<"4. View Balance\n";
cout<<"5. Exit\n";
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1: b.opbal();
break;
case 2: b.deposit();
break;
case 3: b.withdraw();
break;
case 4: b.display();
break;
case 5: exit(1);
}
}while(1);
}

Output
BANK OF INDIA
------------------------
1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 1
Enter Name: Vijay
Enter A/c no.: 1001
Enter A/c Type: Savings
Enter Opening Balance:500

Ch. Vijayananda Ratnam@Dept. Of CSE 16


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE

1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 2
Enter Deposit amount: 1500
Total balance = 2000

1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 3
Balance Amount = 2000
Enter Withdraw Amount:1025
After Withdraw Balance is 975

1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 4
ACCOUNT DETAILS
-----------------------------
Name : Vijay
A/c no.: 1001
A/c Type: Savings
Balance: 975

1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 5

Exercise – 7 (Access)
Write a program for illustrating Access Specifiers public, private, protected
a) Write a program implementing Friend Function
Aim: Write a program to illustrate this pointer

Source Code (this.cpp)


#include<iostream.h>
#include<conio.h>
class Number
{
int x;

Ch. Vijayananda Ratnam@Dept. Of CSE 17


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
public:
void input( )
{
cout<< "\nEnter a number:";
cin>>x;
}
void show()
{
cout<< "\nThe Minimum number is :"<<x;
}

Number min( Number n2 )


{
if(n2.x<x)
return n2;
else
return *this;
}
};

int main()
{
clrscr();
Number n,n1,n2;
n1.input();
n2.input();
n= n1.min(n2);
n.show();
return 0;
}

Output
Enter a number: 3
Enter a number: 5
The minimum number is 3.

Aim: Write a Program to illustrate pointer to a class


Source Code(cls_ptr.c)
#include<iostream.h>
#include<conio.h>
class Man
{
public:
char name[25];
int age;
};

int main()
{
Man m = { " Vijayanand ",30};
Man *ptr;

Ch. Vijayananda Ratnam@Dept. Of CSE 18


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
ptr= &m;
clrscr();
cout<< "\nName= "<<m->name;
cout<< "\tAge= "<<m->age;
return 0;
}

Output:
Name=Vijayanand Age=30

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

Exercise -9 (Inheritance)
Aim: Write C++ Programs and incorporating various forms of Inheritance

Aim: Write C++ Program that illustrates single inheritance


Source Code(single_inh.cpp)
#include<iostream.h>
#include<conio.h>
class Emp
{
public:
int eno;
char ename[20],desig[20];
void input()
{
cout<<"Enter employee no:";
cin>>eno;
cout<<"Enter employee name:";
cin>>ename;
cout<<"Enter designation:";
cin>>desig;
}
};

class Salary: public Emp


{
float bp, hra, da, pf, np;
public:
void input1()
{
cout<<"Enter Basic pay:";
cin>>bp;
cout<<"Enter House Rent Allowance: ";
cin>>hra;
cout<<"Enter Dearness Allowance :";

Ch. Vijayananda Ratnam@Dept. Of CSE 19


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
cin>>da;
cout<<"Enter Provident Fund:";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<"\nEmp no: "<<eno
cout<<"\nEmp name: "<<ename;
cout<<"\nDesignation: "<<design;
cout<<"\nBasic pay:"<<bp;
cout<<"\nHRA:"<<hra;
cout<<"\nDA:"<<da;
cout<<"\nPF:"<<pf;
cout<<"\nNet pay:"<<np;
}
};

int main()
{
clrscr();
Salary s;
s.input();
s.input1();
s.calculate();
s.show();
getch();
return 0;
}

Output:
Enter employee number: 1001
Enter employee name: Vijayanand
Enter designation: Manager
Enter basic pay: 25000
Enter House Rent Allowance: 2500
Enter Dearness Allowance: 5000
Enter Provident Fund: 1200

Emp no: 1001


Emp name: Vijayanand
Designation: Manager
Basic pay: 25000
HRA: 2500
DA: 5000
PF: 1200
Net pay: 31300

Ch. Vijayananda Ratnam@Dept. Of CSE 20


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Aim: Write C++ Program that illustrates hierarchical inheritance
Source Code(hie r_inh.cpp)
#include<iostream.h>
#include<conio.h>
class Vehicle
{
public:
Vehicle()
{
cout<<"\nIt is motor vehicle";
}
};
class TwoWheelers : public Vehicle
{
public:
TwoWheelers()
{
cout<<"\nIt has two wheels";
}
void speed()
{
cout<<"\nSpeed: 80 kmph";
}
};
class ThreeWheelers : public Vehicle
{
public:
ThreeWheelers()
{
cout<<"\nIt has three wheels";
}
void speed()
{
cout<<"\nSpeed: 60 kmph";
}
};
class FourWheelers : public Vehicle
{
public:
FourWheelers()
{
cout<<"\nIt has four wheels";
}
void speed()
{
cout<<"\nSpeed: 120 kmph";
}
};

int main( )
{

Ch. Vijayananda Ratnam@Dept. Of CSE 21


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
clrscr();
TwoWheelers two;
two.speed();
cout<<"\n---------------------";
ThreeWheelers three;
three.speed();
cout<<"\n---------------------";
FourWheelers four;
four.speed();
getch();
return 0;
}

Output
It is motor vehicle
It has two wheels
Speed: 80 kmph";
---------------------
It is motor vehicle
It has three wheels
Speed: 60 kmph";
---------------------
It is motor vehicle
It has four wheels
Speed: 120 kmph";

Aim: Write C++ Program that illustrates multiple inheritance


Source Code(mple_inh.cpp)
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rno,m1,m2;
public:
void input()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two subject marks :";
cin>>m1>>m2;
}
};

class Sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{

Ch. Vijayananda Ratnam@Dept. Of CSE 22


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};

class Report : public Student, public Sports


{
int tot, avg;
public:
void show()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\nRoll No : "<<rno<<"\nTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};

int main()
{
clrscr();
Report r;
r.input();
r.getsm();
r.show();
getch():
return 0;
}

Output:
Enter the roll no: 10
Enter the two marks : 70 90
Enter the sports mark: 60
Roll no : 10
Total : 110
Average: 73

Aim: Write C++ Program that illustrates multi- level inheritance


Source Code (mlevel_inh.cpp)
#include<iostream.h>
#include<conio.h>
class Car
{
public:
Car()
{
cout<<"\nVehicle type: Car";
}
};

class Maruti : public Car

Ch. Vijayananda Ratnam@Dept. Of CSE 23


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
{
public:
Maruti()
{
cout<<"\nComnay: Maruti"; }
}
void speed()
{
cout<<"\nSpeed: 90 kmph";
}
};

class Maruti800 : public Maruti


{
public:
Maruti800( )
{
cout<<"\nModel: Maruti 800");
}
void speed()
{
cout<<"\nSpeed: 120 kmph";
}
};

int main( )
{
clrscr();
Maruti800 m;
m.speed();
getch();
}

Output:
Vehicle type: Car
Company: Maruti
Model: Maruti 800
Speed: 120Kmph

Aim: Write C++ Program that illustrates hybrid inheritance


Source Code (hybrid_inh.cpp)
#include<iostream.h>
#include<conio.h>
class Player
{
protected:
char name[20];
char gender;
int age;
};

Ch. Vijayananda Ratnam@Dept. Of CSE 24


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
class Physique : public Player
{
protedted:
float height, weight;
};
class Location
{
protected:
char city[15];
long int pin;
};
class Game : public Physique, public Location
{
char game[15];
public:
void input()
{
cout<<"\nEnter Player Information";
cout<<"Name: ";
cin>>name;
cout<<"Genger: ";
cin>>gender;
cout<<"Age: ";
cin>>age;
cout<<"Height and Weight: ";
cin>>heigh>>weight;
cout<<"City: ";
cin>>city;
cout<<"Pincode: ";
cin>>pin;
cout<<"Game played: ";
cin>>game;

}
void input()
{
cout<<"\nPlayer Information";
cout<<"\nName: "<<name;
cout<<"\nGenger: "<<gender;
cout<<"\nAge: "<<age;
cout<<"\nHeight<<height;
cout<<"\nWeight: "<<weight;
cout<<"\nCity: "<<city;
cout<<"\nPincode: "<<pin;
cout<<"\nGame: "<<game;
}
};
int main( )
{
clrscr();
Game g;

Ch. Vijayananda Ratnam@Dept. Of CSE 25


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
g.input();
g.show();
return 0;
}

Output
Enter Player Information
Name: Azar
Genger: M
Age: 38
Height and Weight: 5.8 70
City: Hyderabad
Pincode: 522183
Game played: Cricket

Player Information
Name: Azar
Genger: M
Age: 38
Height and Weight: 5.8 70
City: Hyderabad
Pincode: 522183
Game played: Cricket

Aim: Write a program to show Virtual Base Class


Source Code (virtual.cpp)
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a1;
};

class B: public virtual A


{
protected:
int a2;
};

class C: public virtual A


{
protected:
int a3;
};

class D :public B, public C


{
int a4;
public:
void input()

Ch. Vijayananda Ratnam@Dept. Of CSE 26


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
{
cout<<"Enter a1,a2,a3 and a4 values: ";
cin>> a1>>a2>>a3>>a4;
}
void show()
{
cout<<"a1="<<a1<<"\na2="a2;
cout<<"\na3="<<a3<<"\na4="<<a4;
}
};

int main()
{
D d;
d.input();
d.show();
return 0;
}

Output
Enter a1, a2, a3 and a4 values: 10 20 30 40
a1=10
a2=20
a3=30
a4=40

Exercise-10 (Inheritance –Continued)


Aim: Write a Program in C++ to illustrate the order of execution of constructors and
destructors in inheritance
Source code(con_des.cpp)
#include<iostream.h>
#include<conio.h>
class A
{
public:
A( )
{
cout<<"\n Class A constructor called";
}
~A( )
{
cout<<"\nClass A destructor called";
}
};

class B : public A
{
public:
B( )
{
cout<<"\n Class B constructor called";

Ch. Vijayananda Ratnam@Dept. Of CSE 27


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
}
~B( )
{
cout<<"\nClass B destructor called";
}
};

class C : public B
{
public:
C( )
{
cout<<"\n Class C constructor called";
}
~C( )
{
cout<<"\nClass C destructor called";
}
};

int main()
{
C c;
return 0;
}

Output
Class A constructor called
Class B constructor called
Class C constructor called
Class C destructor called
Class B destructor called
Class A destructor called

Exercise -11 (Polymorphism)


Aim: Write a program to illustrate runtime polymorphism
Source Code(runtime.cpp)
#include<iostream.h>
#include<conio.h>
class A
{
int a;
public:
A( ) { a = 1; }
virtual void show( ) //base class member function
{ cout <<"\na="<<a; }
};

class B: public A
{
int b;

Ch. Vijayananda Ratnam@Dept. Of CSE 28


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
public:
B( ) { b = 2; }
void show( ) //derived class member function
{ cout <<"\nb="<<b; }
};

int main()
{
clrscr();
A *bptr;
A a;
bptr= &a;
bptr->show();

B b;
bptr= &b;
bptr->show();
getch();
return 0;
}

Output:
a=1
b=2

Aim: 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 // Abstract class


{
protected:
float l;
public:
void getData()
{
cin >> l;
}
virtual float area() = 0; // Pure virtual Function
};

class Square : public Shape


{
public:
float area()
{ return l*l; }
};

class Circle : public Shape


{

Ch. Vijayananda Ratnam@Dept. Of CSE 29


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
public:
float area()
{ return 3.14*l*l; }
};

int main()
{
Square s;
Circle c;
cout << "Enter the value of side: ";
s.getData();
cout<<"Area of square: " << s.area();
cout<<"\nEnter radius of a circle: ";
c.getData();
cout << "Area of circle: " << c.area();
return 0;
}

Output
Enter the value of side: 4
Area of square: 16
Enter radius of a circle: 2
Area of circle: 12.56

Exercise -12 (Templates)


Aim: Write a C++ Program to illustrate template class
Source Code (template.cpp)
#include <iostream.h>
#include <conio.h>
template<class T>
class Data
{
public: Data(T c)
{
cout<<"\n Value = "<<c;
cout<<"\tSize = "<<sizeof(c)<<" byte(s)";
}
};
int main()
{
clrscr();
Data <char> c('A');
Data <int> i(100);
Data <double> f(23.45);
getch();
return 0;
}

Output
Value = A Size = 1 byte(s)
Value = 100 Size = 2 byte(s)

Ch. Vijayananda Ratnam@Dept. Of CSE 30


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Value = 23.45 Size = 8 byte(s)

b) Write a Program to illustrate class templates with multiple parameters


#include <iostream.h>
#include<conio.h>
template <class T>
class Calculator
{
private:
T num1, num2;
public:
Calculator(T n1, T n2)
{
num1 = n1;
num2 = n2;
}
void show()
{
cout << "\nNumbers are: " << num1 << " and " << num2;
cout << "\nAddition is: " << add();
cout << "\nSubtraction is: " << subtract();
cout << "\nProduct is: " << multiply();
cout << "\nDivision is: " << divide();
}

T add() { return num1 + num2; }


T subtract() { return num1 - num2; }
T multiply() { return num1 * num2; }
T divide() { return num1 / num2; }
};

int main()
{
clrscr();
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
cout << "Integer results\n-----------------------";
intCalc.show();
cout << endl << "\nFloat results\n---------------------";
floatCalc.show();
getch();
return 0;
}

Output
Integer results
---------------------------------
Numbers are: 2 and 1
Addition is: 3
Subtraction is: 1

Ch. Vijayananda Ratnam@Dept. Of CSE 31


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Product is: 2
Division is: 2

Float results
---------------------------------
Numbers are: 2.4 and 1.2
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2

Aim: Write a Program to illustrate member function templates


Source Code(fun_temp.cpp)
# include <iostream.h>
# include <conio.h>
template <class T>
T Max (T a, T b)
{
return a > b ? a:b;
}
int main ()
{
int i = 39;
int j = 20;
clrscr();
cout << "Max("<<i<<","<<j<<"): " << Max(i, j) << endl;
double f1 = 13.5;
double f2 = 20.7;
cout << "Max("<<f1<<","<<f2<<"): " << Max(f1, f2) << endl;
char c1 = 'Z';
char c2 = 'A';
cout << "Max("<<c1<<","<<c2<<"): " << Max(c1, c2) << endl;
getch();
return 0;
}

Output
Max (39, 20): 39
Max (13.5, 20.7): 20.7
Max (Z, A): Z

Exercise -13 (Exception Handling)


Aim: Write a Program for Exception Handling Divide by zero
Source Code(Excep.cpp)
# include <iostream>
using namespace std;
int main( )
{
int a,b;
cout<<"Enter values of a and b: ";
cin>>a>>b;

Ch. Vijayananda Ratnam@Dept. Of CSE 32


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
try
{
if(b!=0)
cout<<"Result of (a/b):"<<a/b<<endl;
else
throw b;
}
catch(int e)
{
cout<<"Divide by zero error due to b = "<<e<<endl;
}
return 0;
}

Output
Enter values of a and b: 4 2
Result of (a/b): 2

Enter values of a and b: 4 0


Divide by zero error due to b = 0

b). Write a Program to rethrow an Exception

Exercise -14 (STL)


Aim: Write a Program to implement List and List Operations
#include <iostream>
#include <list>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
int myints[] = {5, 6, 3, 2, 7};
list<int> l, l1 (myints, myints + sizeof(myints) / sizeof(int));
list<int>::iterator it;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"List Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
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;

Ch. Vijayananda Ratnam@Dept. Of CSE 33


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
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.Sort Forward List"<<endl;
cout<<"13.Merge Sorted Lists"<<endl;
cout<<"14.Display Forward List"<<endl;
cout<<"15.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted at the front: ";
cin>>item;
l.push_front(item);
break;
case 2:
cout<<"Enter value to be inserted at the end: ";
cin>>item;
l.push_back(item);
break;
case 3:
item = l.front();
l.pop_front();
cout<<"Element "<<item<<" deleted"<<endl;
break;
case 4:
item = l.back();
l.pop_back();
cout<<"Element "<<item<<" deleted"<<endl;
break;
case 5:
cout<<"Front Element of the List: ";
cout<<l.front()<<endl;
break;
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>>item;
if (item <= l.size())
l.resize(item);
else
l.resize(item, 0);
break;

Ch. Vijayananda Ratnam@Dept. Of CSE 34


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
case 9:
cout<<"Enter element to be deleted: ";
cin>>item;
l.remove(item);
break;
case 10:
l.unique();
cout<<"Duplicate Items Deleted"<<endl;
break;
case 11:
l.reverse();
cout<<"List reversed"<<endl;
break;
case 12:
l.sort();
cout<<"List Sorted"<<endl;
break;
case 13:
l1.sort();
l.sort();
l.merge(l1);
cout<<"Lists Merged after sorting"<<endl;
break;
case 14:
cout<<"Elements of the List: ";
for (it = l.begin(); it != l.end(); it++)
cout<<*it<<" ";
cout<<endl;
break;
case 15:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

Output
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List

Ch. Vijayananda Ratnam@Dept. Of CSE 35


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 5
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 6
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 7
---------------------
List Implementation in Stl

Ch. Vijayananda Ratnam@Dept. Of CSE 36


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 8
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 2
Enter value to be inserted at the end: 4
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values

Ch. Vijayananda Ratnam@Dept. Of CSE 37


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 2
Enter value to be inserted at the end: 3
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 2
Enter value to be inserted at the end: 2
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 7
Size of the List: 7
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front

Ch. Vijayananda Ratnam@Dept. Of CSE 38


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List: 8 7 6 5 4 3 2
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 5
Front Element of the List: 8
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List

Ch. Vijayananda Ratnam@Dept. Of CSE 39


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 6
Last Element of the List: 2
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 9
Enter element to be deleted: 7
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List: 8 6 5 4 3 2
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front

Ch. Vijayananda Ratnam@Dept. Of CSE 40


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 1
Enter value to be inserted at the front: 8
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 2
Enter value to be inserted at the end: 2
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List

Ch. Vijayananda Ratnam@Dept. Of CSE 41


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
15.Exit
Enter your Choice: 14
Elements of the List: 8 8 6 5 4 3 2 2
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 10
Duplicate Items Deleted
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List: 8 6 5 4 3 2
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List

Ch. Vijayananda Ratnam@Dept. Of CSE 42


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 12
List Sorted
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List: 2 3 4 5 6 8
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 13

Ch. Vijayananda Ratnam@Dept. Of CSE 43


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Lists Merged after sorting
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List: 2 2 3 3 4 5 5 6 6 7 8
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 9
Enter element to be deleted: 7
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List

Ch. Vijayananda Ratnam@Dept. Of CSE 44


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List: 2 2 3 3 4 5 5 6 6 8
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 11
List reversed
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List: 8 6 6 5 5 4 3 3 2 2
---------------------

Ch. Vijayananda Ratnam@Dept. Of CSE 45


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 10
Duplicate Items Deleted
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 14
Elements of the List: 8 6 5 4 3 2
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
9.Remove Elements with Specific Values

Ch. Vijayananda Ratnam@Dept. Of CSE 46


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
10.Remove Duplicate Values
11.Reverse the order of elements
12.Sort Forward List
13.Merge Sorted Lists
14.Display Forward List
15.Exit
Enter your Choice: 15
------------------

Aim: Write a program to implement Vector and its operations


#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
vector<int> ss;
vector<int>::iterator it;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Vector Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
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;
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: ";

Ch. Vijayananda Ratnam@Dept. Of CSE 47


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
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;
case 6:
ss.clear();
cout<<"Vector Cleared"<<endl;
break;
case 7:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

Output
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 4
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit

Ch. Vijayananda Ratnam@Dept. Of CSE 48


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Enter your Choice: 1
Enter value to be inserted: 6
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 3
---------------------
Vector Implementation in Stl

---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 8
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 9
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 2

Ch. Vijayananda Ratnam@Dept. Of CSE 49


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 3
Size of Vector: 6
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 4
Displaying Vector by Index: 4 6 3 8 9 2
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 2
Delete Last Element Inserted:
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 3
Size of Vector: 5
---------------------
Vector Implementation in Stl
---------------------

Ch. Vijayananda Ratnam@Dept. Of CSE 50


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 5
Displaying Vector by Iterator: 4 6 3 8 9
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 4
Displaying Vector by Index: 4 6 3 8 9
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 6
Vector Cleared
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 3
Size of Vector: 0
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector

Ch. Vijayananda Ratnam@Dept. Of CSE 51


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 7
------------------

Exercise -15 (STL Continued)


Aim : Write a program to implement Deque and its opeations
#include <iostream>
#include <deque>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
deque<int> dq;
deque<int>::iterator it;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Deque Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element at the End"<<endl;
cout<<"2.Insert Element at the Front"<<endl;
cout<<"3.Delete Element at the End"<<endl;
cout<<"4.Delete Element at the Front"<<endl;
cout<<"5.Front Element at Deque"<<endl;
cout<<"6.Last Element at Deque"<<endl;
cout<<"7.Size of the Deque"<<endl;
cout<<"8.Display Deque"<<endl;
cout<<"9.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted at the end: ";
cin>>item;
dq.push_back(item);
break;
case 2:
cout<<"Enter value to be inserted at the front: ";
cin>>item;
dq.push_front(item);
break;
case 3:
item = dq.back();
dq.pop_back();
cout<<"Element "<<item<<" deleted"<<endl;

Ch. Vijayananda Ratnam@Dept. Of CSE 52


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
break;
case 4:
item = dq.front();
dq.pop_front();
cout<<"Element "<<item<<" deleted"<<endl;
break;
case 5:
cout<<"Front Element of the Deque: ";
cout<<dq.front()<<endl;
break;
case 6:
cout<<"Back Element of the Deque: ";
cout<<dq.back()<<endl;
break;
case 7:
cout<<"Size of the Deque: "<<dq.size()<<endl;
break;
case 8:
cout<<"Elements of Deque: ";
for (it = dq.begin(); it != dq.end(); it++)
cout<<*it<<" ";
cout<<endl;
break;
case 9:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

Output
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 1
Enter value to be inserted at the end: 9
---------------------
Deque Implementation in Stl
---------------------

Ch. Vijayananda Ratnam@Dept. Of CSE 53


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 2
Enter value to be inserted at the front: 8
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 1
Enter value to be inserted at the end: 7
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 1
Enter value to be inserted at the end: 6
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit

Ch. Vijayananda Ratnam@Dept. Of CSE 54


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
Enter your Choice: 1
Enter value to be inserted at the end: 5
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 2
Enter value to be inserted at the front: 10
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 2
Enter value to be inserted at the front: 7
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 7
Size of the Deque: 7
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front

Ch. Vijayananda Ratnam@Dept. Of CSE 55


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 8
Elements of Deque: 7 10 8 9 7 6 5
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 5
Front Element of the Deque: 7
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 6
Back Element of the Deque: 5
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 3
Element 5 deleted
---------------------
Deque Implementation in Stl

Ch. Vijayananda Ratnam@Dept. Of CSE 56


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 8
Elements of Deque: 7 10 8 9 7 6
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 4
Element 7 deleted
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 7
Size of the Deque: 5

---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque

Ch. Vijayananda Ratnam@Dept. Of CSE 57


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
8.Display Deque
9.Exit
Enter your Choice: 8
Elements of Deque: 10 8 9 7 6
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 9
------------------

Aim: Write a program to implement Map and its operations


#include <iostream>
#include <map>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
map<char,int> mp;
map<char, int>::iterator it;
int choice, item;
char s;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Map Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the Map"<<endl;
cout<<"2.Delete Element of the Map"<<endl;
cout<<"3.Size of the Map"<<endl;
cout<<"4.Find Element at a key in Map"<<endl;
cout<<"5.Dislplay by Iterator"<<endl;
cout<<"6.Count Elements at a specific key"<<endl;
cout<<"7.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
cout<<"Enter the key: ";

Ch. Vijayananda Ratnam@Dept. Of CSE 58


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
cin>>s;
mp.insert(pair<char,int>(s ,item));
break;
case 2:
cout<<"Enter the mapped string to be deleted: ";
cin>>s;
mp.erase(s);
break;
case 3:
cout<<"Size of Map: ";
cout<<mp.size()<<endl;
break;
case 4:
cout<<"Enter the key at which value to be found: ";
cin>>s;
if (mp.count(s) != 0)
cout<<mp.find(s)->second<<endl;
else
cout<<"No Element Found"<<endl;
break;
case 5:
cout<<"Displaying Map by Iterator: ";
for (it = mp.begin(); it != mp.end(); it++)
{
cout << (*it).first << ": " << (*it).second << endl;
}
break;
case 6:
cout<<"Enter the key at which number of values to be counted: ";
cin>>s;
cout<<mp.count(s)<<endl;
break;
case 7:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

Output
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map

Ch. Vijayananda Ratnam@Dept. Of CSE 59


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 1
Enter the key: a
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 2
Enter the key: b
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 3
Enter the key: c
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 4
Enter the key: d
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map

Ch. Vijayananda Ratnam@Dept. Of CSE 60


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 1
Enter value to be inserted: 5
Enter the key: e
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 3
Size of Map: 5
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 4
Enter the key at which value to be found: a
1
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 5
Displaying Map by Iterator: a: 1
b: 2
c: 3
d: 4
e: 5
---------------------
Map Implementation in Stl

Ch. Vijayananda Ratnam@Dept. Of CSE 61


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 2
Enter the mapped string to be deleted: c
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 5
Displaying Map by Iterator: a: 1
b: 2
d: 4
e: 5

---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 7
------------------

Installing c++/g++ on Windows

Follow these steps to install g++ (the GNU C++ compiler) for Windows.
1. Pick the drive and a folder in which you want to install g++. I'll assume that it is C:,
but you can choose a different one. If you choose a different drive or a different
folder, you'll need to adapt the directions below accordingly.

2. Download full.exe, an about 14 megabyte executable, to C:\full.exe by right-clicking


on the link. Use Save Link As... or Save Target As... Be sure the browser saves the
file as C:\full.exe.

Ch. Vijayananda Ratnam@Dept. Of CSE 62


OBJECT-ORIENTED PROGRAMMING LAB MANUAL (R16) FOR CSE
3. Run the downloaded executable. This will install g++ (and a lot of other things that
you don't really need) on your hard drive.

4. Locate where the bin folder was created for the g++ installation. On my Windows
machine, it was created in the following path:
C:\cygnus\cygwin-b20\H- i586-cygwin32\bin
5. You now should add it to the PATH environment variable. You do that by following:
Start -> Control Panel -> System -> Advanced -> Environment Variables
At this point you can see the PATH variable either in the User Variables or in
the System Variables. Add the g++ path into the PATH variable. You add it to the end
of the existing value separated by a semicolon (';').

6. Restart your computer. You should now be able to run g++ from a DOS command
prompt window. You will use it from DOS command prompt as explained below. For
example, to compile a file called C:\dir_name\hello.cpp, go to the C:\dir_name folder
and enter
g++ -g hello.cpp -o hello –lm
Note: Use lm only when you include math header file in your program.

7. You'll then be able to run the compiled program by entering hello in the DOS
command prompt window.

For latest compiler, use minGW. To install the this compiler, follow the following link
“https://www.youtube.com/watch?v=lqzuR2USKRM&vl=en”

Ch. Vijayananda Ratnam@Dept. Of CSE 63

Anda mungkin juga menyukai