Assignment 3 Oop
Assignment 3 Oop
REG. ID 241446
SECTION BSAI-F-24-A
QUESTION NO 1:
#include <iostream>
using namespace std;
class Force2D {
protected:
float fx, fy;
public:
Force2D(float x = 0, float y = 0) : fx(x), fy(y) {}
void print() {
cout << "Force: (" << fx << ", " << fy << ")" << endl;
}
};
void print() {
cout << "Force: (" << fx << ", " << fy << ", " << fz << ")" << endl;
}
};
int main() {
Force2D f1(1, 2), f2(3, 4);
Force2D f3 = f1 + f2;
f3.print();
class Algebra {
protected:
float a, b;
public:
Algebra(float x = 0, float y = 0) : a(x), b(y) {}
void print() {
cout << "(" << a << ", " << b << ")" << endl;
}
};
void print() {
cout << "(" << a << ", " << b << ", " << c << ")" << endl;
}
};
int main() {
Algebra a1(1, 2), a2(3, 4);
Algebra a3 = a1 + a2;
a3.print();
QUESTION NO 3:
#include <iostream>
using namespace std;
class BankAccount {
protected:
string name;
double balance;
public:
BankAccount(string n, double b) : name(n), balance(b) {}
void print() {
cout << name << ": $" << balance << " (Interest: $" << getInterest() << ")" << endl;
}
};
int main() {
SavingsAccount sa("John", 1000, 5);
CheckingAccount ca("Alice", 2000);
sa.print();
ca.print();
}
QUESTION NO 4:
#include <iostream>
using namespace std;
class Employee {
protected:
string name;
double salary;
public:
Employee(string n, double s) : name(n), salary(s) {}
void print() {
cout << name << ": Salary $" << salary << ", Bonus $" << getBonus() << endl;
}
};
int main() {
Developer d("Bob", 70000, 10);
Manager m("Carol", 90000, 5);
d.print();
m.print();
}
QUESTION NO 5:
#include <iostream>
#include <vector>
using namespace std;
class Patient {
private:
string name;
int age;
float temp;
public:
Patient(string n, int a, float t) : name(n), age(a), temp(t) {}
void print() {
cout << name << ", " << age << " years, Temp: " << temp << "°F" << endl;
}
int main() {
vector<Patient> patients = {
Patient("John", 35, 98.6),
Patient("Jane", 28, 101.2)
};
QUESTION NO 6:
#include <iostream>
#include <vector>
using namespace std;
class Product {
private:
string name;
float price;
public:
Product(string n, float p) : name(n), price(p) {}
void print() {
cout << name << ": $" << price << endl;
}
};
class ShoppingCart {
private:
vector<Product> items;
public:
void add(Product p) {
items.push_back(p);
}
void print() {
float total = 0;
for (auto p : items)
{
p.print();
total += p.print;
}
cout << "Total: $" << total << endl;
}
};
int main() {
ShoppingCart cart;
cart.add(Product("Shirt", 25.99));
cart.add(Product("Pants", 35.99));
cart.print();
}
OUTPUT QUESTIONS