0% found this document useful (0 votes)
44 views29 pages

Sanket Oop PDF

The document describes a C++ program that uses inheritance to create classes for publications like books and CDs. The base publication class has title and price attributes, while derived book class adds a pagecount and CD class adds time. Both derived classes display the attributes using a display function.

Uploaded by

nileshlawande09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views29 pages

Sanket Oop PDF

The document describes a C++ program that uses inheritance to create classes for publications like books and CDs. The base publication class has title and price attributes, while derived book class adds a pagecount and CD class adds time. Both derived classes display the attributes using a display function.

Uploaded by

nileshlawande09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Name: Lotake Sanket Bibhishan

Class: SE (COMP)
Roll No.:76
Subject:OOP
_______________________________________________________________________________________
Practical No.: 01
Title: Arithmetic operations on complex numbers using operator overloading. (Using Class Member
function)

#include<iostream>
using namespace std;

class complex
{
int real;
int imaginary;
public:
complex() //Default constructor
{
real = 0;
imaginary = 0;
}

void setdata(int m,int n)


{
real=m;
imaginary=n;
}

void display()
{
cout<<""<<real<<"+"<<imaginary<<"i"<<endl;
}
complex operator+(complex x);
complex operator-(complex y);
complex operator*(complex z);

};

complex complex::operator+(complex x)
{
complex c; c.real=x.real+real;
c.imaginary=x.imaginary+imaginary;
return c;
}

complex complex::operator-(complex y)
{
complex c1; c1.real=real-y.real;
c1.imaginary=imaginary-y.imaginary;

return c1;
}

complex complex::operator*(complex z)
{
complex c2;c2.real=((real)*(z.real))-((imaginary)*(z.imaginary));
c2.imaginary=((real)*(z.imaginary))+((z.real)*(imaginary));

return c2;
}

int main()
{
complex o1,o2,o3; o1.setdata(5,4);
o1.display();
o2.setdata(2,2);
o2.display();

int ch1;
char ch='y';
while(ch=='y')
{
cout<<"\n 1. Addition";
cout<<"\n 2. Substraction";
cout<<"\n 3. Multiplication";
cout<<"\n Enter a choice that you want to perform : ";
cin>>ch1;

switch(ch1)
{
case 1:
o3=o1+o2;
cout<<"\nAddition is : ";
o3.display();
break;
case 2:
o3=o1-o2;
cout<<"\nSubstraction is : ";
o3.display();
break;
case 3:
o3=o1*o2;
cout<<"\nMultiplication is : ";
o3.display();
break;
default:
cout<<"\nInvalid Choice"<<endl;
break;
}
cout<<"\n Whether you want to continue : ";
cin>>ch;
}
}
OUTPUT:

root123@root123-HP-ProDesk-600-G3-SFF:~$ g++ PR1.cpp


root123@root123-HP-ProDesk-600-G3-SFF:~$ ./a.out
5+4i
2+2i

1. Addition
2. Substraction
3. Multiplication
Enter a choice that you want to perform : 1

Addition is : 7+6i

Whether you want to continue : y

1. Addition
2. Substraction
3. Multiplication
Enter a choice that you want to perform : 2

Substraction is : 3+2i

Whether you want to continue : y

1. Addition
2. Substraction
3. Multiplication
Enter a choice that you want to perform : 3

Multiplication is : 2+18i

Whether you want to continue : n


Name: Lotake Sanket Bibhishan
Class: SE (COMP)
Roll No.:76
Subject:OOP

Practical No.: 01
Title : Arithmetic operations on complex numbers using operator overloading (with friend function).

#include<iostream>
using namespace std;

class Complex
{
private:
float real ;
float imaginary ;
public:
Complex()
{
real = 0;
imaginary = 0;
}

Complex(float r, float i)
{
real = r;
imaginary = i;
}

friend Complex operator +(Complex a, Complex b);

void display()
{
cout<<real<<"+"<<imaginary<<"i"<<endl;
}

};

Complex operator +(Complex a, Complex b)


{
Complex temp;
temp.real = a.real + b.real;
temp.imaginary = a.imaginary + b.imaginary;

return temp;
}
int main()
{
Complex c1(2.1,4.0), c2(3.0,4.0), c3;

c3 = operator +(c1,c2);
c1.display();
c2.display();

cout<<"And the sum is :"<<endl;


c3.display();
return 0;
}
OUTPUT:
root123@root123-HP-ProDesk-600-G3-SFF:~$ g++ PRF1.cpp
root123@root123-HP-ProDesk-600-G3-SFF:~$ ./a.out
2.1+4i
3+4i
And the sum is :
5.1+8i
Name: Lotake Sanket Bibhishan
Class: SE (COMP)
Roll No.:76
Subject:OOP

Practical No.: 02
Title: Develop a program in C++ to create a database of student’s information system using constructor,
destructor, static member functions, this pointer, inline code and dynamic memory allocation.

#include<iostream>
#include<string.h>
using namespace std;

class StudData;

class Student
{
string name;
int roll_no;
string cls;
char *division;
string dob;
char *bloodgroup;
static int count;
public:
Student() // Default Constructor
{
name="";
roll_no=0;
cls="";
division=new char;
dob="dd/mm/yyyy";
bloodgroup=new char[4];
}
~Student()
{
delete division;
delete[] bloodgroup;
cout<<"\nDestructor is Called in Student....!!!\n";
}

static int getCount()


{
return count;
}
void getData(StudData *);
void dispData(StudData *);
};

class StudData
{
string caddress;
long int *telno;
string *dlno;
friend class Student;
public:
StudData()
{
caddress="";
telno=new long;
dlno=new string;
}

~StudData()
{
delete telno;
delete dlno;
cout<<"\nDestructor is Called in StudData...!!!";
}
void getStudData()
{
cout<<"Enter Contact Address : ";
cin>>caddress;
cout<<"Enter Telephone Number : ";
cin>>*telno;
cout<<"Enter Driving License Number : ";
cin>>*dlno;
}

void dispStudData()
{
cout<<"Contact Address : "<<caddress<<endl;
cout<<"Telephone Number : "<<*telno<<endl;
cout<<"Driving License Number : "<<*dlno<<endl;
}
};

inline void Student::getData(StudData *st)


{
cout<<"Enter Student Name : ";
cin>>name;
cout<<"Enter Roll Number : ";
cin>>roll_no;
cout<<"Enter Class : ";
cin>>cls;
cout<<"Enter Division : ";
cin>>division;
cout<<"Enter Date of Birth : ";
cin>>dob;
cout<<"Enter Blood Group : ";
cin>>bloodgroup;
st->getStudData();
count++;
}

inline void Student::dispData(StudData *st1)


{
cout<<"Student Name : "<<name<<endl;
cout<<"Roll Number : "<<roll_no<<endl;
cout<<"Class : "<<cls<<endl;
cout<<"Division : "<<division<<endl;
cout<<"Date of Birth : "<<dob<<endl;
cout<<"Blood Group : "<<bloodgroup<<endl;
st1->dispStudData();
}

int Student::count;

int main()
{
Student *stud1[100];
StudData *stud2[100];
int n=0;
char ch;
do
{
stud1[n]=new Student;
stud2[n]=new StudData;
stud1[n]->getData(stud2[n]);
n++;
cout<<"Do you want to add another student (y/n) : ";
cin>>ch;
cin.get();
} while (ch=='y' || ch=='Y');

for(int i=0;i<n;i++)
{
cout<<"-----------------------------------------"<<endl;
stud1[i]->dispData(stud2[i]);
}
cout<<"------------------------------------------"<<endl;
cout<<"Total Students : "<<Student::getCount();
cout<<endl<<"-----------------------------------"<<endl;

for(int i=0;i<n;i++)
{
delete stud1[i];
delete stud2[i];
}

return 0;
}

OUTPUT:
root123@root123-HP-ProDesk-600-G3-SFF:~$ g++ studPR2.cpp
root123@root123-HP-ProDesk-600-G3-SFF:~$ ./a.out
Enter Student Name : NISHA
Enter Roll Number : 12
Enter Class : SE
Enter Division : A
Enter Date of Birth : 30-12-1998
Enter Blood Group : A+
Enter Contact Address : ANAGAR
Enter Telephone Number : 7654321789
Enter Driving License Number : AB1236789345
Do you want to add another student (y/n) : y
Enter Student Name : JAYESH
Enter Roll Number : 33
Enter Class : SE
Enter Division : A
Enter Date of Birth : 11-09-1999
Enter Blood Group : AB+
Enter Contact Address : PUNA
Enter Telephone Number : 8765432198
Enter Driving License Number : GH3421789976
Do you want to add another student (y/n) : n
-----------------------------------------
Student Name : NISHA
Roll Number : 12
Class : SE
Division : A
Date of Birth : 30-12-1998
Blood Group : A+
Contact Address : ANAGAR
Telephone Number : 7654321789
Driving License Number : AB1236789345
-----------------------------------------
Student Name : JAYESH
Roll Number : 33
Class : SE
Division : A
Date of Birth : 11-09-1999
Blood Group : AB+
Contact Address : PUNA
Telephone Number : 8765432198
Driving License Number : GH3421789976
------------------------------------------
Total Students : 2
-----------------------------------

Destructor is Called in Student....!!!

Destructor is Called in StudData...!!!


Destructor is Called in Student....!!!

Destructor is Called in StudData...!!!


Name: Lotake Sanket Bibhishan
Class: SE (COMP)
Roll No.:76
Subject:OOP

Practical No.: 03
Title:Creating a class which uses the concept of inheritance displays data & data members.

#include <iostream>
#include<string>
using namespace std;
class publication
{
protected:
string title;
float price;
public:
publication() //default constructor
{
title=" ";
price=0.0;
}

publication(string t,float p) //parameterized constructor


{
title=t;
price=p;
}

};

class book : public publication


{
int pagecount;
public:
book()
{
pagecount=0;
}

book(string t,float p,int pc):publication(t,p)


{
pagecount=pc;
}

void display()
{
cout<<"title :"<<title<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Pagecount :"<<pagecount<<endl;
}
};

class CD : public publication


{
float time;
public:
CD()
{
time=0.0;
}

CD(string t,float p,float tim):publication(t,p)


{
time=tim;
}

void display()
{
cout<<"title :"<<title<<endl;
cout<<"Price: "<<price<<endl;
cout<<"time in minutes :"<<time<<endl;
}
};

int main()
{
cout<<endl<<"Book data"<<endl;
book b("C++",230,300);
b.display();
cout<<endl<<"CD Data"<<endl;
CD c("programming",100,120.5);
c.display();

return 0;
}

OUTPUT:
root123@root123-HP-ProDesk-600-G3-SFF:~$ g++ TapeBook.cpp
root123@root123-HP-ProDesk-600-G3-SFF:~$ ./a.out

Book data
title :C++
Price: 230
Pagecount :300

CD Data
title :programming
Price: 100
time in minutes :120.5
Name: Lotake Sanket Bibhishan
Class: SE (COMP)
Roll No.:76
Subject:OOP

Practical No.: 03
Title: Creating a class which uses the concept of inheritance displays data & data members (USING
EXCEPTION HANDLING).
====================================================================
#include<iostream>

using namespace std;


class publication
{
private:
string title;
float price;
public:
void add()
{
cout<<"\nEnter the publication information : "<<endl;
cout<<"Enter title of the publication : ";
cin.ignore();
getline(cin, title);
cout<<"Enter Price of publication : ";
cin>>price;
}
void display()
{
cout<<"\n_______________________________________";
cout<<"\n Title of Publication : "<<title;
cout<<"\n Publication Price : "<<price;
}
};
class book : public publication
{
private:
int page_count;
public:
void add_book()
{
try
{
add();
cout<<"Enter page count of book : ";
cin>>page_count;
if (page_count <= 0)
{
throw page_count;
}
}
catch(...)
{
cout<<"\n Invalid Page Count!!!";
page_count=0;
}
}

void display_book()
{
display();
cout<<"\n Page Count : "<<
page_count;
cout<<"\n_______________________________________\n";
}
};
class tape : public publication
{
private:
float play_time;
public:
void add_tape()
{
try
{
add();
cout<<"Enter play duration of the tape : ";
cin>>play_time;
if (play_time <=0 )
throw play_time;
}
catch(...)
{
cout<<"\n Invalid Play Time!!!";
play_time=0;
}
}

void display_tape()
{
display();
cout<<"\nPlay Time : "<<play_time<<"min";
cout<<"\n__________________________________\n";
}
};

int main()
{
book b1[10];
tape t1[10];
int ch, b_count = 0, t_count = 0;
do
{
cout<<"\n*****PUBLICATION DATABASE SYSTEM*****";
cout<<"\n-----MENU-----";
cout<<"\n1. Add Information to Books";
cout<<"\n2. Add Information to Tapes";
cout<<"\n3. Display Books Information";
cout<<"\n4. Display Tapes Information";
cout<<"\n5. Exit";
cout<<"\nEnter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
b1[b_count].add_book();
b_count++;
break;
case 2:
t1[t_count].add_tape();
t_count++;
break;
case 3:
cout<<"\n***BOOK PUBLICATION DATABASE SYSTEM***";
for (int j=0;j<b_count;j++)
{
b1[j].display_book();
}
break;
case 4:
cout<<"\n***TAPE PUBLICATION DATABASE SYSTEM***";
for (int j=0;j<t_count;j++)
{
t1[j].display_tape();
}
break;
case 5:
exit(0);
}
}while(ch!=5);

return 0;
}

OUTPUT:
root123@root123-HP-ProDesk-600-G3-SFF:~$ gedit TapeBook123.cpp
root123@root123-HP-ProDesk-600-G3-SFF:~$ g++ TapeBook123.cpp
root123@root123-HP-ProDesk-600-G3-SFF:~$ ./a.out

*****PUBLICATION DATABASE SYSTEM*****


-----MENU-----
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter Your Choice : 1

Enter the publication information :


Enter title of the publication : C++ Programming
Enter Price of publication : 259.50
Enter page count of book : 369

*****PUBLICATION DATABASE SYSTEM*****


-----MENU-----
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter Your Choice : 3

***BOOK PUBLICATION DATABASE SYSTEM***


_______________________________________
Title of Publication : C++ Programming
Publication Price : 259.5
Page Count : 369
_______________________________________

*****PUBLICATION DATABASE SYSTEM*****


-----MENU-----
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter Your Choice : 2

Enter the publication information :


Enter title of the publication : C++ Black Book
Enter Price of publication : 199.00
Enter play duration of the tape : 120

*****PUBLICATION DATABASE SYSTEM*****


-----MENU-----
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter Your Choice : 4

***TAPE PUBLICATION DATABASE SYSTEM***


_______________________________________
Title of Publication : C++ Black Book
Publication Price : 199
Play Time : 120min
__________________________________

*****PUBLICATION DATABASE SYSTEM*****


-----MENU-----
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter Your Choice : 5
Name: Lotake Sanket Bibhishan
Class: SE (COMP)
Roll No.:76
Subject:OOP

Practical No.: 04
Title: Write a C++ program that creates an output file, write information of it, read information from the file
(open in input file) and closes the file.
=====================================================================

#include<iostream>
#include<fstream>

using namespace std;


class Employee
{
char Name[20];
int ID;
double salary;
public:
void accept()
{
cout<<"\n Enter Employee Name:";
cin>>Name;
cout<<"\n Enter Employee ID:";
cin>>ID;
cout<<"\n Enter Employee Salary:";
cin>>salary;
}
void display()
{
cout<<"\n Employee Name:"<<Name;
cout<<"\n Employee Id:"<<ID;
cout<<"\n Employee Salary:"<<salary;
}
};

int main()
{
Employee o[5];
fstream f;
int i,n;

f.open("PQR.txt",ios::out);
cout<<"\n How many employee information do you need to store? : ";
cin>>n;
cout<<"\n Enter information of employee (NAME/ID/SALARY)";

for(i=0;i<n;i++)
{
cout<<"\n Enter information of:"<<i<<" Employee:"<<endl;
o[i].accept();
f.write((char*)&o[i],sizeof o[i]);
}

f.close();

f.open("PQR.txt",ios::in);
cout<<"\n===== Information of Employees is as follows =====";
for(i=0;i<n;i++)
{
f.read((char*)&o[i],sizeof o[i]);
o[i].display();
}
f.close();

return 0;
}

OUTPUT:
root123@root123-HP-ProDesk-600-G3-SFF:~$ g++ FileHandling.cpp
root123@root123-HP-ProDesk-600-G3-SFF:~$ ./a.out

How many employee information do you need to store? : 2

Enter information of employee (NAME/ID/SALARY)


Enter information of:0 Employee:

Enter Employee Name:Ganesh

Enter Employee ID:101

Enter Employee Salary:29000

Enter information of:1 Employee:

Enter Employee Name:NIKITA

Enter Employee ID:123

Enter Employee Salary:32000

===== Information of Employees is as follows =====


Employee Name:Ganesh
Employee Id:101
Employee Salary:29000
Employee Name:NIKITA
Employee Id:123
Employee Salary:32000
Name: Lotake Sanket Bibhishan
Class: SE (COMP)
Roll No.:76
Subject:OOP

Practical No.: 05
Title: Write a function template selection sort on integer and float array.
----------------------------------------------------------------------------
#include<iostream>

using namespace std;


#define size 10 //constant variable declaration
int n; //global declaration of variable

template<class T>
void selection(T A[size])
{
int i,j,min;
T temp;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(A[j]<A[min])
min=j;
}
temp=A[i];
A[i]=A[min];
A[min]=temp;
}
cout<<"\nSorted array: ";
for(i=0;i<n;i++)
{
cout<<" "<<A[i];
}
}

int main()
{
int choice;
int A[size];
float B[size];
int i;
cout<<"-----------------------";
do
{
cout<<"\n 1. Integer ";
cout<<"\n 2. Float ";
cout<<"\n 3. Exit "<<endl;
cout<<"\n Enter Choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nEnter Total Number Of Integer Elements:";
cin>>n;
cout<<"\nEnter Integer Elements:";
for(i=0;i<n;i++)
{
cin>>A[i];
}
selection(A);
break;
case 2:
cout<<"\nEnter Total Number Of Float Elements:";
cin>>n;
cout<<"\nEnter Float Elememts:";
for(i=0;i<n;i++)
{
cin>>B[i];
}
selection(B);
break;
case 3:
exit(0);
default:
cout<<"\n Invalid Choice";
}
}while(choice!=4);

return 0;
}

OUTPUT:

root123@root123-HP-ProDesk-600-G3-SFF:~$ g++ temp.cpp


root123@root123-HP-ProDesk-600-G3-SFF:~$ ./a.out
-----------------------
1. Integer
2. Float
3. Exit

Enter Choice : 1

Enter Total Number Of Integer Elements:5

Enter Integer Elements:30 12 45 10 8

Sorted array: 8 10 12 30 45
1. Integer
2. Float
3. Exit

Enter Choice : 2

Enter Total Number Of Float Elements:5

Enter Float Elememts:12.00 9.05 8.00 25.01 22.05

Sorted array: 8 9.05 12 22.05 25.01


1. Integer
2. Float
3. Exit

Enter Choice : 3
Name: Lotake Sanket Bibhishan
Class: SE (COMP)
Roll No.:76
Subject:OOP

Practical No.: 06
Title: Write C++ program using STL for sorting and searching user defined records such as Item records
(item code, name, cost, quantity etc.) using vector container.
====================================================================

#include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Item
{
public:
char name[10];
int quantity;
int cost;
int code;
bool operator ==(const Item &i1)
{
if(code == i1.code)
return 1;
return 0;
}
bool operator <(const Item &i1)
{
if(code < i1.code)
return 1;
return 0;
}
};
vector <Item> v1;
void print(Item &i1);
void display();
void insert();
void search();
void del();
bool compare(const Item &i1, const Item &i2)
{
return i1.cost < i2.cost;
}
int main(){
int ch;
do
{
cout<<"\nOperations:\n";
cout<<"\n1. Insert/Add";
cout<<"\n2. Display";
cout<<"\n3. Search";
cout<<"\n4. Sort";
cout<<"\n5. Delete";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
sort(v1.begin(), v1.end(), compare);
cout<<"\n Sorted cost";
display();
break;
case 5:
del();
break;
case 6:
exit(0);
}
}while(ch!=7);

return 0;
}
void insert()
{
Item i1;
cout<<"\nEnter Item name: ";
cin>>i1.name;
cout<<"\nEnter Item Quantity: ";
cin>>i1.quantity;
cout<<"\nEnter Item Cost: ";
cin>>i1.cost;cout<<"\nEnter Item code: ";
cin>>i1.code;
v1.push_back(i1);
}
void display()
{
for_each(v1.begin(), v1.end(), print);
}
void print(Item &i1)
{
cout<<"\n\nItem name: "<<i1.name;
cout<<"\nItem Quantity: "<<i1.quantity;
cout<<"\nItem Cost: "<<i1.cost;
cout<<"\nItem Code: "<<i1.code;
cout<<"\n";
}
void search()
{
vector <Item> :: iterator p;
Item i1;
cout<<"\nEnter Item Code to search whether item record is found or not?:";
cin>>i1.code;
p = find(v1.begin(), v1.end(), i1);
if(p == v1.end())
{
cout<<"\nRecord Not Found ... ";
}
else
{
cout<<"\nRecord Found ...";
}
}
void del()
{
vector <Item> :: iterator p;
Item i1;
cout<<"\nEnter Item Code which is record to be deleted : ";
cin>>i1.code;
p = find(v1.begin(), v1.end(), i1);
if(p == v1.end())
{
cout<<"\nRecord Not Found ... ";
}
else
{
v1.erase(p);cout<<"\nRecord is Deleted ...";
}
}

OUTPUT:-
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# g++ dhiraj6.cpp
root@root123-HP-ProDesk-600-G3-SFF:/home/root123# ./a.out

Operations:

1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit

Enter your choice: 1


Enter Item name: Mobile

Enter Item Quantity: 6

Enter Item Cost: 12000

Enter Item code: 456

Operations:

1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit

Enter your choice: 1

Enter Item name: computer

Enter Item Quantity: 3

Enter Item Cost: 15000

Enter Item code: 689

Operations:

1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit

Enter your choice: 2

Item name: Mobile


Item Quantity: 6
Item Cost: 12000
Item Code: 456

Item name: computer


Item Quantity: 3
Item Cost: 15000
Item Code: 689

Operations:
1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit

Enter your choice: 3

Enter Item Code to search whether item record is found or not?:456

Record Found ...


Operations:

1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit

Enter your choice: 4

Sorted cost

Item name: Mobile


Item Quantity: 6
Item Cost: 12000
Item Code: 456

Item name: computer


Item Quantity: 3
Item Cost: 15000
Item Code: 689

Operations:

1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit

Enter your choice: 5

Enter Item Code which is record to be deleted : 689

Record is Deleted ...


Operations:
1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit

Enter your choice: 6


root@root123-HP-ProDesk-600-G3-SFF:/home/root123#
Name: Lotake Sanket Bibhishan
Class: SE (COMP)
Roll No.:76
Subject:OOP

Practical No.: 07
Title: Write a program in C++ to use map associative container.
========================================================================
#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
typedef map<string,int> mapType;
mapType populationMap;

populationMap.insert(pair<string, int>("Maharashtra", 7026357));


populationMap.insert(pair<string, int>("Rajasthan", 6578936));
populationMap.insert(pair<string, int>("Karanataka", 6678993));
populationMap.insert(pair<string, int>("Punjab", 5789032));
populationMap.insert(pair<string, int>("West Bengal", 6676291));

mapType::iterator iter;

cout<<"========Population of states in India==========\n";


cout<<"\n Size of populationMap : "<<populationMap.size()<<"\n";
string state_name;
cout<<"\n Enter name of the state :";
cin>>state_name;

iter = populationMap.find(state_name);

if( iter!= populationMap.end() )


cout<<state_name<<" 's population is "<<iter->second ;

else
cout<<"Key is not populationMap"<<"\n";

populationMap.clear();

return 0;
}
OUTPUT 1:
root123@root123-HP-ProDesk-600-G3-SFF:~$ g++ map.cpp
root123@root123-HP-ProDesk-600-G3-SFF:~$ ./a.out
========Population of states in India==========

Size of populationMap : 5
Enter name of the state :Punjab
Punjab 's population is 5789032

OUTPUT 2:
root123@root123-HP-ProDesk-600-G3-SFF:~$ ./a.out
========Population of states in India==========

Size of populationMap : 5

Enter name of the state :Maharashtra


Maharashtra 's population is 7026357

You might also like