0% found this document useful (0 votes)
76 views37 pages

Oops 20 To 26

Uploaded by

rajkirdath369
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)
76 views37 pages

Oops 20 To 26

Uploaded by

rajkirdath369
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/ 37

Pratical 20.

1
#include<iostream.h>
#include<conio.h>

class complex
{
public:
double real,imag;
complex(double r=0.0,double i=0.0):real(r),imag(i) {}

friend complex operator+(const complex& c1,const complex& c2);


void print()
{
cout<<"\n";
cout<<" Add of two complex numbers using friend function"<<endl;
cout<<"\n";
cout<<" The Result :- "<<real<<" + "<<imag<<"i"<<endl;
}
};
complex operator+(const complex& c1,const complex& c2)
{
complex result;
result.real=c1.real+c2.real;
result.imag=c1.imag+c2.imag;
return result;
}

void main()
{
clrscr();
complex c1(2,3),c2(4,5);
complex c3=c1+c2;
c3.print();
getch();
}
OUPUT:-

Add of two complex numbers using friend function


The Result :- 6 + 8i
Pratical 20.2
#include<iostream.h>
#include<conio.h>

class complex
{
public:
double real,imag;
complex(double r=0.0,double i=0.0):real(r),imag(i) {}

complex operator-(const complex& other)


{
complex result;
result.real=this->real-other.real;
result.imag=this->imag-other.imag;
return result;
}
void print()
{
cout<<" subtract of two complex numbers using friend function"<<endl;
cout<<" The Result : "<<real<<" + "<<imag<<"i"<<endl;
}
};

void main()
{
clrscr();
complex c1(7,8),c2(3,4);
complex c3=c1-c2;
c3.print();
getch();
}
OUPUT:-

Subtract of two complex number using friend fuction


The Result : 4 + 4i
Pratical 20.3
#include <iostream>
#include <cstring>
#include <vector>

class BookTitle {
private:
char* title;

public:
// Constructor
BookTitle(const char* t = nullptr) {
if (t) {
title = new char[strlen(t) + 1];
strcpy(title, t);
} else {
title = new char[1];
title[0] = '\0';
}
}

// Destructor
~BookTitle() {
delete[] title;
}

// Copy constructor
BookTitle(const BookTitle& other) {
title = new char[strlen(other.title) + 1];
strcpy(title, other.title);
}

// Overloading the == operator


bool operator==(const BookTitle& other) const {
return strcmp(title, other.title) == 0;
}

// Function to display the title


void display() const {
std::cout << title;
}
};

class Library {
private:
std::vector<BookTitle> books;

public:
void addBook(const BookTitle& book) {
books.push_back(book);
}

bool isBookAvailable(const BookTitle& searchTitle) const {


for (const auto& book : books) {
if (book == searchTitle) {
return true;
}
}
return false;
}
};

int main() {
Library library;

// Adding books to the library


library.addBook(BookTitle("The Great Gatsby"));
library.addBook(BookTitle("To Kill a Mockingbird"));
library.addBook(BookTitle("1984"));
library.addBook(BookTitle("Pride and Prejudice"));

// Searching for books


BookTitle search1("1984");
BookTitle search2("The Catcher in the Rye");

std::cout << "Searching for '1984': ";


if (library.isBookAvailable(search1)) {
std::cout << "Available\n";
} else {
std::cout << "Not Available\n";
}

std::cout << "Searching for 'The Catcher in the Rye': ";


if (library.isBookAvailable(search2)) {
std::cout << "Available\n";
} else {
std::cout << "Not Available\n";
}

return 0;
}
OUTPUT:-

Searching for '1984': Available

Searching for 'The Catcher in the Rye': Not Available


Pratical 21.1
#include<iostream.h>
#include<string.h>
#include<conio.h>

class Parent
{
public:
virtual void read()
{
cout << " Reading data in Parent class." << endl;
}
};

class Child : public Parent


{
public:
void read()
{
cout<<"\n";
cout << " Reading data in Child class." << endl;
}
};

void main()
{
Parent* ptr;
Parent p;
Child c;
clrscr();
ptr = &p;
ptr->read();

ptr = &c;
ptr->read();
getch();
}
OUPUT:-

Reading data in Parent class.

Reading data in Child class.


21.2
#include<iostream.h>
#include<conio.h>

class Animal
{
public:
virtual void sound()
{
cout << "This is an animal sound." << endl;
}
};
class Dog : public Animal
{
public:
void sound()
{
cout << "The dog barks: Woof Woof!" << endl;
}
};
class Cat : public Animal
{
public:
void sound()
{
cout << "The cat meows: Meow Meow!" << endl;
}
};

void main()
{
Animal* animalPtr;
Dog dog;
Cat cat;
animalPtr = &dog;
animalPtr->sound();
animalPtr = &cat;
animalPtr->sound();
getch();
}
OUPUT:-

The dog barks: Woof Woof!


The cat meows: Meow Meow!
Pratical 22
#include <iostream>
#include <string>
#include <vector>

// Abstract base class


class PaymentMethod {
protected:
double amount;

public:
PaymentMethod(double amt) : amount(amt) {}

// Pure virtual function making this an abstract class


virtual void processPayment() = 0;

// Virtual destructor
virtual ~PaymentMethod() {}

// Common method for all payment methods


void setAmount(double amt) {
amount = amt;
}

double getAmount() const {


return amount;
}
};

// Concrete class for credit card payments


class CreditCardPayment : public PaymentMethod {
private:
std::string cardNumber;
std::string expiryDate;

public:
CreditCardPayment(double amt, const std::string& number, const std::string& expiry)
: PaymentMethod(amt), cardNumber(number), expiryDate(expiry) {}

void processPayment() override {


std::cout << "Processing Credit Card payment of $" << amount << std::endl;
std::cout << "Card Number: " << cardNumber << ", Expiry: " << expiryDate <<
std::endl;
std::cout << "Payment successful!" << std::endl;
}
};

// Concrete class for PayPal payments


class PayPalPayment : public PaymentMethod {
private:
std::string email;

public:
PayPalPayment(double amt, const std::string& emailAddress)
: PaymentMethod(amt), email(emailAddress) {}

void processPayment() override {


std::cout << "Processing PayPal payment of $" << amount << std::endl;
std::cout << "PayPal Account: " << email << std::endl;
std::cout << "Payment successful!" << std::endl;
}
};

// Concrete class for bank transfer payments


class BankTransferPayment : public PaymentMethod {
private:
std::string accountNumber;
std::string bankCode;

public:
BankTransferPayment(double amt, const std::string& account, const std::string&
code)
: PaymentMethod(amt), accountNumber(account), bankCode(code) {}

void processPayment() override {


std::cout << "Processing Bank Transfer payment of $" << amount << std::endl;
std::cout << "Account Number: " << accountNumber << ", Bank Code: " <<
bankCode << std::endl;
std::cout << "Payment successful!" << std::endl;
}
};

// Online store class


class OnlineStore {
private:
std::vector<PaymentMethod*> payments;

public:
void addPayment(PaymentMethod* payment) {
payments.push_back(payment);
}

void processPayments() {
for (auto payment : payments) {
payment->processPayment();
std::cout << "------------------------" << std::endl;
}
}

~OnlineStore() {
for (auto payment : payments) {
delete payment;
}
}
};

int main() {
OnlineStore store;

// Adding different types of payments


store.addPayment(new CreditCardPayment(100.50, "1234-5678-9012-3456",
"12/25"));
store.addPayment(new PayPalPayment(75.25, "[email protected]"));
store.addPayment(new BankTransferPayment(200.00, "987654321", "BANKUS123"));

// Processing all payments


store.processPayments();

return 0;
}
OUTPUT:-

Processing Credit Card payment of $100.5 Card Number: 1234-5678-


9012-3456, Expiry: 12/25
Payment successful!
Processing PayPal payment of $75.25
PayPal Account: [email protected]
Payment successful!
Processing Bank Transfer payment of $200 Account Number:
987654321, Bank Code: BANKUS123 Payment successful!
Pratical 23.1

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>

int main()
{
ifstream file("example.txt");

if (!file.is_open())
{
cout << "Error: Could not open the file!" << endl;

getch();
}
OUTPUT:-

ERROR!
Error: Could not open the file!
Pratical 23.2
#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
std::string path = "."; // Current directory

std::cout << "Files in the current directory:" << std::endl;

try {
for (const auto& entry : fs::directory_iterator(path)) {
if (fs::is_regular_file(entry.status())) {
std::cout << entry.path().filename().string() << std::endl;
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
}

return 0;
}
OUTPUT:-

Files in the current directory:


.bash_logout
.bashrc
.profile
Pratical24.1
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
string sourcefilename,destinationfilename;
cout<<"Enter the name of the source file: ";
cin>>sourcefilename;
cout<<"Enter the name of the destination file:";
cin>>destinationfilename;

ifstream sourcefile(sourcefilename);
if(!sourcefile)
{
cerr<<"Error: could not open source file "<<sourcefilename<<endl;
return 1;
}

ofstream destinationfile(destinationfilename);
if(!destinationfile)
{
cout<<"Error:could not open destination file"<<destinationfilename<<endl;
return 1;
}

string line;
while(getline(sourcefile,line) ) {
destinationfile<<line<<endl;
}
sourcefile.close();
destinationfile.close();
cout<<"file content copied sucessfully !"<<endl;
return 0;
}
OUTPUT:-
Enter the name of the source file: aryan
Enter the name of the destination file: aryan
ERROR!
Error: could not open source file aryan
Pratical 24.2
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
string sourcefilename,destinationfilename;

cout<<"Enter the name of the source file: ";


cin>>sourcefilename;
cout<<"Enter th name of the destination file: ";
cin>>destinationfilename;

ifstream sourcefile(sourcefilename,ios::binary);

if(!sourcefile)
{
cerr<<"Error : could not open source file"<<sourcefilename<<endl;
}

ofstream destinationfile(destinationfilename,ios::binary);

if(!destinationfile)
{
cerr<<"Error : could not open destination file "<<destinationfilename<<endl;
return 1;
}

destinationfile<<sourcefile.rdbuf();

sourcefile.close();
destinationfile.close();

cout<<"file contendts copied successfully !"<<endl;


return 0;
}
OUTPUT:-

Enter the name of the source file: aryan


Enter th name of the destination file: aryan
Error : could not open source filearyan
Error : could not open destination file aryan
Pratical 25.1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");

if (!inputFile)
{
cerr << "Error opening input file." <<endl;
return 1;
}

if (!outputFile) {
cerr << "Error opening output file." <<endl;
return 1;
}

string line;
while (getline(inputFile, line))
{
outputFile << line <<endl;
}

inputFile.close();
outputFile.close();

cout << "File content copied successfully." <<endl;


return 0;

}
OUTPUT:-

Error opening input file.


Pratical 25.2
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
string sourceFile, destFile;
ifstream sourceStream;
ofstream destStream;
string line;
cout << "Enter source file name: ";
getline(cin, sourceFile);

sourceStream.open(sourceFile);
if (!sourceStream) {
cerr << "Error: Unable to open source file " << sourceFile << endl;
return 1;
}

cout << "Enter destination file name: ";


getline(cin, destFile);

destStream.open(destFile);
if (!destStream) {
cerr << "Error: Unable to create destination file " << destFile << endl;
return 1;
}

while (getline(sourceStream, line)) {


destStream << line << endl;
}
sourceStream.close();
destStream.close();
cout << "File copied successfully." << endl;
return 0;
OUTPUT:-

Enter source file name: aryan


ERROR!
Error: Unable to open source file Aryan
Pratical 26.1
#include <iostream>
#include <fstream>
#include <string>

class Person {
public:
std::string name;
int age;

// Constructor
Person(std::string n, int a) : name(n), age(a) {}

// Default constructor
Person() : name(""), age(0) {}

// Function to display person details


void display() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};

// Function to write object to binary file


void writeToFile(const std::string& filename, const Person& person) {
std::ofstream outFile(filename, std::ios::binary);
if (!outFile) {
std::cerr << "Error opening file for writing!" << std::endl;
return;
}

// Write name length and name


size_t nameLength = person.name.size();
outFile.write(reinterpret_cast<const char*>(&nameLength),
sizeof(nameLength));
outFile.write(person.name.c_str(), nameLength);

// Write age
outFile.write(reinterpret_cast<const char*>(&person.age), sizeof(person.age));

outFile.close();
}

// Function to read object from binary file


Person readFromFile(const std::string& filename) {
std::ifstream inFile(filename, std::ios::binary);
if (!inFile) {
std::cerr << "Error opening file for reading!" << std::endl;
return Person();
}

Person person;
size_t nameLength;
inFile.read(reinterpret_cast<char*>(&nameLength), sizeof(nameLength));

person.name.resize(nameLength);
inFile.read(&person.name[0], nameLength);

inFile.read(reinterpret_cast<char*>(&person.age), sizeof(person.age));

inFile.close();
return person;
}

int main() {
Person p1("John doe", 30);
std::string filename = "person.dat";
writeToFile(filename, p1);
std::cout << "Writing to file..." << std::endl;
std::cout << "Name: " << p1.name << ", Age: " << p1.age << std::end
Person p2 = readFromFile(filename);
std::cout << "\nReading from file..." << std::endl;
p2.display();
return 0;
}
OUTPUT:-

Writing to file...
Name: John doe, Age: 30

Reading from file...


Name: John doe, Age: 30
Pratical 26.2
#include <iostream>
#include <fstream>

void copyBinaryFile(const std::string& sourceFilename, const


std::string& destFilename) {
std::ifstream sourceFile(sourceFilename, std::ios::binary);
std::ofstream destFile(destFilename, std::ios::binary);

if (!sourceFile) {
std::cerr << "Error opening source file for reading!" << std::endl;
return;
}

if (!destFile) {
std::cerr << "Error opening destination file for writing!" <<
std::endl;
return;
}

// Buffer to hold data during copy


char buffer[1024];
size_t bytesRead;

// Read from source and write to destination


while (sourceFile) {
sourceFile.read(buffer, sizeof(buffer));
bytesRead = sourceFile.gcount(); // Get the number of bytes read
destFile.write(buffer, bytesRead);
}

sourceFile.close();
destFile.close();
std::cout << "File copied successfully from " << sourceFilename << "
to " << destFilename << std::endl;
}

int main() {
std::string sourceFilename = "source.dat"; // Ensure this file exists
std::string destFilename = "destination.dat";

// Call the function to copy the binary file


copyBinaryFile(sourceFilename, destFilename);

return 0;
}
OUTPUT:-

File copied successfully from source.dat to


destination.dat
Pratical 12
#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

class Area
{
protected:
float length, breadth;
public:
float area()
{
return length * breadth;
}
};

class Perimeter
{
protected:
float length, breadth;

public:
float perimeter()
{
return 2 * (length + breadth);
}
};
class Rectangle : public Area ,public perimeter
{
public:
void get_data()
{
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the breadth of the rectangle: ";
cin >> breadth;
}
void display()
{
cout << "Length: " << length << ", Breadth: " << breadth <<endl;
cout << "Area of Rectangle: " << area() <<endl;
cout << "Perimeter of Rectangle: " << perimeter() <<endl;
}
};

int main()
{
Rectangle rect;
rect.get_data();
rect.display();

return 0;
}
OUTPUT:-

Length : 5 , breadth : 5

Area of rectangle : 25
Perimeter of rectangle : 20

You might also like