0% found this document useful (0 votes)
40 views29 pages

OOP All Practicals With Output

Uploaded by

Mayuresh kadam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views29 pages

OOP All Practicals With Output

Uploaded by

Mayuresh kadam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Experiment No : 1

/*
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 mul ply two complex numbers.
4. Overload operators << and >> to print and read Complex Numbers */
# include<iostream>
using namespace std;
class Complex //decaring Class Complex
{
double real;
double img;
public:
Complex(); // Default Constructor
friend istream & operator >> (istream &, Complex &); // Input
friend ostream & operator << (ostream &, const Complex &); // Output
Complex operator + (Complex); // Addi on
Complex operator * (Complex); // Mul plica on
};
Complex::Complex() // Default Constructor
{
real = 0;
img = 0;
}
istream & operator >> (istream &, Complex & i)
{
cin >> i.real >> i.img;
return cin;
}
ostream & operator << (ostream &, const Complex & d)
{
cout << d.real << " + " << d.img << "i" << endl;
return cout;
}
Complex Complex::operator + (Complex c1) // Overloading + operator
{
Complex temp;
temp.real = real + c1.real;
temp.img = img + c1.img;
return temp;
}
Complex Complex::operator * (Complex c2) // Overloading * Operator
{
Complex tmp;
tmp.real = real * c2.real - img * c2.img;
tmp.img = real * c2.img + img * c2.real;
return tmp;
}
int main()
{
Complex C1, C2, C3, C4;

cout << "Enter Real and Imaginary part of the Complex Number 1 : \n";
cin >> C1;
cout << "Enter Real and Imaginary part of the Complex Number 2 : \n";
cin >> C2;

cout << "Complex Number 1 : " << C1 << endl;


cout << "Complex Number 2 : " << C2 << endl;

C3 = C1+C2;
cout << "Addi on : " << C3 << endl;

C4 = C1 * C2;
cout << "Mul plica on : " << C4 << endl;

return 0;
}
Experiment No : 1
Output :
Experiment No : 2
/*
Experiment Number 2 : Develop a program in C++ to create a database of student’s informa on system
containing the following informa on: Name, Roll number, Class, Division, Date of Birth, Blood group,
Contactaddress, Telephone number, Driving license no. and other. Construct the database with suitable
member func ons. Make use of constructor, default constructor, copy constructor, destructor, sta c member
func ons, friend class, this pointer, inline code and dynamic memory alloca on operators-new and delete as
well as excep on handling.
*/
#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;
sta c 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;
}
sta c int getCount()
{
return count;
}
void getData(StudData*);
void dispData(StudData*);
};
class StudData{
string caddress;
long int* telno;
long int* dlno;
friend class Student;
public:
StudData()
{
caddress="";
telno=new long;
dlno=new long;
}
~StudData()
{
delete telno;
delete dlno;
}
void getStudData()
{
cout<<"Enter Contact Address : ";
cin.get();
getline(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;
}
};

inline void Student::getData(StudData* st)


{
cout<<"Enter Student Name : ";
getline(cin,name);
cout<<"Enter Roll Number : ";
cin>>roll_no;
cout<<"Enter Class : ";
cin.get();
getline(cin,cls);
cout<<"Enter Division : ";
cin>>division;
cout<<"Enter Date of Birth : ";
cin.get();
getline(cin,dob);
cout<<"Enter Blood Group : ";
cin>>bloodgroup;
st->getStudData();
count++;
}
inline void Student::dispData(StudData* st1)
{
cout<<"Student Name : "<<name<<endl;
cout<<"Roll Number : "<<roll_no<<endl;
cout<<"Class : "<<cls<<endl;
cout<<"Division : "<<division<<endl;
cout<<"Date of Birth : "<<dob<<endl;
cout<<"Blood Group : "<<bloodgroup<<endl;
st1->dispStudData();
}
int Student::count;
int main()
{
Student* stud1;
StudData* stud2;
stud1 = new Student;
stud2 = new StudData;
stud1 -> getData(stud2);
cout<<"---------------------------------------------------------------"<<endl;
stud1->dispData(stud2);
cout<<"---------------------------------------------------------------"<<endl;
delete stud1;
delete stud2;
return 0;
}
Experiment No : 2
Output :
Experiment No : 3
/*
Imagine a publishing company which does marke ng for book and audiocasse e versions. Create a class
publica on that stores the tle (a string) and price (type float) of a publica on. From this class derive two
classes: book, which adds a page count(type int), and tape, which adds a playing me in minutes(type float).
Write a program that instan ates the book and tape classes, allows user to enter data and displays the data
members.If an excep on is caught, replace all the data member values with zero values.
*/
# include<iostream>
//# include<stdio.h>
using namespace std;
class publica on // declaring class Publica on
{
private:
string tle;
float price;
public:
void add()
{
cout << "\nEnter the Publica on informa on : " << endl;
cout << "Enter Title of the Publica on : ";
cin>> tle;
cout << "Enter Price of Publica on : ";
cin >> price;
}
void display()
{
cout << "\n--------------------------------------------------";
cout << "\nTitle of Publica on : " << tle;
cout << "\nPublica on Price : " << price;
}
};
class book : public publica on // declaring class book which inherits class publica on in public mode.
{
private:
int page_count;
public:
void add_book()
{
try
{
add();
cout << "Enter Page Count of Book : ";
cin >> page_count;
if (page_count <= 0)
{
throw page_count;
}
}
catch(...)
{
cout << "\nInvalid Page Count!!!";
page_count = 0;
}
}
void display_book()
{
display();
cout << "\nPage Count : " <<
page_count;
cout << "\n--------------------------------------------------\n";
}
};
class tape : public publica on // declaring class tape which inherits class publica on in public mode
{
private:
float play_ me;
public:
void add_tape()
{
try
{
add();
cout << "Enter Play Dura on of the Tape : ";
cin >> play_ me;
if (play_ me <= 0)
throw play_ me;
}
catch(...)
{
cout << "\nInvalid Play Time!!!";
play_ me = 0;
}
}
void display_tape()
{
display();
cout << "\nPlay Time : " <<
play_ me << " min";
cout << "\n--------------------------------------------------\n";
}
};

int main()
{
book b1; // object of class book
tape t1; // object of class tape
b1.add_book();
t1.add_tape();
cout << "\n* * * * BOOK PUBLICATION DATABASE SYSTEM * * * *";
b1.display_book();
cout << "\n* * * * TAPE PUBLICATION DATABASE SYSTEM * * * *";
t1.display_tape();
return 0;
}
Experiment No : 3
Output :
Experiment No : 4
/*
Write a C++ program that creates an output file, writes informa on to it, closes the file, open it again as an
input file and read the informa on from the file.
*/

#include<iostream>
#include<fstream>
using namespace std;
class Employee // declaring class employee
{
string Name;
int ID;
double salary;
public:
void accept()
{
cout<<"\n Name : ";
cin>>Name;
cout<<"\n Id : ";
cin>>ID;
cout<<"\n Salary : ";
cin>>salary;
}
void display()
{
cout<<"\n Name : "<<Name;
cout<<"\n Id : "<<ID;
cout<<"\n Salary : "<<salary<<endl;
}
};
int main()
{
Employee o[5];
fstream f;
int i,n;

f.open("aaa.txt",ios::out);
cout<<"\n Enter the number of employees you want to store : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n Enter informa on of Employee "<<i+1<<"\n";
o[i].accept();
f.write((char*)&o[i],sizeof o[i]);
}

f.close();

f.open("aaa.txt",ios::in);
cout<<"\n Informa on of Employees is as follows : \n";
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;
}
Experiment No : 4
Output :
Experiment No : 5
/*
Write a func on template for selec on sort that inputs, sorts and outputs an integer array and
a float array.
*/
#include<iostream>
using namespace std;
int n;
#define size 10
template<class T>
void sel(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<<"\n "<<A[i];
}
}
int main()
{
int A[size];
float B[size] ;
int i;
cout<<"\nEnter total no of int elements:";
cin>>n;
cout<<"\nEnter int elements:";
for(i=0;i<n;i++)
{
cin>>A[i];
}
sel(A);

cout<<"\nEnter total no of float elements:";


cin>>n;
cout<<"\nEnter float elements:";
for(i=0;i<n;i++)
{
cin>>B[i];
}
sel(B);
return 0;
}
Experiment No : 5
Output :
Experiment No : 6
/*
Write C++ Program using STL for sor ng and searching user defined records such as item records
using vector container.
*/
#include <iostream> //standard input output stream header file
#include <algorithm> //The STL algorithms are generic because they can operate on a variety of data
structures
#include <vector> //The header file for the STL vector library is vector.
using namespace std;
class Item // crea ng class Item
{
public:
char name[10];
int quan ty;
int cost;
int code;
bool operator==(const Item& i1) //Boolean operators allow you to create more complex condi onal
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;
}
bool operator<(const Item& i1)
{
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 print(Item &i1);
void display();
void insert();
void search();
void dlt();
bool compare(const Item &i1, const Item &i2)
{
//if (i1.name != i2.name) return i1.cost < i2.cost;
return i1.cost < i2.cost;
}
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 Quan ty : ";
cin>>i1.quan ty;
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);
}
void print(Item &i1)
{
cout<<"\n";
cout<<"\nItem Name : "<<i1.name;
cout<<"\nItem Quan ty : "<<i1.quan ty;
cout<<"\nItem Cost : "<<i1.cost;
cout<<"\nItem Code : "<<i1.code;
cout<<"\n\n";
}
void search()
{
vector<Item>::iterator p;
Item i1;
cout<<"\nEnter Item Code to search : ";
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;
cout<<"\nEnter Item Code to delete : ";
cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
{
cout<<"\nNot found!!!";
}
else
{
o1.erase(p);
cout<<"\nDeleted!!!";
}
}
Experiment No : 6
Output :
Experiment No : 7
/*
Write a program in C++ to use map associa ve container. The keys will be the names of states, and
the values will be the popula ons 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 popula on of the state.
*/

#include <iostream>
#include <map>
#include <string>
#include <u lity>

using namespace std;


int main()
{
typedef map<string,int> mapType;
mapType popula onMap;
popula onMap.insert(pair<string, float>("Maharashtra", 125));
popula onMap.insert(pair<string, float>("U ar Pradesh", 225));
popula onMap.insert(mapType::value_type("Bihar", 120));
popula onMap.insert(mapType::value_type("West Bengal", 100));
popula onMap.insert(make_pair("Madhya Pradesh", 90));
popula onMap.insert(make_pair("Tamil Nadu", 80));
popula onMap.insert(make_pair("Rajasthan", 78));
popula onMap.insert(make_pair("Andhra Pradesh", 53));
popula onMap.insert(make_pair("Odisha", 47));
popula onMap.insert(make_pair("Kerala", 38));
popula onMap.insert(make_pair("Telangana", 37));
popula onMap.insert(make_pair("Assam", 35));
popula onMap.insert(make_pair("Jharkhand", 38));
popula onMap.insert(make_pair("Karnataka", 68));
popula onMap.insert(make_pair("Gujarat", 70));
popula onMap.insert(make_pair("Punjab", 31));
popula onMap.insert(make_pair("Chha sgarh", 30));
popula onMap.insert(make_pair("Haryana", 29));
popula onMap.insert(make_pair("UT Delhi", 19));
popula onMap.insert(make_pair("UT Jammu and Kashmir", 14));
popula onMap.insert(make_pair("U arakhand", 12));
popula onMap.insert(make_pair("Himachal Pradesh", 8));
popula onMap.insert(make_pair("Tripura", 04));
popula onMap.insert(make_pair("Meghalaya", 4));
popula onMap.insert(make_pair("Manipur[", 3));
popula onMap.insert(make_pair("Nagaland", 2));
popula onMap.insert(make_pair("Goa", 2));
popula onMap.insert(make_pair("Arunachal Pradesh", 2));
popula onMap.insert(make_pair("UT Puducherry", 2));
popula onMap.insert(make_pair("Mizoram", 1));
popula onMap.insert(make_pair("UT Chandigarh", 1));
popula onMap.insert(make_pair("Sikkim", 1));
popula onMap.insert(make_pair("UT Dadra and Nagar Haveli and Daman and Diu", 1));
popula onMap.insert(make_pair("UT Andaman and Nicobar Islands", 1));
popula onMap.insert(make_pair("UT Lakshadweep", 0.0003));
popula onMap.insert(make_pair("UT Ladakh", 0.00006));
mapType::iterator iter = --popula onMap.end();
popula onMap.erase(iter);
cout << "Total state and UT of India with Size of popula onMap: "<< popula onMap.size() << '\n';
for (iter = popula onMap.begin(); iter != popula onMap.end(); ++iter)
{
cout << iter->first <<":" << iter->second << " million\n";
}
char c;
do
{
string state;
cout<<"\nEnter that state you want to know the popula on of: ";
cin>>state;
iter = popula onMap.find(state);
if( iter != popula onMap.end() )
cout << state <<"'s popula ons is "
<< iter->second << " million\n";
else
cout << "State is not in popula onMap" << '\n';
cout<<"Do you wish to con nue?(y/n):";
cin>>c;
}while(c=='y'||c=='Y');
popula onMap.clear();
return 0;
}
Experiment No : 7
Output :

You might also like