Experiment 1,3,6,7 Code
Experiment 1,3,6,7 Code
/*
Imagine a publishing company which does marketing for book
and audiocassette versions.
Create a class publication that stores the title (a string)
and price (type float) of a
publication.From this class derive two classes: book, which
adds a page count(type int),
and tape, which adds a playing time in minutes(type float).
Write a program that instantiates the book and tape classes,
allows user to enter data and
displays the data members.If an exception is caught, replace
all the data member values
with zero values.
*/
#include<iostream>
#include<string>
using namespace std;
class publication
{
protected:
string title;
float price;
public:
publication()
{
title=" ";
price=0.0;
}
publication(string t,float p)
{
title=t;
price=p;
}
public:
void getdata()
{
cout<<"Enter title of publication: ";
cin>>title;
cout<<"Enter price of publication: ";
cin>>price;
}
void putdata(void)
{
cout<<"Publication titles :"<<title<<endl;
cout<<"Publication price :"<<price<<endl;
}
};
class book : public publication
{
int pagecount;
public:
book()
{
pagecount=0;
}
book(string t,float p,int pc):publication(t,p)
{
pagecount=pc;
}
void getdata(void)
{
publication::getdata();
cout <<"Enter Book Page Count :";
cin>> pagecount;
}
void putdata(void)
{
publication::putdata(); //Show Publication data
cout<< "Book page count:"<<pagecount <<endl;
}
};
class CD: public publication
{
float time1;
public:
CD()
{
time1=0.0;
}
#include <algorithm>
#include <vector>
class Item
{
public:
char name[10];
int quantity;
int cost;
int code;
if(code==i1.code)
return 1;
return 0;
}
if(code<i1.code)
return 1;
return 0;
};
vector<Item> o1;
void print(Item &i1);
void display();
void insert();
void search();
void dlt();
{
return i1.cost < i2.cost;
}
int main()
int ch;
do
cout<<"\n2.Display";
cout<<"\n3.Search";
cout<<"\n4.Sort";
cout<<"\n5.Delete";
cout<<"\n6.Exit";
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
sort(o1.begin(),o1.end(),compare);
display();
break;
case 5:
dlt();
break;
case 6:
exit(0);
}while(ch!=7);
return 0;
void insert()
{
Item i1;
cin>>i1.name;
cout<<"\nEnter Item Quantity : ";
cin>>i1.quantity;
o1.push_back(i1);
}
void display()
{
for_each(o1.begin(),o1.end(),print);
}
cout<<"\n";
cout<<"\n\n";
}
void search()
vector<Item>::iterator p;
Item i1;
cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
cout<<"\nNot found!!!";
}
else
{
cout<<"\nFound!!!";
}
}
void dlt()
{
vector<Item>::iterator p;
Item i1;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
cout<<"\nNot found!!!";
}
else
{
o1.erase(p);
cout<<"\nDeleted!!!";
}
/*
OUTPUT:-
* * * * * Menu * * * * *
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
---------------------------------------------------------
Enter your choice : 1
Enter Item Name : PEN
Enter Item Quantity : 100
Enter Item Cost : 5
Enter Item Code : 001
* * * * * Menu * * * * *
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
--------------------------------------------------------------------------------------------------------
Enter your choice : 1
Enter Item Name : PENCIL
Enter Item Quantity : 250
Enter Item Cost : 7
Enter Item Code : 002
* * * * * Menu * * * * *
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
-------------------------------------------------------------------------------------------------------
Enter your choice : 1
Enter Item Name : NOTEBOOK
Enter Item Quantity : 50
Enter Item Cost : 55
Enter Item Code : 004
* * * * * Menu * * * * *
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
-------------------------------------------------------------------------------------------------
Enter your choice : 2
Item Name : N
Item Quantity : 100
Item Cost : 5
Item Code : 1
Item Name : PENCIL
Item Quantity : 250
Item Cost : 7
Item Code : 2
Item Name : NOTEBOOK
Item Quantity : 50
Item Cost : 55
Item Code : 4
* * * * * Menu * * * * *
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
-----------------------------------------------------------------------------------------------------------
Enter your choice : 3
Enter Item Code to search : 002
Found!!!
* * * * * Menu * * * * *
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
-----------------------------------------------------------------------------------------------------
Enter your choice : 4
Sorted on Cost :
Item Name : N
Item Quantity : 100
Item Cost : 5
Item Code : 1
Item Name : PENCIL
Item Quantity : 250
Item Cost : 7
Item Code : 2
Item Name : NOTEBOOK
Item Quantity : 50
Item Cost : 55
Item Code : 4
* * * * * Menu * * * * *
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
-------------------------------------------------------------------------------------------------------------
Enter your choice : 5
Enter Item Code to delete : 004
Deleted!!!
* * * * * Menu * * * * *
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
--------------------------------------------------------------------------------------------------------------
Enter your choice : 2
Item Name : N
Item Quantity : 100
Item Cost : 5
Item Code : 1
Item Name : pencil
Item Quantity : 250
Item Cost : 7
Item Code : 2
* * * * * Menu * * * * *
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
-----------------------------------------------------------------------------------------------------
Enter your choice : 6
=== Code Execution Successful ===
/*
Experiment 7 Code:-
/*Write a program in C++ to use map associative container. The keys will be the names
of
states and the values will be the populations of the states. When the program runs, the
user
is prompted to type the name of a state. The program then looks in the map, using the
state
name as an index and returns the population of the state.
/*
#include <iostream>
#include <map>
#include <string>
#include <utility>
int main()
mapType populationMap;
populationMap.insert(make_pair("Rajasthan", 78));
populationMap.insert(make_pair("Andhra Pradesh", 53));
populationMap.insert(make_pair("Odisha", 47));
populationMap.insert(make_pair("Kerala", 38));
populationMap.insert(make_pair("Telangana", 37));
populationMap.insert(make_pair("Assam", 35));
populationMap.insert(make_pair("Jharkhand", 38));
populationMap.insert(make_pair("Karnataka", 68));
populationMap.insert(make_pair("Gujarat", 70));
populationMap.insert(make_pair("Punjab", 31));
populationMap.insert(make_pair("Chhattisgarh", 30));
populationMap.insert(make_pair("Haryana", 29));
populationMap.insert(make_pair("Uttarakhand", 12));
populationMap.insert(make_pair("Meghalaya", 4));
populationMap.insert(make_pair("Manipur[", 3));
populationMap.insert(make_pair("Nagaland", 2));
populationMap.insert(make_pair("Goa", 2));
populationMap.insert(make_pair("Sikkim", 1));
populationMap.insert(make_pair("UT Dadra and Nagar Haveli and Daman and Diu",
1));
populationMap.erase(iter);
cout << "Total state and UT of India with Size of populationMap: " <<
populationMap.size() << '\n';
char c;
do
{
string state;
cout<<"\nEnter that state you want to know the population of: ";
cin>>state;
iter = populationMap.find(state);
if( iter != populationMap.end() )
else
cout << "State is not in populationMap" << '\n';
}while(c=='y'||c=='Y');
populationMap.clear();
return 0;
/*
OUTPUT:-
Total state and UT of India with Size of populationMap: 35
Andhra Pradesh:53 million
Arunachal Pradesh:2 million
Assam:35 million
Bihar:120 million
Chhattisgarh:30 million
Goa:2 million
Gujarat:70 million
Haryana:29 million
Himachal Pradesh:8 million
Jharkhand:38 million
Karnataka:68 million
Kerala:38 million
Madhya Pradesh:90 million
Maharashtra:125 million
Manipur[:3 million
Meghalaya:4 million
Mizoram:1 million
Nagaland:2 million
Odisha:47 million
Punjab:31 million
Rajasthan:78 million
Sikkim:1 million
Tamil Nadu:80 million
Telangana:37 million
Tripura:4 million
UT Andaman and Nicobar Islands:1 million
UT Chandigarh:1 million
UT Dadra and Nagar Haveli and Daman and Diu:1 million
UT Delhi:19 million
UT Jammu and Kashmir:14 million
UT Ladakh:0 million
UT Lakshadweep:0 million
UT Puducherry:2 million
Uttar Pradesh:225 million
Uttarakhand:12 million
----------------------------------------------------------------------------------------------------------------
Enter that state you want to know the population of: Maharashtra
Maharashtra's populations is 125 million
--------------------------------------------------------------------------------------------------------------
Do you wish to continue?(y/n):n
=== Code Execution Successful ===
/*