0% found this document useful (0 votes)
29 views17 pages

All Pratical Oop

Title nutrition or certain

Uploaded by

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

All Pratical Oop

Title nutrition or certain

Uploaded by

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

Practical no 1

#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex() : real(0), imag(0) {}
Complex(double r, double i) : real(r), imag(i) {}
Complex operator+(const Complex& other) {
return Complex(real + other.real, imag + other.imag);
}
Complex operator*(const Complex& other)
{
double r = real * other.real - imag * other.imag;
double i = real * other.imag + imag * other.real;
return Complex(r, i);
}
friend ostream& operator<<(ostream& os, const
Complex& c) {
os << c.real;
if (c.imag >= 0)
{
os << " + " << c.imag << "i";
}
else {
os << " - " << -c.imag << "i";
}
return os;
}
friend istream& operator>>(istream& is, Complex& c) {
is >> c.real >> c.imag;
return is;
}
};
int main()
{
Complex c1(3, 4), c2(1, 2);
Complex c3;
c3 = c1 + c2;
cout << "Sum: " << c3 << endl;
c3 = c1 * c2;
cout << "Product: " << c3 << endl;
cout << "Enter a complex number (real and imaginary
parts): ";
cin >> c3;
cout << "You entered: " << c3 << endl;
return 0;
}

Ritesh patil
Pratical No 2
#include <iostream>
#include <string>
using namespace std;
class StudData;
class Student {
string name, cls, division, dob, bloodgroup;
int roll_no;
static int count;
public:
Student() {
name = ""; roll_no = 0; cls = ""; division = ""; dob = "dd/mm/yyyy";
bloodgroup = "";
}
~Student() {}
static int getCount() { return count; }
void getData(StudData* st);
void dispData(StudData* st);
};
class StudData {
string caddress;
long telno, dlno;
friend class Student;
public:
StudData() : caddress(""), telno(0), dlno(0) {}
void getStudData() {
cout << "Enter Contact Address: "; cin.ignore(); 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: "; cin.ignore(); getline(cin, name);
cout << "Enter Roll Number: "; cin >> roll_no;
cout << "Enter Class: "; cin.ignore(); getline(cin, cls);
cout << "Enter Division: "; getline(cin, division);
cout << "Enter Date of Birth: "; getline(cin, dob);
cout << "Enter Blood Group: "; getline(cin, bloodgroup);
st->getStudData();
count++;
}
inline void Student::dispData(StudData* st) {
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;
st->dispStudData();
}
int Student::count = 0;
int main() {
Student* stud[100];
StudData* studData[100];
int n = 0;
char ch;
do {
stud[n] = new Student;
studData[n] = new StudData;
stud[n]->getData(studData[n]);
n++;
cout << "Do you want to add another student (y/n)? ";
cin >> ch;
} while (ch == 'y' || ch == 'Y');
for (int i = 0; i < n; i++) {
cout << "---------------------------------------------------\n";
stud[i]->dispData(studData[i]);
}
cout << "Total Students: " << Student::getCount() << endl;
for (int i = 0; i < n; i++) {
delete stud[i];
delete studData[i];
}

return 0;
}
Pratical No 3
#include <iostream>
#include <string>
using namespace std;
class publication {
private:
string title;
float price;
public:
void add() {
cout << "\nEnter the Publication Information:" << endl;
cout << "Enter Title of the Publication: ";
cin.ignore();
getline(cin, title);
cout << "Enter Price of Publication: ";
cin >> price;
}
void display() const {
cout << "\n--------------------------------------------------";
cout << "\nTitle of Publication: " << title;
cout << "\nPublication Price: " << price;
}
};
class book : public publication {
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! Resetting to 0.";
page_count = 0;
}
}
void display_book() const {
display();
cout << "\nPage 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 << "\nInvalid Play Time! Resetting to 0.";
play_time = 0;
}
}
void display_tape() const {
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 << "\n\nEnter your choice: ";
cin >> ch;
switch (ch) {
case 1:
b1[b_count].add_book();
b_count++; // Increment book count
break;
case 2:
t1[t_count].add_tape();
t_count++; // Increment tape count
break;
case 3:
cout << "\n* * * * BOOK PUBLICATION DATABASE * * * *";
for (int i = 0; i < b_count; i++) {
b1[i].display_book(); // Display all books
}
break;
case 4:
cout << "\n* * * * TAPE PUBLICATION DATABASE * * * *";
for (int i = 0; i < t_count; i++) {
t1[i].display_tape(); // Display all tapes
}
break;
case 5:
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (ch != 5);

return 0;
}
Pratical No 4
#include<iostream>
#include<fstream>
using namespace std;
class Employee
{
string Name;
int ID;
double salary;
public:
void accept()
{
cout<<"\n Name : ";
cin.ignore();
getline(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("demo.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 information of Employee "<<i+1<<"\n";
o[i].accept();
f.write((char*)&o[i],sizeof o[i]);
}
f.close();
f.open("demo.txt",ios::in);
cout<<"\n Information 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;
}
Pratical No 5
#include<iostream>
using namespace std;
#define SIZE 10
template<class T>
void sel(T A[], int n) {
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] << " ";
}
cout << endl;
}
int main() {
int A[SIZE], n, i;
float B[SIZE];
int ch;

do {
cout << "\n* * * * * SELECTION SORT SYSTEM * * * * *";
cout << "\n--------------------MENU-----------------------";
cout << "\n1. Integer Values";
cout << "\n2. Float Values";
cout << "\n3. Exit";
cout << "\n\nEnter your choice: ";
cin >> ch;
switch (ch) {
case 1:
cout << "\nEnter total number of integer elements: ";
cin >> n;
cout << "\nEnter integer elements: ";
for (i = 0; i < n; i++) {
cin >> A[i];
}
sel(A, n); // Call the selection sort function for integers
break;

case 2:
cout << "\nEnter total number of float elements: ";
cin >> n;
cout << "\nEnter float elements: ";
for (i = 0; i < n; i++) {
cin >> B[i];
}
sel(B, n); // Call the selection sort function for floats
break;

case 3:
cout << "Exiting the program." << endl;
break;

default:
cout << "Invalid choice. Please enter a valid option." << endl;
}
} while (ch != 3);

return 0;
}
Pratical No 6
#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> o1;
void print(Item &i1);
void display();
void insert();
void search();
void dlt();
bool compare(const Item &i1, const Item &i2)
{

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 Quantity : ";
cin>>i1.quantity;
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 Quantity : "<<i1.quantity;
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!!!";
}
}
Pratical No 7
#include <iostream>
#include <map>
using namespace std;
int main(){
typedef map<string,int> mapType;
mapType populationMap;
populationMap.insert(pair<string, float>("Maharashtra", 125));
populationMap.insert(pair<string, float>("Uttar Pradesh", 225));
populationMap.insert(mapType::value_type("Bihar", 120));
populationMap.insert(mapType::value_type("West Bengal", 100));
populationMap.insert(make_pair("Madhya Pradesh", 90));
populationMap.insert(make_pair("Rajasthan", 78));
populationMap.insert(make_pair("Andhra Pradesh", 53));
mapType::iterator iter = --populationMap.end();
populationMap.erase(iter);
cout << "Total state and UT of India with Size of populationMap: " <<
populationMap.size() << '\n';
for (iter = populationMap.begin(); iter != populationMap.end(); ++iter)
{
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);
if( iter != populationMap.end() )
cout << state <<"'s populations is "
<< iter->second << " million\n";
else
cout << "State is not in populationMap" << '\n';

cout<<"Do you wish to continue?(y/n):";


cin>>c;
}while(c=='y'||c=='Y');
populationMap.clear();
return 0; RiteshPatil

You might also like