OOP Lab Assignments
OOP Lab Assignments
Implement a class Complex which represents the Complex Number data type. Implement the
following
⦁ Constructor (including a default constructor which creates the complex number 0+0i).
⦁ Overload operator+ to add two complex numbers.
⦁ Overload operator* to multiply two complex numbers.
● Overload operators << and >> to print and read Complex Numbers.
#include<iostream>
using namespace std;
class complex
{
private:
float x, y;
public:
complex()
{
x = 0;
y = 0;
}
complex operator+(complex &c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
complex operator*(complex &c)
{
complex temp;
temp.x= (x*c.x)-(y*c.y);
temp.y= (x*c.y)+(y*c.x);
return(temp);
}
friend istream &operator>>(istream &input,complex &c)
{
cout<<"Enter the real value: ";
input>>c.x;
cout<<"Enter the imaginary value: ";
input>>c.y;
}
friend ostream &operator<<(ostream &output,complex &c)
{
output<<c.x<<"+"<<c.y<<"i \n";
}
};
int main()
{
complex c1,c2,c3,c4;
cout<<"Default constructor value = "<<c1;
cout<<"Enter the 1st number "<<endl;
cin>>c1;
cout<<" Enter the 2nd number "<<endl;
cin>>c2;
c3=c1+c2;
c4=c1*c2;
cout<<"first number is: "<<c1<<endl;
cout<<"second number is: "<<c2<<endl;
cout<<"addition is :"<<c3<<endl;
cout<<"multiplication is: "<<c4<<endl;
return 0;
}
Assignment No 2
Develop a program in C++ 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
#include<iostream>
#include<string.h>
using namespace std;
#define max 100;
class per_info{
string lic, dob, bldgrp;
public:
per_info();
per_info(per_info &);
friend class student;
};
class student{
string name, address, year;
char divi;
int roll_no;
long mob;
static int cnt;
public:
void create(per_info &);
void display(per_info &);
inline static void inccnt(){
cnt++;
}
inline static void showcnt(){
cout<<"\nTOTAL NO OF RECORDS ARE : "<<cnt;
}
student();
student(student &);
~student(){
}
};
int student::cnt;
student::student()
{
name="ANAGHA";
address="PUNE";
year="2";
divi='A';
roll_no=42;
mob=942329999;
}
per_info::per_info(){
lic="4676745656";
dob="02/11/2002";
bldgrp="A-";
}
student::student(student &obj){
this->name=obj.name;
this->address=obj.address;
this->year=obj.year;
this->divi=obj.divi;
this->roll_no=obj.roll_no;
this->mob=obj.mob;
}
per_info::per_info(per_info &obj){
lic=obj.lic;
dob=obj.dob;
bldgrp=obj.bldgrp;
}
void student::create(per_info &obj){
cout<<"NAME: ";
cin>>name;
cout<<"ADDRESS: ";
cin>>address;
cout<<"DATE OF BIRTH: ";
cin>>obj.dob;
cout<<"YEAR: ";
cin>>year;
cout<<"DIVISION: ";
cin>>divi;
cout<<"ROLL NUMBER: ";
cin>>roll_no;
cout<<"BLOOD GROUP: ";
cin>>obj.bldgrp;
cout<<"LICENSE NUMBER: ";
cin>>obj.lic;
cout<<"PHONE NUMBER: ";
cin>>mob;
}
void student::display(per_info &obj){
cout<<"NAME OF STUDENT: "<<name<<endl;
cout<<"ADDRESS OF STUDENT: "<<address<<endl;
cout<<"DATE OF BIRTH: "<<obj.dob<<endl;
cout<<"YEAR: "<<year<<endl;
cout<<"DIVISION: "<<divi<<endl;
cout<<"ROLL NO: "<<roll_no<<endl;
cout<<"BLOOD GROUP: "<<obj.bldgrp<<endl;
cout<<"LICENSE NUMBER: "<<obj.lic<<endl;
cout<<"PHONE NUMBER: "<<mob<<endl;
}
int main(){
int n;
int ch;
cout<<"ENTER NO OF STUDENTS: ";
cin>>n;
student *sobj=new student[n];
per_info *pobj=new per_info[n];
do{
cout<<"\n Menu \n 1. Create Database \n 2. Display Databse \n 3.Exit";
cout<<"\n Enter your Choice: ";
cin>>ch;
switch(ch){
case 1:
{ for(int i=0;i<n;i++){
sobj[i].create(pobj[i]);
sobj[i].inccnt();
}
break;
}
case 2:
{ sobj[0].showcnt();
for(int i=0;i<n;i++){
sobj[i].display(pobj[i]);
}
break;
}
case 3:
{
cout<<"Exited"<<endl;
}
}
}
while(ch!=3);
return 0;
}
Assignment 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 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.
#include<iostream>
using namespace std;
class publication
{
public:
string title;
float price;
void add()
{
cout<<"enter publication information: ";
cout<<"\n enter title of publication: ";
cin>>title;
cout<<"\n enter price of publication: ";
cin>>price;
}
void display()
{
cout<<"\ntitle of publication: "<<title;
cout<<"\nprice of publication: "<<price;
}
};
class book: public publication
{
private:
int page_count;
public:
void add_book()
{
try
{
add();
cout<<"\n enter page count of book: ";
cin>>page_count;
if(page_count<=0)
throw page_count;
}
catch(...)
{
cout<<"invalid page count:";
title=" ";
price=0.0;
page_count=0;
}
}
void display_book()
{
display();
cout<<"\npage count is:"<<page_count<<endl;
}
};
class tape: public publication
{
private:
float play_time;
public:
void add_tape()
{
try
{
add();
cout<<"\n enter play duration of tape: ";
cin>>play_time;
if(play_time<=0)
throw play_time;
}
catch(...)
{
cout<<"invalid play_time:";
title=" ";
price=0.0;
play_time=0;
}
}
void display_tape()
{
display();
cout<<"\nplay time:"<<play_time<<"minutes\n"<<endl;
}
};
int main()
{
book b[10];
tape t[10];
int ch = 0,b_count = 0,t_count = 0;
while(1)
{
cout<<"\n publication system information ";
cout<<"\n 1. add information of book";
cout<<"\n 2. add information of tape";
cout<<"\n 3. display information of book";
cout<<"\n 4. display information of tape";
cout<<"\n 5. exit";
cout<<"\n enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
b[b_count].add_book();
b_count++;
break;
case 2:
t[t_count].add_tape();
t_count++;
break;
case 3:
cout<<"\npublication information system(book)";
for(int i=0;i<b_count;i++)
{
b[i].display_book();
}
break;
case 4:
cout<<"\npublication information system(tape)";
for(int i=0;i<t_count;i++)
{
t[i].display_tape();
}
break;
case 5:
exit(0);
}
}
return 0;
}
Assignment 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>
using namespace std;
int main()
{
ofstream outfile;
outfile.open("example.txt",ios::app);
string s;
while(1)
{
cout<<"Enter \n 1-Enter data \n 0-Exit"<<endl;
int ch;
cout<<"Enter option:"<<endl;
cin>>ch;
if (ch==1)
{ cin.get();
getline(cin,s);
outfile<<s<<endl;
}
else if(ch==0)
{
cout<<"Exited"<<endl;
break;
}
}
outfile.close();
ifstream infile;
infile.open("shardul.txt",ios::in);
cout<<"File Contains:"<<endl;
while(!infile.eof())
{
getline(infile,s);
cout<<s<<endl;
}
return 0;
}
Assignment No 5
//Write a function template for selection sort that inputs, sorts and outputs an integer
array and a float array
#include<iostream>
using namespace std;
template<typename t>
}
else if(ch==2)
{
cout<<"Enter the no. of elements in array:"<<endl;
cin>>n;
cout<<"Enter the Float array:"<<endl;
for(int i=0;i<n;i++)
{
cout<<"Enter the element at index "<<i<<":"<<endl;
cin>>float_array[i];
}
selection_sort(float_array,n);
}
else if(ch==3)
{
cout<<"Exited"<<endl;
break;
}
}
return 0;
}
Assignment No 6
/* 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<vector>
#include<algorithm>
using namespace std;
class items
{
private:
string name;
int cost;
int quantity;
public:
int code;
items(int c,string n,int cs,int q)
{
code=c;
name=n;
cost=cs;
quantity=q;
}
void display()
{
cout<<"Item
code="<<code<<"|Name="<<name<<"|Cost="<<cost<<"|Quantity="<<quantity<<endl;
}
};
vector<items>v1;
bool compare(const items &i1, const items &i2)
{
return i1.code < i2.code;
}
int main()
{
vector<items>v1;
int choice;
while(1)
{
cout<<"Enter\n 1.Insert Item \n 2. Display Items \n 3. Search Item \n 4. Sort Item \n 0.
Exit"<<endl;
cin>>choice;
if(choice==1)
{
int code,cost,quantity;
string name;
cout<<"Enter Item Details:(In below format)"<<endl;
cout<<"Code|Name|Cost|Quantity"<<endl;
cin>>code>>name>>cost>>quantity;
items item(code,name,cost,quantity);
v1.push_back(item);
}
else if(choice==2)
{
for (int i=0;i<v1.size();i++)
{
v1[i].display();
}
}
else if(choice==3)
{
int search;
cout<<"Enter the Item code to search:"<<endl;
cin>>search;
int flag=0;
for(int i=0;i<v1.size();i++)
{
if(v1[i].code==search)
{
v1[i].display();
flag=1;
break;
}
}
if(flag==0)
{
cout<<"Item is not found!"<<endl;
}
}
else if(choice==4)
{
sort(v1.begin(),v1.end(),compare);
cout<<"Sorted as per item Code:"<<endl;
}
else if(choice==0)
{
cout<<"Exited"<<endl;
break;
}
}
return 0;
}
Assignment 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
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
typedef map<string,int>maptype;
maptype populationMap; //create empty map
populationMap.insert(pair<string,int>("Maharashtra",12357646)),
populationMap.insert(pair<string,int>("Karnataka",23489612)),
populationMap.insert(pair<string,int>("Orissa",1432567)),
populationMap.insert(pair<string,int>("Goa",452367)),
populationMap.insert(pair<string,int>("Kerala",12357646)),
populationMap.insert(pair<string,int>("Madhya Pradesh",23489612)),
populationMap.insert(pair<string,int>("Uttar Pradesh",1432567)),
populationMap.insert(pair<string,int>("Gujarat",452367));
maptype::iterator iter;
cout<<"# # # Popultion of States # # #"<<endl;
cout<<"Size of Population Map is:"<<populationMap.size()<<endl;
string s;
cout<<"Enter the State Name:"<<endl;
cin>>s;
iter=populationMap.find(s);
if(iter!=populationMap.end())
cout<<s<<" has Population of "<<iter->second;
else
cout<<"Key is not found in Map"<<endl;
populationMap.clear();
return 0;
}