Oops Journal
Oops Journal
Class: FYMCA
Division: B
Roll no.: B-121 Assignment- 1
#include<iostream>
using namespace std;
class Complex{
private:
float real;
float imag;
public:
Complex():real(0),imag(0){}
Complex(float r,float i){
real=r;
imag=i;
}
Complex operator+(const Complex& t){
return Complex(real + t.real , imag + t.imag);
}
Complex operator*(const Complex& t){
float r=(real*t.real-imag*t.imag);
float i=(real*t.imag+imag*t.real);
return Complex(r,i);
}
friend ostream& operator<<(ostream& out,Complex& c){
out<<c.real;
if(c.imag>=0){
out<<"+"<<c.imag<<"i";
}else{
out<<"-"<<-c.imag<<"i";
}
return out;
}
friend istream& operator>>(istream& in, Complex& c){
in>>c.real>>c.imag;
return in;
}
};
int main(){
Complex c1,c2,result;
cout<<"Enter first Complex Number : ";
cin>>c1;
cout<<"Enter second Complex Number : ";
cin>>c2;
result=c1+c2;
cout<<"Sum of the Complex numbers is : "<<result<<endl;
result=c1*c2;
cout<<"Product of the complex numbers is : "<<result;
return 0;
}
Name: Khadija Asif Gharatkar Subject: OOP
Class: FYMCA
Division: B
Roll no.: B-121 Assignment- 2
#include <iostream>
#include <cmath>
#include <tuple>
using namespace std;
class Quadratic {
private:
double a, b, c; // Coefficients of the quadratic equation ax^2 + bx + c
public:
Quadratic() : a(0), b(0), c(0) {}
Quadratic(double a, double b, double c) : a(a), b(b), c(c) {}
Quadratic operator+(const Quadratic& other) const {
return Quadratic(a + other.a, b + other.b, c + other.c);
}
friend ostream& operator<<(ostream& os, const Quadratic& q) {
os << q.a << "x^2 ";
if (q.b >= 0) os << "+ " << q.b << "x ";
else os << "- " << -q.b << "x ";
if (q.c >= 0) os << "+ " << q.c;
else os << "- " << -q.c;
return os;
}
friend istream& operator>>(istream& is, Quadratic& q) {
cout << "Enter coefficients (a, b, c): ";
is >> q.a >> q.b >> q.c;
return is;
}
double eval(double x) const {
return a * x * x + b * x + c;
}
tuple<bool, double, double> solve() const {
if (a == 0) {
if (b == 0) {
return {false, 0, 0};
}
return {true, -c / b, -c / b};
}
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
return {false, 0, 0};
}
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
return {true, root1, root2};
}
};
int main() {
Quadratic q1, q2;
cout << "Enter first quadratic polynomial:" << endl;
cin >> q1;
cout << "Enter second quadratic polynomial:" << endl;
cin >> q2;
Quadratic sum = q1 + q2;
cout << "Sum of the polynomials: " << sum << endl;
double x;
cout << "Enter a value for x to evaluate the first polynomial: ";
cin >> x;
cout << "q1(" << x << ") = " << q1.eval(x) << endl;
auto [hasSolutions, root1, root2] = q1.solve();
if (hasSolutions) {
cout << "Solutions of the first polynomial: " << root1 << " and " << root2 << endl;
} else {
cout << "The first polynomial has no real solutions." << endl;
}
return 0;
}
Output:
Name: Khadija Asif Gharatkar Subject: OOP
Class: FYMCA
Division: B
Roll no.: B-121 Assignment- 3
#include <iostream>
using namespace std;
class CppArray {
private:
int* arr;
int size;
public:
CppArray(int s = 0) : size(s) {
arr = new int[size];
}
CppArray(const CppArray& other) : size(other.size) {
arr = new int[size];
for (int i = 0; i < size; ++i) {
arr[i] = other.arr[i];
}
}
CppArray& operator=(const CppArray& other) {
if (this != &other) {
delete[] arr;
size = other.size;
arr = new int[size];
for (int i = 0; i < size; ++i) {
arr[i] = other.arr[i];
}
}
return *this;
}
int& operator[](int index) {
if (index < 0 || index >= size) {
throw out_of_range("Index out of bounds.");
}
return arr[index];
}
int getSize() const {
return size;
}
friend istream& operator>>(istream& in, CppArray& cppArr) {
cout << "Enter " << cppArr.size << " elements: ";
for (int i = 0; i < cppArr.size; ++i) {
in >> cppArr.arr[i];
}
return in;
}
friend ostream& operator<<(ostream& out, const CppArray& cppArr) {
out << "[";
#include <iostream>
using namespace std;
int main() {
Name: Khadija Asif Gharatkar Subject: OOP
Class: FYMCA
Division: B
Roll no.: B-121 Assignment- 4
char choice = 'y';
while (choice == 'y' || choice == 'Y') {
double num1, num2, result;
char op;
cout << "Enter first number, operator, second number: ";
cin >> num1 >> op >> num2;
switch (op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
cout << "Error: Division by zero is not allowed.\n";
continue;
}
break;
default:
cout << "Error: Invalid operator.\n";
continue;
}
cout << "Answer = " << result << endl;
cout << "Do another (y/n)? ";
cin >> choice;
}
cout << "Exiting\n";
return 0;
}
Output:
#include <iostream>
Name: Khadija Asif Gharatkar Subject: OOP
Class: FYMCA
Division: B
Roll no.: B-121 Assignment- 5
#include <string>
using namespace std;
class Student {
private:
string* name;
int rollNo;
string className;
char division;
string dob;
string bloodGroup;
string contactAddress;
int telephoneNo;
string drivingLicenseNo;
static int studentCount;
public:
Student()
: name(new string("Unknown")), rollNo(0), className("N/A"), division('N'), dob("N/A"),
bloodGroup("N/A"), contactAddress("N/A"), telephoneNo(0), drivingLicenseNo("N/A") {
studentCount++;
}
Student(const string& name, int rollNo, const string& className, char division, const
string& dob,
const string& bloodGroup, const string& contactAddress, long long telephoneNo,
const string& drivingLicenseNo)
: name(new string(name)), rollNo(rollNo), className(className), division(division),
dob(dob),
bloodGroup(bloodGroup), contactAddress(contactAddress),
telephoneNo(telephoneNo), drivingLicenseNo(drivingLicenseNo) {
studentCount++;
}
Student(const Student& other)
: name(new string(*other.name)), rollNo(other.rollNo), className(other.className),
division(other.division), dob(other.dob), bloodGroup(other.bloodGroup),
contactAddress(other.contactAddress), telephoneNo(other.telephoneNo),
drivingLicenseNo(other.drivingLicenseNo) {
studentCount++;
}
~Student() {
delete name;
studentCount--;
}
static int getStudentCount() {
return studentCount;
}
friend void displayStudent(const Student& s);
inline void updateContactAddress(const string& newAddress) {
contactAddress = newAddress;
}
void inputDetails() {
cout << "Enter Name: ";
cin.ignore(); // Clear input buffer
getline(cin, *name);
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Class: ";
cin >> className;
cout << "Enter Division: ";
cin >> division;
cout << "Enter Date of Birth (dd/mm/yyyy): ";
cin >> dob;
cout << "Enter Blood Group: ";
cin >> bloodGroup;
cout << "Enter Contact Address: ";
cin.ignore();
getline(cin, contactAddress);
cout << "Enter Telephone Number: ";
cin >> telephoneNo;
cout << "Enter Driving License Number: ";
cin >> drivingLicenseNo;
}
void displayDetails() const {
cout << "\nStudent Details:\n";
cout << "Name: " << *name << "\nRoll Number: " << rollNo
<< "\nClass: " << className << "\nDivision: " << division
<< "\nDate of Birth: " << dob << "\nBlood Group: " << bloodGroup
<< "\nContact Address: " << contactAddress
<< "\nTelephone Number: " << telephoneNo
<< "\nDriving License Number: " << drivingLicenseNo << endl;
}
};
int Student::studentCount = 0;
void displayStudent(const Student& s) {
cout << "\nFriend Function - Student Details:\n";
cout << "Name: " << *s.name << "\nRoll Number: " << s.rollNo
<< "\nClass: " << s.className << "\nDivision: " << s.division
<< "\nDate of Birth: " << s.dob << "\nBlood Group: " << s.bloodGroup
<< "\nContact Address: " << s.contactAddress
<< "\nTelephone Number: " << s.telephoneNo
<< "\nDriving License Number: " << s.drivingLicenseNo << endl;
}
int main() {
cout << "Creating student database...\n";
Student* student1 = new Student("Kais", 51, "FYMCA", 'B', "10/06/2003", "A+", "123
Street, Pune City", 9999999999, "DL12345");
student1->displayDetails();
Student student2;
student2.inputDetails();
student2.displayDetails();
Student student3 = student2;
cout << "\nCopy Constructor: ";
student3.displayDetails();
displayStudent(student2);
student2.updateContactAddress("New Address, City");
cout << "\nUpdated Address: ";
student2.displayDetails();
cout << "\nTotal Students: " << Student::getStudentCount() << endl;
delete student1;
cout << "\nAfter deleting student1, Total Students: " << Student::getStudentCount() <<
endl;
return 0;
}
Output:
Student Details:
Name: Kais
Roll Number: 51
Class: FYMCA
Division: B
Date of Birth: 10/06/2003
Blood Group: A+
Contact Address: 123 Street, Pune City
Telephone Number: 1410065407
Driving License Number: DL12345
Enter Name: Kais
Enter Roll Number: 51
Enter Class: fymca
Enter Division: b
Enter Date of Birth (dd/mm/yyyy): 10/06/2003
Enter Blood Group: b+
Enter Contact Address: pune
Enter Telephone Number: 1212121212
Enter Driving License Number: dl99999
Student Details:
Name: Kais
Roll Number: 51
Class: fymca
Division: b
Date of Birth: 10/06/2003
Blood Group: b+
Contact Address: pune
Telephone Number: 1212121212
Driving License Number: dl99999
Copy Constructor:
Student Details:
Name: Kais
Roll Number: 51
Class: fymca
Division: b
Date of Birth: 10/06/2003
Blood Group: b+
Contact Address: pune
Telephone Number: 1212121212
Driving License Number: dl99999
Updated Address:
Student Details:
Name: Kais
Roll Number: 51
Class: fymca
Division: b
Date of Birth: 10/06/2003
Blood Group: b+
Contact Address: New Address, City
Telephone Number: 1212121212
Driving License Number: dl99999
Total Students: 3
After deleting student1, Total Students: 2
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Vector {
private:
vector<T> vec;
public:
Vector(int size, T defaultValue = T()) {
vec.resize(size, defaultValue);
}
void modify(int index, T value) {
Name: Khadija Asif Gharatkar Subject: OOP
Class: FYMCA
Division: B
Roll no.: B-121 Assignment- 6
if (index >= 0 && index < vec.size()) {
vec[index] = value;
} else {
cout << "Error: Index out of bounds!" << endl;
}
}
void multiplyByScalar(T scalar) {
for (int i = 0; i < vec.size(); i++) {
vec[i] *= scalar;
}
}
void display() const {
cout << "(";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i];
if (i != vec.size() - 1) {
cout << ", ";
}
}
cout << ")" << endl;
}
};
int main() {
Vector<int> intVec(5);
cout << "Initial vector: ";
intVec.display();
intVec.modify(2, 100);
cout << "After modification: ";
intVec.display();
intVec.multiplyByScalar(2);
cout << "After multiplying by scalar 2: ";
intVec.display();
Vector<double> doubleVec(4, 1.5);
Output:
#include <iostream>
#include <cmath>
using namespace std;
class RationalNumber {
private:
int numerator;
int denominator;
int gcd(int a, int b) const {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
Name: Khadija Asif Gharatkar Subject: OOP
Class: FYMCA
Division: B
Roll no.: B-121 Assignment- 7
}
void simplify() {
int divisor = gcd(abs(numerator), abs(denominator)); // GCD of absolute values
numerator /= divisor;
denominator /= divisor;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
public:
RationalNumber(int num = 0, int denom = 1) {
if (denom == 0) {
cout << "Error: Denominator cannot be zero!" << endl;
numerator = 0;
denominator = 1;
} else {
numerator = num;
denominator = denom;
simplify();
}
}
RationalNumber operator+(const RationalNumber& other) const {
int num = numerator * other.denominator + other.numerator * denominator;
int denom = denominator * other.denominator;
return RationalNumber(num, denom);
}
RationalNumber operator-(const RationalNumber& other) const {
int num = numerator * other.denominator - other.numerator * denominator;
int denom = denominator * other.denominator;
return RationalNumber(num, denom);
}
RationalNumber operator*(const RationalNumber& other) const {
int num = numerator * other.numerator;
int denom = denominator * other.denominator;
return RationalNumber(num, denom);
}
RationalNumber operator/(const RationalNumber& other) const {
if (other.numerator == 0) {
cout << "Error: Division by zero!" << endl;
return RationalNumber(0, 1);
}
int num = numerator * other.denominator;
int denom = denominator * other.numerator;
return RationalNumber(num, denom);
}
bool operator==(const RationalNumber& other) const {
return numerator == other.numerator && denominator == other.denominator;
}
bool operator!=(const RationalNumber& other) const {
return !(*this == other);
}
bool operator<(const RationalNumber& other) const {
return numerator * other.denominator < other.numerator * denominator;
}
bool operator>(const RationalNumber& other) const {
return other < *this;
}
bool operator<=(const RationalNumber& other) const {
return !(*this > other);
}
bool operator>=(const RationalNumber& other) const {
return !(*this < other);
}
void display() const {
cout << numerator << "/" << denominator;
}
};
int main() {
RationalNumber r1(4, 6);
RationalNumber r2(3, 4);
cout << "r1: ";
r1.display();
cout << endl;
cout << "r2: ";
r2.display();
cout << endl;
RationalNumber r3 = r1 + r2;
cout << "r1 + r2: ";
r3.display(); // 17/12
cout << endl;
RationalNumber r4 = r1 - r2;
cout << "r1 - r2: ";
r4.display(); // -1/12
cout << endl;
RationalNumber r5 = r1 * r2;
cout << "r1 * r2: ";
r5.display();
cout << endl;
RationalNumber r6 = r1 / r2;
cout << "r1 / r2: ";
r6.display();
cout << endl;
cout << "r1 == r2: " << (r1 == r2) << endl;
cout << "r1 != r2: " << (r1 != r2) << endl;
cout << "r1 < r2: " << (r1 < r2) << endl;
cout << "r1 > r2: " << (r1 > r2) << endl;
cout << "r1 <= r2: " << (r1 <= r2) << endl;
cout << "r1 >= r2: " << (r1 >= r2) << endl;
return 0;
}
Output:
#include <iostream>
#include <string>
using namespace std;
class Publication {
protected:
string title;
float price;
public:
Publication(string t = "", float p = 0.0) : title(t), price(p) {}
void display() const {
cout << "Title: " << title << endl;
cout << "Price: $" << price << endl;
}
void setData(const string& t, float p) {
if (p < 0) {
cout << "Error: Price cannot be negative." << endl;
price = 0.0;
} else {
title = t;
price = p;
}
}
Name: Khadija Asif Gharatkar Subject: OOP
Class: FYMCA
Division: B
Roll no.: B-121 Assignment- 8
};
class Book : public Publication {
private:
int pageCount;
public:
Book(string t = "", float p = 0.0, int pages = 0) : Publication(t, p), pageCount(pages) {}
void display() const {
Publication::display();
cout << "Page Count: " << pageCount << endl;
}
void setData(const string& t, float p, int pages) {
Publication::setData(t, p);
if (pages < 0) {
cout << "Error: Page count cannot be negative." << endl;
pageCount = 0;
} else {
pageCount = pages;
}
}
};
class Tape : public Publication {
private:
float playTime;
public:
Tape(string t = "", float p = 0.0, float time = 0.0) : Publication(t, p), playTime(time) {}
void display() const {
Publication::display();
cout << "Playing Time: " << playTime << " minutes" << endl;
}
void setData(const string& t, float p, float time) {
Publication::setData(t, p);
if (time < 0) {
cout << "Error: Playing time cannot be negative." << endl;
playTime = 0.0;
} else {
playTime = time;
}
}
};
int main() {
string title;
float price;
int pageCount;
float playTime;
cout << "Enter book title: ";
getline(cin, title);
cout << "Enter book price: ";
cin >> price;
cout << "Enter page count: ";
cin >> pageCount;
cin.ignore();
Book book;
book.setData(title, price, pageCount);
cout << "\nEnter tape title: ";
getline(cin, title);
cout << "Enter tape price: ";
cin >> price;
cout << "Enter playing time (in minutes): ";
cin >> playTime;
Tape tape;
tape.setData(title, price, playTime);
cout << "\nBook Details" << endl;
book.display();
cout << "\nTape Details" << endl;
tape.display();
return 0;
}
Output:
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
Output:
Name: Shaikh Mohammad Kais Subject: OOP
Class: FYMCA Subject-in-charge: Vaishali Ma’am
Division: B
Roll no.: B-151 Assignment- 9
Number of lines not starting with 'A': 3
Name: Khadija Asif Gharatkar Subject: OOP
Class: FYMCA
Division: B
Roll no.: B-121 Assignment- 10
#include <iostream>
using namespace std;
class Convert {
protected:
double val1;
double val2;
public:
Convert(double v = 0.0) : val1(v), val2(0.0) {}
double getinit() const {
return val1;
}
double getconv() const {
return val2;
}
virtual void compute() = 0;
virtual ~Convert() {}
};
class FahrenheitToCelsius : public Convert {
public:
FahrenheitToCelsius(double f) : Convert(f) {}
void compute() override {
val2 = (val1 - 32) * 5.0 / 9.0;
}
};
class InchesToCentimeters : public Convert {
public:
InchesToCentimeters(double i) : Convert(i) {}
void compute() override {
val2 = val1 * 2.54;
}
};
class PoundsToKilograms : public Convert {
public:
PoundsToKilograms(double p) : Convert(p) {}
void compute() override {
val2 = val1 * 0.453592;
}
};
int main() {
double value;
cout << "Enter temperature in Fahrenheit: ";
cin >> value;
FahrenheitToCelsius temp1(value);
temp1.compute();
Name: Shaikh Mohammad Kais Subject: OOP
Class: FYMCA Subject-in-charge: Vaishali Ma’am
Division: B
Roll no.: B-151 Assignment- 10
cout << "Fahrenheit: " << temp1.getinit() << " -> Celsius: " << temp1.getconv() << endl;
cout << "\nEnter length in Inches: ";
cin >> value;
InchesToCentimeters length1(value);
length1.compute();
cout << "Inches: " << length1.getinit() << " -> Centimeters: " << length1.getconv() << endl;
cout << "\nEnter weight in Pounds: ";
cin >> value;
PoundsToKilograms weight1(value);
weight1.compute();
cout << "Pounds: " << weight1.getinit() << " -> Kilograms: " << weight1.getconv() << endl;
return 0;
}
Output: