0% found this document useful (0 votes)
34 views21 pages

Experiment 1,3,6,7 Code

oop codes

Uploaded by

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

Experiment 1,3,6,7 Code

oop codes

Uploaded by

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

Experiment 3 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;
}

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


{
time1=tim;
}
void getdata(void)
{
publication::getdata();
cout <<"Enter tape's playing time:";
cin>> time1;
}
void putdata(void)
{
publication::putdata();
cout<<" Tape's playing time :"<< time1<<endl;
}
};
int main()
{
cout<<endl<<"Book data"<<endl;
book b("C++",230,300);
b.putdata();
cout<<endl<<"CD Data"<<endl;
CD c("C++",100,120.5);
c.putdata();
cout<<"\n Enter New Details Of Book :\n";
b.getdata();
c.getdata();
cout<<"\n\n Book data entered by user:\n";
b.putdata();
c.putdata();
return 0;
}
/*
OUTPUT:-
Book data
Publication titles :C++
Publication price :230
Book page count:300
-----------------------------------------------------------------------------------
CD Data
Publication titles :C++
Publication price :100
Tape's playing time :120.5
-------------------------------------------------------------------------------------
Enter New Details Of Book :
-------------------------------------------------------------------------------------
Enter title of publication: IKIGAI
Enter price of publication: 100
Enter Book Page Count :277
-------------------------------------------------------------------------------------
Enter title of publication: MAHABHARAT
Enter price of publication: 800
Enter tape's playing time:1025
--------------------------------------------------------------------------------------
Book data entered by user:
Publication titles :IKIGAI
Publication price :100
Book page count:277
----------------------------------------------------------------------------------------
Publication titles :MAHABHARAT
Publication price :800
Tape's playing time :1025
-------------------------------------------------------------------------------------
=== Code Execution Successful ===
/*
Experiment 1 Code:-
/*PROBLEM STATEMENT:
Implement a class Complex which represents the Complex Number data type. Implement
the
following operations:
1.Constructor (including a default constructor which creates the complex number 0+0i).
2.Overloaded operator+ to add two complex numbers.
3.Overloaded operator* to multiply two complex numbers.
4.Overloaded <<and>> to print and read Complex Numbers.
*/
#include <iostream>
using namespace std;
class complex //class name complex is declared
{
float realp,imagp;
public:
complex() //default constructor
{
realp=0;
imagp=0;
}
complex operator+(complex &); //for addition of two complex nos
complex operator*(complex &); //for multiplication of two complex nos
complex(float,float); //parameterized constructor
friend istream &operator>>(istream &,complex &);
friend ostream &operator<<(ostream &,complex &);
};
complex::complex(float x,float y) //parameterized constructor definition
{
realp=x;
imagp=y;
}
//function to accept values of real and imag parts of complex no
istream &operator>>(istream &din,complex &c)
{
cout<<"Enter real part of complex number 2: ";
din>>c.realp;
cout<<"\nEnter imaginary part of complex number 2: ";
din>>c.imagp;
return din;
}
//functions to display complex nos
ostream &operator<<(ostream &dout , complex &c)
{
dout<<c.realp<<" + "<<c.imagp<<"i";
dout<<endl;
return dout;
}
//function to add two complex nos
complex complex::operator+(complex &c)
{
complex temp;
temp.realp=realp + c.realp;
temp.imagp=imagp + c.imagp;
return temp;
}
//function to multiply two complex nos
complex complex::operator*(complex &c)
{
complex mul;
mul.realp=(realp*c.realp) - ( imagp*c.imagp);
mul.imagp=(imagp*c.realp) + (realp*c.imagp);
return mul;
}
int main()
{
complex c2,c3;
complex c1(1.2,2.2);
cout<<"Complex no 1 is:"<<c1;
cout<<"Enter complex no 2:\n";
cin>>c2;
cout<<"Complex number 1 is :";
cout<<c1;
cout<<"Complex number 2 is :";
cout<<c2;
cout<<"Complex number 3 is :";
cout<<c3;
cout<<"\nAddition of two complex numbers is: ";
c3=c1+c2;
cout<<c3;
cout<<"\nMultiplication of two complex number is: ";
c3=c1*c2;
cout<<c3; //display value of c3
return 0;
}
/*OUTPUT:-
Complex no 1 is:1.2 + 2.2i
Enter complex no 2:
Enter real part of complex number 2: 3.4
Enter imaginary part of complex number 2: 7i
Complex number 1 is :1.2 + 2.2i
Complex number 2 is :3.4 + 7i
Complex number 3 is :0 + 0i

Addition of two complex numbers is: 4.6 + 9.2i

Multiplication of two complex number is: -11.32 + 15.88i


=== Code Execution Successful ===
*/
Experiment 6 Code:-
/*
Write C++ Program using STL for sorting and searching user defined
records such as item records 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> o1;
void print(Item &i1);

void display();
void insert();

void search();

void dlt();

bool compare(const Item &i1, const Item &i2)

{
return i1.cost < i2.cost;

}
int main()

int ch;
do

cout<<"\n* * * * * Menu * * * * *";


cout<<"\n1.Insert";

cout<<"\n2.Display";

cout<<"\n3.Search";

cout<<"\n4.Sort";
cout<<"\n5.Delete";

cout<<"\n6.Exit";

cout<<"\nEnter your choice : ";


cin>>ch;

switch(ch)

{
case 1:

insert();
break;

case 2:
display();
break;
case 3:

search();
break;

case 4:

sort(o1.begin(),o1.end(),compare);

cout<<"\n\n Sorted on Cost : ";

display();
break;

case 5:
dlt();

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;

o1.push_back(i1);
}
void display()
{

for_each(o1.begin(),o1.end(),print);
}

void print(Item &i1)

cout<<"\n";

cout<<"\nItem Name : "<<i1.name;


cout<<"\nItem Quantity : "<<i1.quantity;

cout<<"\nItem Cost : "<<i1.cost;


cout<<"\nItem Code : "<<i1.code;

cout<<"\n\n";

}
void search()

vector<Item>::iterator p;
Item i1;

cout<<"\nEnter Item Code to search : ";

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;

cout<<"\nEnter Item Code to delete : ";


cin>>i1.code;

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>

using namespace std;

int main()

typedef map<string,int> mapType;

mapType populationMap;

populationMap.insert(pair<string, float>("Maharashtra", 125));

populationMap.insert(pair<string, float>("Uttar Pradesh", 225));


populationMap.insert(mapType::value_type("Bihar", 120));
populationMap.insert(mapType::value_type("West Bengal", 100));

populationMap.insert(make_pair("Madhya Pradesh", 90));


populationMap.insert(make_pair("Tamil Nadu", 80));

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("UT Delhi", 19));


populationMap.insert(make_pair("UT Jammu and Kashmir", 14));

populationMap.insert(make_pair("Uttarakhand", 12));

populationMap.insert(make_pair("Himachal Pradesh", 8));


populationMap.insert(make_pair("Tripura", 04));

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("Arunachal Pradesh", 2));

populationMap.insert(make_pair("UT Puducherry", 2));


populationMap.insert(make_pair("Mizoram", 1));

populationMap.insert(make_pair("UT Chandigarh", 1));

populationMap.insert(make_pair("Sikkim", 1));
populationMap.insert(make_pair("UT Dadra and Nagar Haveli and Daman and Diu",
1));

populationMap.insert(make_pair("UT Andaman and Nicobar Islands", 1));

populationMap.insert(make_pair("UT Lakshadweep", 0.0003));

populationMap.insert(make_pair("UT Ladakh", 0.00006));

mapType::iterator iter = --populationMap.end();

populationMap.erase(iter);
cout << "Total state and UT of India with Size of populationMap: " <<
populationMap.size() << '\n';

for (iter = populationMap.begin(); iter != populationMap.end(); ++iter)

cout << iter->first <<":" << iter->second << " million\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() )

cout << state <<"'s populations is "

<< iter->second << " million\n";

else
cout << "State is not in populationMap" << '\n';

cout<<"Do you wish to continue?(y/n):";


cin>>c;

}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 ===
/*

You might also like