Oop Final
Oop Final
#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) {}
int main() {
Complex c1, c2, result_add, result_multiply;
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";
}
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
No of records count=1
No of records count=2
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);
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.";
}
}
public:
void getdata() {
publication::getdata();
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.";
}
}
public:
void getdata() {
publication::getdata();
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.";
}
}
int main() {
book bk;
tape tp;
try {
std::cout << "Enter Book Details:\n";
bk.getdata();
return 0;
}
OUTPUT:
Enter Book Details:
Enter title: The King
Enter price: 100
Enter page count: 256
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.close();
std::ifstream inputFile("data.txt");
if (!inputFile.is_open()) {
std::cerr << "Unable to open the file for reading!" << std::endl;
return 1;
}
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>
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
#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)
{
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:
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
Found.
Item Name : Pen
Item Quantity : 100
Item Cost : 5
Item Code: 1001
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
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>
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;
cout << "The population of " << stateName << " is " << population << endl;
return 0;
}
OUTPUT:
Enter the name of a state: Maharashtra
The population of Maharashtra is 123144223