0% found this document useful (0 votes)
3 views13 pages

Assignment 3 Oop

The document contains several C++ programming assignments focusing on object-oriented programming concepts such as classes, inheritance, and operator overloading. It includes examples of 2D and 3D force classes, algebra classes, bank account management, employee bonus calculations, patient records, and a shopping cart system. Each section provides code snippets demonstrating the implementation of these concepts.

Uploaded by

241446
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Assignment 3 Oop

The document contains several C++ programming assignments focusing on object-oriented programming concepts such as classes, inheritance, and operator overloading. It includes examples of 2D and 3D force classes, algebra classes, bank account management, employee bonus calculations, patient records, and a shopping cart system. Each section provides code snippets demonstrating the implementation of these concepts.

Uploaded by

241446
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

NAME MUBASHRA NADEEM

REG. ID 241446

SUBJECT OOP Theory

SECTION BSAI-F-24-A

SUBMITTED TO Sir Tahir Akram


ASSIGNMENT NO 3:

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) {}

Force2D operator+(const Force2D& other) {


return Force2D(fx + other.fx, fy + other.fy);
}

void print() {
cout << "Force: (" << fx << ", " << fy << ")" << endl;
}
};

class Force3D : public Force2D {


private:
float fz;
public:
Force3D(float x = 0, float y = 0, float z = 0) : Force2D(x, y), fz(z) {}

Force3D operator+(const Force3D& other) {


return Force3D(fx + other.fx, fy + other.fy, fz + other.fz);
}

void print() {
cout << "Force: (" << fx << ", " << fy << ", " << fz << ")" << endl;
}
};

int main() {
Force2D f1(1, 2), f2(3, 4);
Force2D f3 = f1 + f2;
f3.print();

Force3D f4(1, 2, 3), f5(4, 5, 6);


Force3D f6 = f4 + f5;
f6.print();
}
QUESTION NO 2:
#include <iostream>
using namespace std;

class Algebra {
protected:
float a, b;
public:
Algebra(float x = 0, float y = 0) : a(x), b(y) {}

Algebra operator+(const Algebra& other) {


return Algebra(a + other.a, b + other.b);
}

void print() {
cout << "(" << a << ", " << b << ")" << endl;
}
};

class Algebra3D : public Algebra {


private:
float c;
public:
Algebra3D(float x = 0, float y = 0, float z = 0) : Algebra(x, y), c(z) {}

Algebra3D operator+(const Algebra3D& other) {


return Algebra3D(a + other.a, b + other.b, c + other.c);
}

void print() {
cout << "(" << a << ", " << b << ", " << c << ")" << endl;
}
};

int main() {
Algebra a1(1, 2), a2(3, 4);
Algebra a3 = a1 + a2;
a3.print();

Algebra3D a4(1, 2, 3), a5(4, 5, 6);


Algebra3D a6 = a4 + a5;
a6.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) {}

virtual double getInterest() = 0; // Pure virtual

void print() {
cout << name << ": $" << balance << " (Interest: $" << getInterest() << ")" << endl;
}
};

class SavingsAccount : public BankAccount {


private:
float rate;
public:
SavingsAccount(string n, double b, float r) : BankAccount(n, b), rate(r) {}

double getInterest() override {


return balance * rate / 100;
}
};

class CheckingAccount : public BankAccount {


public:
CheckingAccount(string n, double b) : BankAccount(n, b) {}

double getInterest() override {


return 0; // No interest
}
};

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) {}

virtual double getBonus() = 0; // Pure virtual

void print() {
cout << name << ": Salary $" << salary << ", Bonus $" << getBonus() << endl;
}
};

class Developer : public Employee {


private:
int bugsFixed;
public:
Developer(string n, double s, int b) : Employee(n, s), bugsFixed(b) {}

double getBonus() override {


return bugsFixed * 50; // $50 per bug
}
};

class Manager : public Employee {


private:
int teamSize;
public:
Manager(string n, double s, int t) : Employee(n, s), teamSize(t) {}

double getBonus() override {


return teamSize * 100; // $100 per team member
}
};

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;
}

bool hasFever() { return temp > 100; }


};

int main() {
vector<Patient> patients = {
Patient("John", 35, 98.6),
Patient("Jane", 28, 101.2)
};

for (auto p : patients) {


p.print();
if (p.hasFever()) cout << "Has fever!\n";
}
}

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

You might also like