OOPSOUTPUTTEJU
OOPSOUTPUTTEJU
ROLL NO.: 61
Practical No.:01
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).
4. Overload operators << and >> to print and read Complex Numbers
Code:
# include<iostream>
double real;
double img;
public:
friend istream & operator >> (istream &, Complex &); // Input
friend ostream & operator << (ostream &, const Complex &); // Output
};
real = 0;
img = 0;
return cin;
}
cout << d.real << " + " << d.img << "i" << endl;
return cout;
Complex temp;
return temp;
Complex tmp;
return tmp;
int main() {
int flag = 1;
char b;
while (flag == 1)
cout << "Enter Real and Imaginary part of the Complex Number 1 : \n";
cout << "Enter Real and Imaginary part of the Complex Number 2 : \n";
int f = 1;
while (f == 1)
int a;
cin >> a;
if (a == 1)
C3 = C1+C2;
cout << "Do you wan to perform another operation (y/n) : \n";
cin >> b;
if (b == 'y' || b == 'Y')
f=1;
} else
flag=0;
f=0;
} else if (a == 2)
C4 = C1 * C2;
cout << "Do you wan to perform another operation (y/n) : \n";
cin >> b;
if (b == 'y' || b == 'Y')
f=1;
} else
flag=0;
f=0;
} else
flag=0;
f=0; } } }
return 0;
Output:
NAME: TEJAS MALI
ROLL NO.: 61
Practical No.:02
code:
#include<iostream>
#include<string.h>
class Data
string address;
public:
Data();
~Data();
void input_student_data();
void display_student_data();
};
class Student
string Name;
int Roll;
string Class;
char *Div;
string dob;
char *bg;
public:
Student();
~Student();
return count;
};
Data::Data()
Data::~Data()
delete telephone;
delete dl_no;
void Data::input_student_data()
cin.ignore();
getline(cin,address);
cin>>*telephone;
cout<<"Driving License Number : ";
cin>>*dl_no;
void Data::display_student_data()
Student::Student()
Roll = 0;
dob = "dd/mm/yyyy";
bg = new char[5];
Student::~Student()
delete Div;
delete[] bg;
cout<<"Name : ";
cin.ignore();
getline(cin,Name);
cin>>Roll;
cout<<"Class : ";
cin.ignore();
getline(cin,Class);
cout<<"Division : ";
cin>>Div;
cin.ignore();
getline(cin,dob);
cin>>bg;
stud1->input_student_data();
count++;
cout<<"Name : "<<Name<<endl;
cout<<"Class : "<<Class<<endl;
cout<<"Division : "<<Div<<endl;
stud2->display_student_data();
cout<<" \n";
int Student::count;
int main()
Student *st1[100];
Data *st2[100];
int a;
int s=0;
char ch;
cout<<" MENU \n";
cin>>a;
if(a==1)
do
st1[s]->input_data(st2[s]);
s++;
cin>>ch;
}while(ch=='y' || ch=='Y');
else if(a==2)
cout<<"**************************StudentDatabase****************************"<<endl;
for(int i=0;i<s;i++)
st1[i]->display_data(st2[i]);
else
cout<<"WRONG CHOICE\n";
return 0;
}
Output:
NAME: TEJAS MALI
ROLL NO.: 61
Practical No.:03
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 publication. From this class
derive two classes: book, which adds a page count (type integer), 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.
Code:
# include<iostream>
# include<stdio.h>
class publication
private:
string title;
float price;
public:
void add()
cin.ignore();
getline(cin, title);
void display()
};
private:
int page_count;
public:
void add_book()
try
add();
if (page_count <= 0)
throw page_count;
catch(...)
page_count = 0;
void display_book()
display();
page_count;
cout << "\n \n";
};
private:
float play_time;
public:
void add_tape()
try
add();
if (play_time <= 0)
throw play_time;
catch(...)
play_time = 0;
void display_tape()
display();
}
};
int main()
book b1[10];
tape t1[10];
do
switch(ch)
case 1:
b1[b_count].add_book();
b_count ++;
break;
case 2:
t1[t_count].add_tape();
t_count ++;
break;
case 3:
{
b1[j].display_book();
break;
case 4:
t1[j].display_tape();
break;
case 5:
exit(0);
return 0;
Output:
NAME: TEJAS MALI
ROLL NO.: 61
Practical No.:04
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.
Code:
#include<iostream>
#include<fstream>
class Employee
string Name;
int ID;
double salary;
public:
void accept()
cin.ignore();
getline(cin,Name);
cout<<"\n Id : ";
cin>>ID;
cin>>salary;
void display()
cout<<"\n Id : "<<ID;
}
};
int main()
Employee o[5];
fstream f;
int i,n;
f.open("demo.txt");
cin>>n;
for(i=0;i<n;i++)
o[i].accept();
f.write((char*)&o[i],sizeof o[i]);
f.close();
f.open("demo.txt",ios::in);
for(i=0;i<n;i++)
cout<<"\nEmployee "<<i+1<<"\n";
f.write((char*)&o[i],sizeof o[i]);
o[i].display();
f.close();
return 0;
}
Output:
NAME: TEJAS MALI
ROLL NO.: 61
Practical No.:05
Write a function template for selection sort that inputs, sorts and outputs an integer array and
float array.
Code:
#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<<"\n3. Exit";
cin>>ch;
switch(ch)
case 1:
cin>>n;
for(i=0;i<n;i++)
{
cin>>A[i];
sel(A);
break;
case 2:
cout<<"\nEnter total no of float elements:";
cin>>n;
for(i=0;i<n;i++)
cin>>B[i];
sel(B);
break;
case 3:
exit(0);
}while(ch!=3);
return 0;
Output:
NAME: TEJAS MALI
ROLL NO.: 61
Practical No.:06
Write C++ Program using STL for sorting and searching user defined
Code:
#include <vector> //The header file for the STL vector library is vector.
public:
char name[10];
int quantity;
int cost;
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;
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 display();
void insert();
void search();
void dlt();
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);
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<<"\n\n";
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!!!";
}
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:
NAME: TEJAS MALI
ROLL NO.: 61
Practical No.:07
Write a program in C++ to use map associative contain 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 than looks in the map, using the state name as an index and
returns the population of the state.
Code:
#include <iostream>
#include <map>
#include <string>
#include <utility>
int main()
mapType populationMap;
populationMap.insert(mapType::value_type("Bihar", 120));
populationMap.insert(make_pair("Rajasthan", 78));
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("Uttarakhand", 12));
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("Mizoram", 1));
populationMap.insert(make_pair("Sikkim", 1));
populationMap.insert(make_pair("UT Dadra and Nagar Haveli and Daman and Diu", 1));
populationMap.erase(iter);
cout << "Total state and UT of India with Size of populationMap: " << populationMap.size() << '\n';
{
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);
else
cin>>c;
}while(c=='y'||c=='Y');
populationMap.clear();
return 0;
}
Output: