All Pratical Oop
All Pratical Oop
#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;
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)
{