OOP ASsignment
OOP ASsignment
Assignment No: 2
Names: Muneeb Arif
Roll No: FA23-BSE-051
Session: 23-27
Code:
#include <iostream>
#include <string>
class Account {
protected:
int accountNumber;
double balance;
public:
Account(int accNo, double initialBalance)
: accountNumber(accNo), balance(initialBalance) {}
virtual ~Account() {}
};
int main() {
CurrentAccount currentAcc(1001, 500.0);
SavingAccount savingAcc(1002, 1000.0);
std::cout << "Initial Balance in Current Account: " << currentAcc.getBalance() << std::endl;
std::cout << "Initial Balance in Saving Account: " << savingAcc.getBalance() << std::endl;
currentAcc.deposit(200.0);
savingAcc.deposit(300.0);
std::cout << "Balance after deposit in Current Account: " << currentAcc.getBalance() << std::endl;
std::cout << "Balance after deposit in Saving Account: " << savingAcc.getBalance() << std::endl;
currentAcc.withdraw(100.0);
savingAcc.withdraw(200.0);
std::cout << "Balance after withdrawal in Current Account: " << currentAcc.getBalance() << std::endl;
std::cout << "Balance after withdrawal in Saving Account: " << savingAcc.getBalance() << std::endl;
return 0;
}
Output:
1. Encapsulation:
2. Inheritance:
3. Polymorphism:
4. Abstraction:
Code:
#include <iostream>
#include <vector>
#include <string>
// Course class
class Course {
std::string title;
std::string courseCode;
public:
Course(std::string t, std::string c) : title(t), courseCode(c) {}
public:
Student(std::string n) : name(n) {}
int main() {
Course course1("Object Oriented Programming", "CS101");
Course course2("Data Structures", "CS102");
UndergraduateStudent ugStudent("Alice");
PostgraduateStudent pgStudent("Bob");
ugStudent.enrollInCourse(course1);
ugStudent.enrollInCourse(course2);
pgStudent.enrollInCourse(course2);
ugStudent.displayCourses();
pgStudent.displayCourses();
ugStudent.dropCourse("CS101");
pgStudent.dropCourse("CS102");
ugStudent.displayCourses();
pgStudent.displayCourses();
return 0;
}
Output:
1. Course Class:
2. Student Class:
4. Main Function:
Code:
#include <iostream>
class Geometry {
float length, width;
public:
void getDimensions();
void calculate();
private:
void square();
void rectangle();
};
void Geometry::getDimensions() {
std::cout << "Enter length: ";
std::cin >> length;
std::cout << "Enter width: ";
std::cin >> width;
}
void Geometry::calculate() {
if (length == width) {
square();
}
else {
rectangle();
}
}
void Geometry::square() {
float area = length * length;
float perimeter = 4 * length;
std::cout << "It's a square." << std::endl;
std::cout << "Area: " << area << std::endl;
std::cout << "Perimeter: " << perimeter << std::endl;
}
void Geometry::rectangle() {
float area = length * width;
float perimeter = 2 * (length + width);
std::cout << "It's a rectangle." << std::endl;
std::cout << "Area: " << area << std::endl;
std::cout << "Perimeter: " << perimeter << std::endl;
}
int main() {
Geometry geo;
geo.getDimensions();
geo.calculate();
return 0;
}
Output:
3. Functions:
class Staff {
private:
string name;
float basic_salary;
float da; // Dearness Allowance
float hra; // House Rent Allowance
public:
// Constructor to initialize the staff member with name and basic salary
Staff(string n, float b_salary) {
name = n;
basic_salary = b_salary;
da = 0.30 * basic_salary; // DA is 30% of basic salary
hra = 0.40 * basic_salary; // HRA is 40% of basic salary
}
int main() {
string name;
float basic_salary;
return 0;
}
Output:
Code:
#include <iostream>
#include <string>
using namespace std;
class City {
private:
string name;
int population;
string famousPlace;
public:
// Constructor
City(string n = "", int p = 0, string f = "") : name(n), population(p), famousPlace(f) {}
int main() {
const int numCities = 10;
City cities[numCities];
return 0;
}
Output:
1. Encapsulation:
#include <iostream>
class Car {
private:
int wheels;
int doors;
public:
int car_speed;
int main() {
// Creating the ferrari object with default values
Car ferrari;
// Creating the hino object with specified values
Car hino(10, 4);
return 0;
}
Output:
1. Encapsulation:
2. Abstraction:
Code:
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string name;
int age;
string NIC;
string address;
int zip_code;
double salary;
public:
// Constructor
Employee(string n, int a, string nic, string addr, int zip, double sal)
: name(n), age(a), NIC(nic), address(addr), zip_code(zip), salary(sal) {}
int main() {
// Creating an Employee object
Employee emp("John Doe", 35, "123456789", "1234 Elm Street", 12345, 55000.00);
return 0;
}
Output:
1. Encapsulation:
2. Constructor :
4. Print Method:
The print method is used to display all the details of
the employee object.
BEST OF LUCK!