Object Oriented Programming Lab Manual R
Object Oriented Programming Lab Manual R
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
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)
SOIT, UTD, RGPV, BHOPAL 1
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
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
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.
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
Output
What distance will you travel(in kms): 30
Fare: 22.5
}
int main()
{
int a=0;
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)
SOIT, UTD, RGPV, BHOPAL 6
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
#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
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
cout<<"\nz="<<z;
cout<<"\na="<<a;
getch();
return 0;
Outpu
t 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>
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.
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>
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
Source Code
(fo_add.cpp)
#include<iostream.h>
#include<conio.h>
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
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;
}
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;
}
};
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
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;
SOIT, UTD, RGPV, BHOPAL 16
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
~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 numeric 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;
SOIT, UTD, RGPV, BHOPAL 18
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
strcpy(actype,"");
bal=0.0;
}
void opbal(void);
void deposit(void);
void withdraw(void);
void display(void);
};
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
{
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:
SOIT, UTD, RGPV, BHOPAL 21
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
Savings
Enter Opening Balance:500
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
SOIT, UTD, RGPV, BHOPAL 23
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
{
int x;
public:
void input( )
{
cout<< "\nEnter a
number:"; cin>>x;
}
void show()
{
cout<< "\nThe Minimum number is :"<<x;
}
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.
int main()
{
Man m = { " Vijayanand
",30}; Man *ptr;
SOIT, UTD, RGPV, BHOPAL 25
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
ptr= &m;
clrscr();
cout<< "\nName= "<<m-
>name; cout<< "\tAge= "<<m-
>age; return 0;
}
Output:
Name=Vijayanand Age=30
Exercise -9 (Inheritance)
Aim: Write C++ Programs and incorporating various forms of Inheritance
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
int main( )
{
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";
class Sports
{
protected:
SOIT, UTD, RGPV, BHOPAL 29
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
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
{
public:
Maruti()
{
cout<<"\nComnay: Maruti"; }
}
void speed()
{
cout<<"\nSpeed: 90 kmph";
}
};
int main( )
{
clrscr();
Maruti800 m;
m.speed();
getch();
}
Output:
Vehicle type: Car
Company: Maruti
Model: Maruti 800
Speed: 120K mph
}
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;
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
void input()
{
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=2
0
a3=3
0
a4=4
0
}
};
class B : public A
{
public:
SOIT, UTD, RGPV, BHOPAL 36
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
B( )
{
cout<<"\n Class B constructor called";
}
~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
class B: public A
{
SOIT, UTD, RGPV, BHOPAL 38
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
int b;
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>
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
Output
Value = A Size = 1
byte(s) Value = 100 Size = 2
byte(s)
SOIT, UTD, RGPV, BHOPAL 41
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
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
SOIT, UTD, RGPV, BHOPAL 42
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
Subtraction is: 1
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
Output
Max (39, 20): 39
Max (13.5, 20.7): 20.7
Max (Z, A): Z
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
List"<<endl;
cout<<"8.Resize List"<<endl;
cout<<"9.Remove Elements with Specific Values"<<endl;
cout<<"10.Remove Duplicate Values"<<endl;
cout<<"11.Reverse the order of elements"<<endl;
cout<<"12.Sort Forward List"<<endl;
cout<<"13.Merge Sorted Lists"<<end l;
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;
SOIT, UTD, RGPV, BHOPAL 47
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
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
-
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
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
SOIT, UTD, RGPV, BHOPAL 55
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
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
-
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
-
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
-
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
-
4.Display by Index
5.Dislplay by
Iterator 6.Clear the
Vector 7.Exit
Enter your Choice: 7
-
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
-
SOIT, UTD, RGPV, BHOPAL 66
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
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 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
-
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
SOIT, UTD, RGPV, BHOPAL 71
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
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
-
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
SOIT, UTD, RGPV, BHOPAL 73
OBJECT-ORIENTED PROGRAMMING LAB MANUAL (CB 303) FOR CSBS
-
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
-
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.
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”