OOPCGL
OOPCGL
OOPCGL
PRACTICAL NO:1
Implement a class Complex which represents the Complex Number data type.
0+0i).
4. Overload operators << and >> to print and read Complex Numbers.
PROGRAM
#include <iostream>
class Complex {
private:
public:
//constructor
friend ostream & operator << (ostream &out, Complex const &obj);
//overloading of + operator
Complex res;
return res;
//overloading of * operator
{
Complex res;
res.real=((real)*(obj.real))-((imag)*(obj.imag));
res.imag=((real)*(obj.imag))+((obj.real)*(imag));
return res;
};
return out;
in >> obj.real;
in >> obj.imag;
return in;
int main()
Complex c1,c2,c3,c4;
//Addition
c3 = c1+c2;
//Multiplication
c4 = c1 * c2;
cout << "\n The Multiplication Is :" <<c4 <<endl;
return 0;
OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------------------
PRACTICAL NO .2
Develop an object oriented program in C++ to create a database of student information system
containing the following information: Name, Roll number, Class, division, Date of Birth, Blood group,
Contact address, telephone number, driving license no. etc . Construct the database with suitable
member functions for initializing and destroying the data viz constructor, default constructor, Copy
constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic
PROGRAM
#include<iostream> //header file used for cin and cout
public:
};
char div;
int roll_no;
long mob;
public:
void create(per_info &);//TO CREATE DATABASE AND PASSED REFERENCE OF PERSONAL INFO OBJECT
cnt++;
//INLINE FUNCTION//
};
name="ANAGHA NIRGUDE";
year="SE COMPUTER";
div='A';
roll_no=21042;
mob=942329999;
lic="ABD45656";
dob="02/11/1997";
bldgrp="A-";
this->year=obj.year;
this->div=obj.div;
this->roll_no=obj.roll_no;
this->mob=obj.mob;
lic=obj.lic;
dob=obj.dob;
bldgrp=obj.bldgrp;
{
cout<<"\nNAME : "<<endl;
cin>>name;
cout<<"\nADDRESS : "<<endl;
cin>>address;
cin>>obj.dob;
cout<<"\nYEAR : "<<endl;
cin>>year;
cout<<"\nDIVISION: "<<endl;
cin>>div;
cin>>roll_no;
cin>>obj.bldgrp;
cin>>obj.lic;
cin>>mob;
cout<<"\n***********************"<<endl;
cout<<"\nYEAR : "<<year<<endl;
cout<<"\nDIVISION : "<<div<<endl;
cout<<"\nROLL NO : "<<roll_no<<endl;
int main()
int ch;
char ans;
cin>>n;
cout<<"\n***********************"<<endl;
student *sobj=new student[n]; //NEW IS DYNAMIC MEMORY ALLOCATION OPERATOR, sobj IS POINTER OF
TYPE STUDENT AND IT IS A ARRAY OF OBJECT OF SIZE n
do
cin>>ch;
switch(ch)
for(int i=0;i<n;i++)
sobj[i].create(pobj[i]);
sobj[i].inccnt();
break;
{
sobj[0].showcnt(); //to display the total count of students
//we can use any object of student class as it is static member function
for(int i=0;i<n;i++)
sobj[i].display(pobj[i]);
break;
student obj1;
per_info obj2;
obj1.create(obj2);
obj3.display(obj4);
break;
obj1.display(obj2);
break;
cin>>ans;
}while(ans=='y') ;
return 0;
OUTPUT
ENTER NO OF STUDENTS :
***********************
Menu
1. Create Database
2. Display Database
3. Copy Constructor
4. Default Constructor
5. Delete (Destructor)
NAME :
PRERNA GAIKWAD
ADDRESS :
DATE OF BIRTH :
10.08.2005
YEAR :
2024
DIVISION:
A
ROLL NUMBER :
12
BLOOD GROUP :
A+
LICEINCE NUMBER :
535362272J
PHONE NUMBER :
9834219493
Want to continue:(y/n)y
Menu
1. Create Database
2. Display Database
3. Copy Constructor
4. Default Constructor
5. Delete (Destructor)
***********************
YEAR : 2024
DIVISION : A
ROLL NO : 12
BLOOD GROUP : A+
PRACTICAL NO.3
> 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
> 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.
PROGRAM
#include<iostream>
#include<string>
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()
cin>>title;
cin>>price;
void putdata(void)
};
int pagecount;
public:
book()
pagecount=0;
{
pagecount=pc;
void getdata(void)
cout <<"Enter Book Page Count :"; //Acquire book data from user
cin>> pagecount;
void putdata(void)
};
float time1;
public:
CD()
time1=0.0;
time1=tim;
void getdata(void)
publication::getdata();
cin>> time1;
}
void putdata(void)
publication::putdata();
};
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();
b.getdata();
c.getdata();
b.putdata();
c.putdata();
return 0;
OUTPUT
Book data
CD Data
---------------------------------------------------------------------------------------------------------------------------------------------------
PRACTICAL NO 4
Write a C++ program that creates an output file, writes information to it, closes the file, open it again as an
input file and read the information from the file.
#include<iostream>
#include<fstream>
class test
public:
void writedata();
void readdata();
};
void test::writedata()
fstream fp;
char ch;
fp.open("it.txt",ios::out);
cin>>ch;
while (ch!='.')
fp.put(ch);
cin>>ch;
fp.close();
void test::readdata()
fstream fp;
char ch;
fp.open("it.txt",ios::in);
ch=fp.get();
while(!fp.eof())
cout<<ch;
ch=fp.get();
fp.close();
int main()
test ob;
int ch;
do
cout<<"\n1.Write\n2.Read";
cin>>ch;
switch(ch)
{
case 1:
ob.writedata();
break;
case 2:
ob.readdata();
break;
}while(ch<3);
OUTPUT
1.Write
2.Read
PRACTICAL NO 5
Write a function template for selection sort that inputs, sorts and outputs an integer array and
a float array.
PROGRAM
#include<iostream>
int n;
#define size 10
template<class T>
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 A[size];
float B[size];
int i;
int ch;
do
cout<<"\n--------------------MENU-----------------------";
cout<<"\n3. Exit";
cin>>ch;
switch(ch)
case 1:
cin>>n;
cout<<"\nEnter int elements:";
for(i=0;i<n;i++)
cin>>A[i];
sel(A);
break;
case 2:
cin>>n;
for(i=0;i<n;i++)
cin>>B[i];
sel(B);
break;
case 3:
exit(0);
}while(ch!=3);
return 0;
OUTPUT
* * * * * SELECTION SORT SYSTEM * * * * *
--------------------MENU-----------------------
1. Integer Values
2. Float Values
3. Exit
Sorted array: 0 2 8
--------------------MENU-----------------------
1. Integer Values
2. Float Values
3. Exit
0.1
8.88
--------------------MENU-----------------------
1. Integer Values
2. Float Values
3. Exit
PRACTICAL NO 6
Write C++ Program using STL for sorting and searching user defined records such as item records using vector
container.
PROGRAM
#include <iostream>
#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 display();
void insert();
void search();
void dlt();
{
return i1.cost < i2.cost;
int main()
int ch;
do
cout<<"\n1.Insert";
cout<<"\n2.Display";
cout<<"\n3.Search";
cout<<"\n4.Sort";
cout<<"\n5.Delete";
cout<<"\n6.Exit";
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;
cin>>i1.name;
cin>>i1.quantity;
cin>>i1.cost;
cin>>i1.code;
o1.push_back(i1);
void display()
for_each(o1.begin(),o1.end(),print);
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;
cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
cout<<"\nNot found.";
else
cout<<"\nFound."<<endl;
void dlt()
vector<Item>::iterator p;
Item i1;
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
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Item Name:laptop
Item Quantity:1
Item Cost:80000
Item Code:3536378
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Found.
Item Quantity : 1
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Sorted on Cost
Item Name:laptop
Item Quantity:1
Item Cost:80000
Item Code:3536378
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Deleted.
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
PRACTICAL NO 7
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.
PROGRAM
#include<iostream>
#include<map>
#include<string>
int main()
mapType populationMap;
mapType::iterator iter;
string state_name;
cin>>state_name;
iter = populationMap.find(state_name);
<<iter->second ;
else
populationMap.clear();
OUTPUT
========Population of states in India==========
Size of populationMap5