0% found this document useful (0 votes)
167 views39 pages

OOP Programs 2025

Uploaded by

mhtanmy862
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)
167 views39 pages

OOP Programs 2025

Uploaded by

mhtanmy862
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/ 39

/*

EXPERIMENT NO. : 01
GROUP : A
TITLE : Implement a class Complex which represents the Complex
Number data type. Implement the following
1. Constructor (including a default constructor which creates the complex
number 0+0i).
2. Overload operator+ to add two complex numbers.
3. Overload operator* to multiply two complex numbers.
4. Overload operators << and >> to print and read Complex Numbers.
ROLL NO. :
BATCH :
*/

#include<iostream>
using namespace
std; class complex
{
float
x;
float
y;
public:
complex()
{
x=0
;
y=0
;
}
complex operator+
(complex); complex
operator*(complex);
friend istream &operator >>(istream &input,complex &t)
{
cout<<"\n Enter the real part
==>"; input>>t.x;
cout<<"\n Enter the imaginary part
==>"; input>>t.y;
}
friend ostream &operator <<(ostream &output,complex &t)
{
output<<t.x<<"+"<<t.y<<"i\n";
}
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.
x;
temp.y=y+c.
y;
return(temp
);
}
complex complex::operator*(complex c)
{
complex temp2;
temp2.x=(x*c.x)-
(y*c.y);
temp2.y=(y*c.x)+
} (x*c.y); return
int (temp2);
main()
{

complex c1,c2,c3,c4;
cout<<"Default constructor value
==>\n"; cout<<c1;
cout<<"\n Enter the 1st number
==>\n"; cin>>c1;
cout<<"\n Enter the 2nd number
==>\n"; cin>>c2;
c3=c1+c2
;
c4=c1*c2
;
cout<<"\n The first number is
==>"; cout<<c1;
cout<<"\n The second number is
==>"; cout<<c2;
cout<<"\n The addition is
==>"; cout<<c3;
} cout<<"\n The multiplication is
==>"; cout<<c4;
return 0;
//
*******************************************************************************
/*
OUTPUT:
Default constructor value
==> 0+0i

Enter the 1st number

Enter the real part


==>4

Enter the imaginary part ==>4


Enter the 2nd number

Enter the real part

==>7

Enter the imaginary part

==>4 The first number is

==>4+4i The second number

is ==>7+4i The addition is

==>11+8i

The multiplication is ==>12+44i

Process exited after 21.98 seconds with return value


0 Press any key to continue . . .
*/
/*
EXPERIMENT NO. : 02
GROUP : A
TITLE : Develop a program C++ program to create a database of student's
information system containing the following information: Name, Roll number,
Class, Division, Date of Birth, Blood group, Contact address, Telephone
number, Driving license no. and other. Construct the database with suitable
member functions. Make use of constructor, default constructor, copy
constructor, destructor, static member functions, friend class, this pointer,
inline code and dynamic memory allocation operators-new and delete as well as
exception handling.
ROLL NO. :
BATCH :
*/

#include<iostream>
#include<string.h> // header file declares a set of functions to work
strings. using namespace std;

class db
{
int roll;
char
name[20];
char
Class[10];
char Div[10];
char dob[12];
char bg[5],city[10];
char phone[12],license[12];

public:
static int stdno; // declaration of static variable
static void count() // defination of static function
{
cout<<"\n No.of objects created: "<<stdno;
}

db() // default constructor


{
roll=7;
strcpy(name,"Shreeya");
strcpy(Class,"SE");
strcpy(Div,"A");
strcpy(dob,"13/08/1992")
;
strcpy(bg,"B+");
strcpy(city,"Nashik");
strcpy(phone,"9123456789");
strcpy(license,"A1010");
++stdno;
}
void getdata() // defining member function
{
cout<<"\n Enter your Name ==>
"; cin>>name;
cout<<"\n Enter Roll No ==>
"; cin>>roll;
cout<<"\n Enter Class ==>
"; cin>>Class;
cout<<"\n Enter Division ==>
"; cin>>Div;
cout<<"\n Enter Date of Birth
==> "; cin>>dob;
cout<<"\n Enter Blood Group ==>
"; cin>>bg;
cout<<"\n Enter City ==>
"; cin>>city;
cout<<"\n Enter Phone number
==> "; cin>>phone;
cout<<"\n Enter Licence number ==> ";
cin>>license;
}
friend void display(db d); // declaration of friend function

~db() // destructor
{
cout<<"\n\n"<<this->name<<"(Object) is destroyed!\n";
}
};

void display(db d) // defination of friend function


{
cout<<"\n Name:"<<d.name;
cout<<"\n
Roll_No:"<<d.roll; cout<<"\
n Class:"<<d.Class;
cout<<"\n Div:"<<d.Div;
cout<<"\n DOB:"<<d.dob;
cout<<"\n Blood
group:"<<d.bg;
cout<<"\n City:"<<d.city;
cout<<"\n Phone_No:"<<d.phone;
cout<<" \n
License_No:"<<d.license;
}
int db::stdno; // Define static data member stdno outside the
class

int main()
{
int n,i;
db d1,*ptr[5]; cout<<"\
nDefault values:";
display(d1);

d1.getdata()
;
display(d1)
;

cout<<"\nHow many objects u want to create?:";


cin>>n;
for(i=0;i<n;i++)
{
ptr[i]=new db();
//new operator use to dynamic memory(run time)
allocation ptr[i]->getdata();
}
for(i=0;i<n;i++)
display(*ptr[i]
);
db::count(); // calling of static function

for(i=0;i<n;i++)
{
delete(ptr[i]);
//delete operator use to deallocation of memory
}
cout<<"\nObjects deleted!" ;
}
//*****************************************************************************
/*
OUTPUT:

Default
values:
Name:Shreeya
Roll_No:7
Class:SE
Div:A
DOB:13/08/19
92
Blood group:B+
City:Nashik
Phone_No:91234567
89
License_No:A1010

Shreeya(Object) is

destroyed! Enter your

Name ==> Kajal Enter Roll

No ==> 9

Enter Class ==> SE

Enter Division ==> A

Enter Date of Birth ==> 21/12/1991

Enter Blood Group ==> A+

Enter City ==> Nashik

Enter Phone number ==> 1234567890

Enter Licence number ==> A2020

Name:Kajal
Roll_No:9
Class:SE
Div:A
DOB:21/12/19
91
Blood group:A+
City:Nashik
Phone_No:12345678
90
License_No:A2020
Kajal(Object) is destroyed!

How many objects u want to

create?:2 Enter your Name ==>

Shubham

Enter Roll No ==>

5 Enter Class ==>

SE Enter Division

==> A

Enter Date of Birth ==> 11/02/1992

Enter Blood Group ==> B+

Enter City ==> Nashik

Enter Phone number ==>

9876543210 Enter Licence

number ==> C3030 Enter your

Name ==> Siddharth Enter Roll

No ==> 6

Enter Class ==> SE

Enter Division ==> A

Enter Date of Birth ==> 05/03/1990

Enter Blood Group ==> AB+

Enter City ==> Nashik

Enter Phone number ==>

123467890 Enter Licence

number ==> D4040 Name:Shubham


Roll_No:5
Class:SE
Div:A
DOB:11/2/19
92
Blood group:B+
City:Nashik
Phone_No:98765432
10
License_No:C3030

Shubham(Object) is

destroyed! Name:Siddharth
Roll_No:6
Class:SE
Div:A
DOB:05/03/19
90
Blood group:AB+
City:Nashik
Phone_No:1234678
90
License_No:D4040

Siddharth(Object) is

destroyed! No.of objects

created: 3 Shubham(Object)

is destroyed!

Siddharth(Object) is

destroyed! Objects deleted!

Kajal(Object) is destroyed!

Process exited after 184.3 seconds with return value


0 Press any key to continue . . .
*/
/*
EXPERIMENT NO. : 03
GROUP : A
TITLE : Imagine a publishing company which does marketing for book and audio
cassette versions. Create a class publication that stores the title (a string)
and price (type float) of publications. 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 class, allows user to
enter data and displays the data members. If an exception is caught, replace
all the data member values with zero values.
ROLL NO. :
BATCH :
*/

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

class publication
{
protected:
string
title;
float
price;
public:
publication()
{
price =
0.0; title
= " ";
}
publication(string t,float p)
{
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<<e
ndl;
}
};
class tape : public publication
{
float
time;
public
:
tape() time=0.0;
{

}
tape(string t,float p,float tim):publication(t,p)
{
time=tim;
}
void display()
{
cout<<"Title :"<<title<<en
dl; cout<<"Price:
"<<price<<endl;
cout<<"Playing time in minutes :"<<time<<endl;
}
};
int main()
{
string
title1;
float
price1;
int pagec;
float min;
cout<<"Enter the book title ==>
"; cin>>title1;
cout<<"Enter the book price ==>
"; cin>>price1;
cout<<"Enter the book pagecount ==> ";
cin>>pagec;
cout<<"Enter the playing time in minutes ==>
"; cin>>min;

cout<<endl<<"********** BOOK DATA **********


"<<endl; book b1(title1,price1,pagec);
b1.display();

cout<<endl<<"********** TAPE DATA


**********"<<endl; tape c1(title1,price1,min);
c1.display()
; return 0;
}
*****************************************************************************
OUTPUT:
Enter the book title ==> Java
Enter the book price ==> 495
Enter the book pagecount ==>
876
Enter the playing time in minutes ==> 55

********** BOOK DATA


********** Title :Java
Price: 495
Pagecount :876

********** TAPE DATA


********** Title :Java
Price: 495
Playing time in minutes :55

Process exited after 20.66 seconds with return value


0 Press any key to continue . . .
/*
EXPERIMENT NO. : 04
GROUP : B
TITLE : Write a C++ program that creates an output file, writes
information to it, closes the file and open it again as an input file and read
the information from the file.
ROLL NO. :
BATCH :
*/

#include <fstream>
#include
<iostream> using
namespace std;
void
writetofile();
void readfile();
int main()
{
int
ch; do
{
cout<<"Main Menu for file\
n"; cout<<"1.write to
file\n"; cout<<"2.read
from file\n";
cout<<"3.Exit\n";
cout<<"Enter your choice\
n"; cin>>ch;
switch(ch)
{
case 1:
writetofile();
break;
case 2:
readfile();
break;
case 3: break;
default:cout<<"wrong choice"<<endl<<endl;
}
}while(ch!=3);
return 0;
}
void writetofile()
{
int
n,i,id,sal;
char name[10];
ofstream
a_file;
a_file.open("C:\\Users\\IDEA\\Desktop\\abcd.txt",ios::app);
cout<<"Enter No of Records you want to
enter::\n"; cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter Id,Name and Salary of
Emp:\n"; cin>>id>>name>>sal;
a_file<<id<<"\t"<<name<<"\t"<<sal<<"\
n";
}
cout<<endl;
// Close the file stream
explicitly a_file.close();
}
void readfile()
{
int
id=0,sal=0;
char name[10];
ifstream
b_file;
b_file.open("C:\\Users\\IDEA\\Desktop\\abcd.txt",ios::in);
cout<<"ID\t"<<"Name"<<"\t"<<"Salary\n";
while(true)
{
b_file>>id>>name>>sa
l;
if( b_file.eof() )
break;
cout<<id<<"\t"<<name<<"\t"<<sal<<"\n";
}
cout<<endl;
b_file.close(
);
}
//
*******************************************************************************
**
/*
OUTPUT
:
Main Menu for
file 1.write to
file 2.read from
file 3.Exit
Enter your choice
1
Enter No of Records you want to enter::
3
enter Id,Name and Salary of Emp:
1 Atul 60000
enter Id,Name and Salary of Emp:
2 Pratik 55000
enter Id,Name and Salary of Emp:
3 Vishal 65000

Main Menu for


file 1.write to
file 2.read from
file 3.Exit
Enter your choice
2
ID Name Salary
1 Atul 60000
2 Pratik 55000
3 Vishal 65000

Main Menu for


file 1.write to
file 2.read from
file 3.Exit
Enter your choice
3

Process exited after 49.75 seconds with return value


0 Press any key to continue . . .
*/
/*
EXPERIMENT NO. : 05
GROUP : B
TITLE : Write a function template selection Sort. Write a program
that inputs, sorts and outputs an int array and a float array.
ROLL NO. :
BATCH :
*/

#include <iostream>
using namespace
std; template
<typename T>
//template <class
T> void
sort()
{
int i,
j,count; T
temp;
cout<<"\n How many no. u want to enter in array
: "; cin>>count;
T n[count];
cout<<"\n Enter" <<count<<" numbers :
"; for(i=0;i<count;i++)
{
cin>>n[i];
}
for(i=0;i<(count-1);i++)
{
for(j=i+1;j<count;j++)
{
if(n[i]>n[j])
{
temp=n[i]
;
n[i]=n[j]
;
n[j]=temp
;
}
}
}
cout<<"\n The array in the sorted order is : "<<endl;
for(i=0;i<count;i++)
{
cout<<"\t"<<n[i];
}
}
int main()
{
int
choice;
char ans;
do
{
cout<<"\n 1. Integer sort. \n 2. Float
sort."; cout<<"\n Enter the input you want
to sort : "; cin>>choice;
switch(choice)
{
case 1 : sort<int>();
break;
case 2 :
sort<float>();
break;
case 3 : cout<<"\n Invalid choice.";
break;
}
cout<<"\n Do u wish to continue
(Y/N)?"; cin>>ans;
}while(ans=='Y' ||
ans=='y'); return 0;
}
//
*******************************************************************************

/*
OUTPUT:

1. Integer sort.
2. Float sort.
Enter the input you want to sort : 1

How many no. u want to enter in array : 5

Enter5 numbers : 5
3
8
2
9

The array in the sorted order is :


2 3 5 8 9
Do u wish to continue (Y/N)?y

1. Integer sort.
2. Float sort.
Enter the input you want to sort : 2

How many no. u want to enter in array :

5 Enter5 numbers : 11.5


6.3
15.5
3.5
20.2

The array in the sorted order is :


3.5 6.3 11.5 15.5 20.2
Do u wish to continue (Y/N)?n

Process exited after 92.33 seconds with return value


0 Press any key to continue . . .
*/
/*
EXPERIMENT NO. : 06
GROUP : C
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.
ROLL NO. :
BATCH :
*/

#include <iostream> //standard input output stream header file


#include <algorithm> //The STL algorithms are generic because they
can
//operate on a variety of data structures
#include <vector> //The header file for the STL vector library is
vector.

using namespace std;


class Item //Item class is created
{
public: char name[10]; //varible
declaration int quantity;
int cost;
string
code;
//int code;

bool operator==(const Item& i1)


//Boolean operators allow you to create more
complex
//conditional statements
{
if(code==i1.code) //operator will return 1 if the comparison
is
//true, or 0 if the comparison is false
return 1;
return 0;
}
bool operator<(const Item& i1)
{
if(code<i1.code) //operator will return 1 if the
//comparison is true, or 0 if the comparison is
false
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)


{
//if (i1.name != i2.name) return i1.cost <
i2.cost; 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;
}
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.";
els
e
{ o1.erase(p); cout<<"\
nDeleted.";

//
*******************************************************************************
*
/*
OUTPUT
:

***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:1

Enter Item

Name:Mouse Enter

Item Quantity:5

Enter Item Cost:100

Enter Item Code:101


***** Menu *****
1.Insert
2.Displa
y
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:1

Enter Item Name:CPU

Enter Item

Quantity:5 Enter

Item Cost:15000

Enter Item Code:102

***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:1

Enter Item

Name:Monitor Enter

Item Quantity:5 Enter

Item Cost:8000 Enter

Item Code:103

***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:1
Enter Item

Name:Keyboard
Enter Ite Quantity:
m 5
Enter Ite Cost:250
m
Enter Ite Code:104
m
***** Men *****
u
1.Insert
2.Displa
y
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:1

Enter Item

Name:Speaker Enter

Item Quantity:5 Enter

Item Cost:1000 Enter

Item Code:105

***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:2

Item
Name:Mouse
Item
Quantity:5
Item Cost:100
Item Code:101

Item Name:CPU
Item
Quantity:5
Item
Cost:15000
Item Code:102
Item
Name:Monitor
Item Quantity:5
Item Cost:8000
Item Code:103

Item
Name:Keyboard
Item Quantity:5
Item Cost:250
Item Code:104

Item
Name:Speaker
Item Quantity:5
Item Cost:1000
Item Code:105
***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:3

Enter Item Code to

search:103 Found.
***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:4

Sorted on Cost

Item
Name:Mouse
Item
Quantity:5
Item Cost:100
Item Code:101
Item
Name:Keyboard
Item Quantity:5
Item Cost:250
Item Code:104

Item
Name:Speaker
Item Quantity:5
Item Cost:1000
Item Code:105

Item
Name:Monitor
Item Quantity:5
Item Cost:8000
Item Code:103

Item Name:CPU
Item
Quantity:5
Item
Cost:15000
Item Code:102
***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:5

Enter Item Code to

delete:105 Deleted.
***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:2
Item Name:Mouse
Item
Quantity:5
Item Cost:100
Item Code:101

Item
Name:Keyboard
Item Quantity:5
Item Cost:250
Item Code:104

Item
Name:Monitor
Item Quantity:5
Item Cost:8000
Item Code:103

Item Name:CPU
Item
Quantity:5
Item
Cost:15000
Item Code:102
***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:6

Process exited after 181.5 seconds with return value


0 Press any key to continue . . .
*/
/*
EXPERIMENT NO. : 07
GROUP : C
TITLE : 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.
ROLL NO. :
BATCH :
*/

/*
*/

#include
<iostream>
#include <map>
#include <string>
#include <utility>
using namespace
std;

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

populationMap.insert(pair<string, int>("China", 1339));


populationMap.insert(pair<string, int>("India", 1187));
populationMap.insert(mapType::value_type("US", 310));
populationMap.insert(mapType::value_type("Indonesia", 234));
populationMap.insert(make_pair("Brasil", 193));
populationMap.insert(make_pair("Pakistan", 170));

// Erase the end element using the erase function


// Because it's ordered map (by key),
// map elements are not in the order of the entry
// In this map it's US since it's ordered alphabetically.
mapType::iterator iter = --populationMap.end();
//or map< string,int>::iterator itr = --
populationMap.end(); populationMap.erase(iter);

// output the size of the map


cout << "Size of populationMap: " << populationMap.size() << '\n';

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


{
cout << iter->first <<": "<< iter->second << " million\n";
}

// find will return an iterator to the matching element if it is found


// or to the end of the map if the key is not found
string country ="Indonesia";
iter = populationMap.find(country);

if( iter != populationMap.end() )


cout << country <<"'s populations is "<< iter->second
<< "
million\n";
else
cout << "Key is not in populationMap" << '\n';

// clear the entries in the


map populationMap.clear();
}
//
*******************************************************************************
*
/*
OUTPUT:
Size of
populationMap: 5
Brasil: 193 million
China: 1339 million
India: 1187 million
Indonesia: 234 million
Pakistan: 170 million
Indonesia's populations is 234 million

Process exited after 0.1496 seconds with return value


0 Press any key to continue . . .
*/

You might also like