Sanket Oop PDF
Sanket Oop PDF
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 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:
1. Addition
2. Substraction
3. Multiplication
Enter a choice that you want to perform : 1
Addition is : 7+6i
1. Addition
2. Substraction
3. Multiplication
Enter a choice that you want to perform : 2
Substraction is : 3+2i
1. Addition
2. Substraction
3. Multiplication
Enter a choice that you want to perform : 3
Multiplication is : 2+18i
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;
}
void display()
{
cout<<real<<"+"<<imaginary<<"i"<<endl;
}
};
return temp;
}
int main()
{
Complex c1(2.1,4.0), c2(3.0,4.0), c3;
c3 = operator +(c1,c2);
c1.display();
c2.display();
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";
}
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;
}
};
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
-----------------------------------
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;
}
};
void display()
{
cout<<"title :"<<title<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Pagecount :"<<pagecount<<endl;
}
};
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>
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
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>
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
Practical No.: 05
Title: Write a function template selection sort on integer and float array.
----------------------------------------------------------------------------
#include<iostream>
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:
Enter Choice : 1
Sorted array: 8 10 12 30 45
1. Integer
2. Float
3. Exit
Enter Choice : 2
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
Operations:
1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit
Operations:
1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit
Operations:
1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit
1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit
Sorted cost
Operations:
1. Insert/Add
2. Display
3. Search
4. Sort
5. Delete
6. Exit
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;
mapType::iterator iter;
iter = populationMap.find(state_name);
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