OOP Programs 2025
OOP Programs 2025
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
==>7
==>11+8i
#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() // destructor
{
cout<<"\n\n"<<this->name<<"(Object) is destroyed!\n";
}
};
int main()
{
int n,i;
db d1,*ptr[5]; cout<<"\
nDefault values:";
display(d1);
d1.getdata()
;
display(d1)
;
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
No ==> 9
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!
Shubham
SE Enter Division
==> A
No ==> 6
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
created: 3 Shubham(Object)
is destroyed!
Siddharth(Object) is
Kajal(Object) is destroyed!
#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;
#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
#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
Enter5 numbers : 5
3
8
2
9
1. Integer sort.
2. Float sort.
Enter the input you want to sort : 2
};
vector<Item> o1;
void print(Item
&i1); void
display();
void
insert();
void
search();
void dlt();
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);
}
//
*******************************************************************************
*
/*
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
Quantity:5 Enter
Item Cost:15000
***** Menu
***** 1.Insert
2.Display
3.Search
4.Sort
5.Delet
e
6.Exit
Enter your choice:1
Enter Item
Name:Monitor 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 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
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
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
/*
*/
#include
<iostream>
#include <map>
#include <string>
#include <utility>
using namespace
std;
int main()
{
typedef map<string, int> mapType; //map<string, int>
populationMap; mapType populationMap;