0% found this document useful (0 votes)
19 views20 pages

Oop Final

Uploaded by

dahaleatharva3
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)
19 views20 pages

Oop Final

Uploaded by

dahaleatharva3
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/ 20

PR 1

#include <iostream>

class Complex {
private:
double real;
double imaginary;

public:
Complex() : real(0.0), imaginary(0.0) {}
Complex(double r, double i) : real(r), imaginary(i) {}

Complex operator+(const Complex& other) const {


return Complex(real + other.real, imaginary + other.imaginary);
}

Complex operator*(const Complex& other) const {


double result_real = (real * other.real) - (imaginary * other.imaginary);
double result_imaginary = (real * other.imaginary) + (imaginary * other.real);
return Complex(result_real, result_imaginary);
}

friend std::ostream& operator<<(std::ostream& os, const Complex& complex) {


os << complex.real << " + " << complex.imaginary << "i";
return os;
}

friend std::istream& operator>>(std::istream& is, Complex& complex) {


std::cout << "Enter real part: ";
is >> complex.real;
std::cout << "Enter imaginary part: ";
is >> complex.imaginary;
return is;
}
};

int main() {
Complex c1, c2, result_add, result_multiply;

std::cout << "Enter first complex number:\n";


std::cin >> c1;

std::cout << "Enter second complex number:\n";


std::cin >> c2;

result_add = c1 + c2;
std::cout << "Sum of the complex numbers: " << result_add << std::endl;
result_multiply = c1 * c2;
std::cout << "Product of the complex numbers: " << result_multiply << std::endl;

return 0;
}

OUTPUT
Enter first complex number:
Enter real part: 10
Enter imaginary part: 20
Enter second complex number:
Enter real part: 30
Enter imaginary part: 14
Sum of the complex numbers: 40 + 34i
Product of the complex numbers: 20 + 740i
PR 2

#include <iostream>
#include <string.h>
using namespace std;
class person_additional_info
{
char address[20], license[20], insurance[20];
long int contact;

public:
person_additional_info()
{
strcpy(address, "XYZ");
strcpy(license, "XY-0000000000");
strcpy(insurance, "XY00000000X");
contact = 000000000;
}

~person_additional_info()
{
cout << "I am in Destructor";
}

friend class person;


};
class person
{
char name[20], dob[10], blood[10];
float ht, wt;
static int count;
person_additional_info *pai;

public:
person()
{
strcpy(name, "XYZ");
strcpy(dob, "dd/mm/yy");
strcpy(blood, "A +");
ht = 0;
wt = 0;
pai = new person_additional_info;
}
person(person *p1)
{
strcpy(name, p1->name);
strcpy(dob, p1->dob);
strcpy(blood, p1->blood);
ht = p1->ht;
wt = p1->wt;
pai = new person_additional_info;
strcpy(pai->address, p1->pai->address);
strcpy(pai->license, p1->pai->license);
strcpy(pai->insurance, p1->pai->insurance);
pai->contact = p1->pai->contact;
}
static void showcount()
{
cout << "\nNo of records count=" << count << "\n";
}
~person()
{
cout << "\nI am in Destructor\n";
}
void getdata(char name[20]);
inline void display();
};
void person::getdata(char name[20])
{
strcpy(this->name, name);
cout << "\n Enter date of birth : ";
cin >> dob;
cout << "\n Enter blood group : ";
cin >> blood;
cout << "\n Enter height : ";
cin >> ht;
cout << "\n Enter weight : ";
cin >> wt;
cout << "\n Enter address : ";
cin >> pai->address;
cout << "\n Enter Licence number : ";
cin >> pai->license;
cout << "\n Enter Insurance policy number : ";
cin >> pai->insurance;
cout << "\n Enter Contact number : ";
cin >> pai->contact;
count++;
}

void person::display()
{
cout << "\t" << name;
cout << "\t" << dob;
cout << "\t" << blood;
cout << "\t" << ht;
cout << "\t" << wt;
cout << "\t" << pai->address;
cout << "\t" << pai->license;
cout << "\t" << pai->insurance;
cout << "\t" << pai->contact;
}
int person::count;
int main()
{
person *p1, *p2;
int ch;
p1 = new person;
p2 = new person(p1);
cout << "\tName";
cout << "\tDob";
cout << "\t";
cout << "\tBlood";
cout << "\tHt";
cout << "\tWt";
cout << "\tAddress";
cout << "\tLicense";
cout << "\tInsurance";
cout << "\tContact";
cout << endl;
cout << "Default Constructor Value \n";
p1->display();
cout << "\n";
cout << "Copy Constructor Value \n";
p2->display();
int n;
cout << "\nEnter how many records you want??";
cin >> n;
person p3[n];
char name[20];
int x = 0;
do
{
cout << "\nWelcome to Personal database system";
cout << "\n1.Enter the record";
cout << "\n2.Display the record";
cout << "\n3.Exit";
cin >> ch;
switch (ch)
{
case 1:
{
cout << "\nEnter the Name ";
cin >> name;
p3[x].getdata(name);
person::showcount();
x++;
break;
}
case 2:
{
cout << "\tName";
cout << "\tDob";
cout << "\t Blood";
cout << "\tHt";
cout << "\tWt";
cout << "\tAddress";
cout << "\tLicense";
cout << "\tInsurance";
cout << "\tContact";
for (int i = 0; i < n; i++)
{
cout << "\n";
p3[i].display();
}
break;
}
}
} while (ch != 3);
delete p1;
delete p2;
return 0;
}

OUTPUT:
Name Dob Blood Ht Wt Address License Insurance Contact
Default Constructor Value
XYZ dd/mm/yy A+ 0 0 XYZ XY-0000000000 XY00000000X 0

Copy Constructor Value


XYZ dd/mm/yy A+ 0 0 XYZ XY-0000000000 XY00000000X 0

Enter how many records you want??2

Welcome to Personal database system


1.Enter the record
2.Display the record
3.Exit
1

Enter the Name John


Enter date of birth : 01/01/1990
Enter blood group : B+
Enter height : 175
Enter weight : 70
Enter address : 123 Main St
Enter Licence number : DL123456
Enter Insurance policy number : XYZ098765
Enter Contact number : 1234567890

No of records count=1

Welcome to Personal database system


1.Enter the record
2.Display the record
3.Exit
1

Enter the Name Alice


Enter date of birth : 05/05/1988
Enter blood group : AB-
Enter height : 160
Enter weight : 55
Enter address : 456 Elm St
Enter Licence number : DL654321
Enter Insurance policy number : ABC123456
Enter Contact number : 9876543210

No of records count=2

Welcome to Personal database system


1.Enter the record
2. Display the record
3. Exit
2
Name Dob Blood Ht Wt Address License Insurance Contact
XYZ dd/mm/yy A + 0 0 XYZ XY-0000000000 XY00000000X 0
XYZ dd/mm/yy A + 0 0 XYZ XY-0000000000 XY00000000X 0
John 01/01/1990 B+ 175 70 123 Main St DL123456 XYZ098765 1234567890
Alice 05/05/1988 AB- 160 55 456 Elm St DL654321 ABC123456 9876543210

Welcome to Personal database system


1.Enter the record
2. Display the record
3.Exit
3

I am in Destructor
I am in Destructor
I am in Destructor
I am in Destructor
I am in Destructor
I am in Destructor
PR 3

#include <iostream>
#include <string>
#include <limits>

class publication {
protected:
std::string title;
float price;

public:
void getdata() {
std::cout << "Enter title: ";
std::cin.ignore();
std::getline(std::cin, title);

std::cout << "Enter price: ";


std::cin >> price;

if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
title = "";
price = 0.0f;
throw "Invalid input! Price must be a number.";
}
}

virtual void display() {


std::cout << "\nTitle: " << title << "\nPrice: $" << price << std::endl;
}
};

class book : public publication {


private:
int pageCount;

public:
void getdata() {
publication::getdata();

std::cout << "Enter page count: ";


std::cin >> pageCount;

if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
pageCount = 0;
throw "Invalid input! Page count must be an integer.";
}
}

void display() override {


publication::display();
std::cout << "Page Count: " << pageCount << std::endl;
}
};

class tape : public publication {


private:
float playingTime;

public:
void getdata() {
publication::getdata();

std::cout << "Enter playing time in minutes: ";


std::cin >> playingTime;

if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
playingTime = 0.0f;
throw "Invalid input! Playing time must be a number.";
}
}

void display() override {


publication::display();
std::cout << "Playing Time (minutes): " << playingTime << std::endl;
}
};

int main() {
book bk;
tape tp;

try {
std::cout << "Enter Book Details:\n";
bk.getdata();

std::cout << "\nEnter Tape Details:\n";


tp.getdata();

std::cout << "\nBook Details:\n";


bk.display();

std::cout << "\nTape Details:\n";


tp.display();
} catch (const char* msg) {
std::cout << "Exception caught: " << msg << std::endl;
std::cout << "\nAll data members replaced with zero values.\n";

std::cout << "\nBook Details:\n";


bk.display();

std::cout << "\nTape Details:\n";


tp.display();
}

return 0;
}

OUTPUT:
Enter Book Details:
Enter title: The King
Enter price: 100
Enter page count: 256

Enter Tape Details:


Enter title: War
Enter price: 100
Enter playing time in minutes: 120

Book Details:

Title: he King
Price: $100
Page Count: 256

Tape Details:

Title: War
Price: $100
Playing Time (minutes): 120
PR 4

#include <iostream>
#include <fstream>

int main() {
std::ofstream outputFile("data.txt");
if (!outputFile.is_open()) {
std::cerr << "Unable to open the file for writing!" << std::endl;
return 1;
}

outputFile << "Hello, this is some data written to the file.\n";


outputFile << "Adding another line of data.\n";
outputFile << "And one more line for good measure.\n";

outputFile.close();

std::ifstream inputFile("data.txt");
if (!inputFile.is_open()) {
std::cerr << "Unable to open the file for reading!" << std::endl;
return 1;
}

std::cout << "Contents of the file:\n";


std::string line;
while (std::getline(inputFile, line)) {
std::cout << line << std::endl;
}
inputFile.close();

return 0;
}

OUTPUT:
Contents of the file:
Hello, this is some data written to the file.
Adding another line of data.
And one more line for good measure.
PR 5

#include <iostream>

template <typename T>


void selectionSort(T arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < size; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
std::swap(arr[i], arr[minIndex]);
}
}
}

int main() {
const int intSize = 5;
const int floatSize = 5;

int intArray[intSize];
float floatArray[floatSize];

std::cout << "Enter " << intSize << " integers:" << std::endl;
for (int i = 0; i < intSize; ++i) {
std::cin >> intArray[i];
}

std::cout << "Enter " << floatSize << " floats:" << std::endl;
for (int i = 0; i < floatSize; ++i) {
std::cin >> floatArray[i];
}

selectionSort(intArray, intSize);
std::cout << "\nSorted Integer Array:" << std::endl;
for (int i = 0; i < intSize; ++i) {
std::cout << intArray[i] << " ";
}
std::cout << std::endl;

selectionSort(floatArray, floatSize);
std::cout << "\nSorted Float Array:" << std::endl;
for (int i = 0; i < floatSize; ++i) {
std::cout << floatArray[i] << " ";
}
std::cout << std::endl;

return 0;
}

OUTPUT:

Enter 5 integers:
25314
Enter 5 floats:
1.2 3.2 4.4 5.4 1.1

Sorted Integer Array:


12345

Sorted Float Array:


1.1 1.2 3.2 4.4 5.4
PR 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;
}
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."<<endl;
cout<<"Item Name : "<<p ->name<<endl;
cout<<"Item Quantity : "<<p ->quantity<<endl;
cout<<"Item Cost : "<<p ->cost<<endl;
cout<<"Item Code: "<<p ->code<<endl;
}
}
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.";
}
}

OUTPUT:

*** Menu ***


1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:1

Enter Item Name:Pen

Enter Item Quantity:100

Enter Item Cost:5

Enter Item Code:1001

*** Menu ***


1.Insert
2.Display
3. Search
4.Sort
5.Delete
6.Exit
Enter your choice:1

Enter Item Name:Book

Enter Item Quantity:5

Enter Item Cost:125

Enter Item Code:1002

*** Menu ***


1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:2

Item Name:Pen
Item Quantity:100
Item Cost:5
Item Code:1001

Item Name:Book
Item Quantity:5
Item Cost:125
Item Code:1002
*** Menu ***
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:3

Enter Item Code to search:1001

Found.
Item Name : Pen
Item Quantity : 100
Item Cost : 5
Item Code: 1001

*** Menu ***


1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:4

Sorted on Cost

Item Name:Pen
Item Quantity:100
Item Cost:5
Item Code:1001

Item Name:Book
Item Quantity:5
Item Cost:125
Item Code:1002
*** Menu ***
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:5

Enter Item Code to delete:1001

Deleted.
*** Menu ***
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:2

Item Name:Book
Item Quantity:5
Item Cost:125
Item Code:1002
*** Menu ***
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:6
PR 7

#include <iostream>
#include <map>

using namespace std;

int main() {
map<string, int> statePopulations;
statePopulations["Uttar Pradesh"] = 224979000;
statePopulations["Maharashtra"] = 123144223;
statePopulations["Bihar"] = 123877477;
statePopulations["West Bengal"] = 99387755;
statePopulations["Madhya Pradesh"] = 85358965;

string stateName;
cout << "Enter the name of a state: ";
cin >> stateName;

int population = statePopulations[stateName];

cout << "The population of " << stateName << " is " << population << endl;

return 0;
}

OUTPUT:
Enter the name of a state: Maharashtra
The population of Maharashtra is 123144223

You might also like