0% found this document useful (0 votes)
41 views

OOP ASsignment

Uploaded by

ahmadhaseeb5964
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)
41 views

OOP ASsignment

Uploaded by

ahmadhaseeb5964
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/ 22

Mirpur University of Science and Technology

Department of Software Engineering

Assignment No: 2
Names: Muneeb Arif
Roll No: FA23-BSE-051
Session: 23-27

Subject: Object Oriented Programming(OOP)

Submitted to: Engr. Aqsa Rasheed


Q-1: Develop a banking system in which client can open
account.There are two kinds of account; Current Account and
Saving Account. Each kind of accounts withdraw in different
ways.The account is identified by account number.The
account has a functionality of deposit, withdraw, and get
balance?

Code:

#include <iostream>
#include <string>

class Account {
protected:
int accountNumber;
double balance;

public:
Account(int accNo, double initialBalance)
: accountNumber(accNo), balance(initialBalance) {}

virtual void deposit(double amount) {


balance += amount;
}

virtual bool withdraw(double amount) = 0; // Pure virtual function

double getBalance() const {


return balance;
}

int getAccountNumber() const {


return accountNumber;
}

virtual ~Account() {}
};

class CurrentAccount : public Account {


public:
CurrentAccount(int accNo, double initialBalance)
: Account(accNo, initialBalance) {}

bool withdraw(double amount) override {


if (balance >= amount) {
balance -= amount;
return true;
}
else {
std::cout << "Insufficient funds in Current Account." << std::endl;
return false;
}
}
};
class SavingAccount : public Account {
public:
SavingAccount(int accNo, double initialBalance)
: Account(accNo, initialBalance) {}

bool withdraw(double amount) override {


if (balance >= amount + 5) { // Assume $5 fee for withdrawals
balance -= (amount + 5);
return true;
}
else {
std::cout << "Insufficient funds in Saving Account." << std::endl;
return false;
}
}
};

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:

 Data members accountNumber and balance are kept


private.
 Public methods (deposit, withdraw, getBalance,
getAccountNumber) provide controlled access to these
members.

2. Inheritance:

 Account is the base class.


 CurrentAccount and SavingAccount are derived classes.

3. Polymorphism:

 Account withdraw is a pure virtual function in the base


class
 CurrentAccount and SavingAccount provide their own
implementations of withdraw.

4. Abstraction:

 The user interacts with a simplified interface to deposit,


withdraw, and check the balance.

5. Code Organization and Structure:

 Classes and methods are appropriately named.


 Inheritance hierarchy is clear and logical.
 Encapsulation ensures that data is accessed and modified
only through well-defined methods.

6. Functionality and Correctness:


 The system supports deposit, withdrawal, and balance
checking.
 Withdrawal behavior differs for CurrentAccount and
SavingAccount.
 Edge cases like insufficient funds are handled with
appropriate messages.

7. Documentation and Comments:

 The code includes basic comments explaining the key


parts and logic.
 More detailed comments can be added as needed,
especially for more complex logic or design decisions.

Q-2: Develop a Student Registration System in which


students gets enroll in a semester . The semester contains
courses. Each courses has a title and course code. The Course
can be registered, dropped, withdraw, passed, and failed by
the students. Also a semester may be freezed or
attended.There are two kinds of students; Undergraduate
Students (for BSS & MCS) and Postgraduate Students(for
MPhil & PHD). Each type of Students enroll in different
ways.

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

std::string getTitle() const { return title; }


std::string getCourseCode() const { return courseCode; }
};

// Base class for students


class Student {
protected:
std::string name;
std::vector<Course> enrolledCourses;

public:
Student(std::string n) : name(n) {}

void enrollInCourse(const Course& course) {


enrolledCourses.push_back(course);
std::cout << name << " enrolled in " << course.getTitle() << "\n";
}

void dropCourse(const std::string& courseCode) {


for (auto it = enrolledCourses.begin(); it != enrolledCourses.end(); ++it) {
if (it->getCourseCode() == courseCode) {
std::cout << name << " dropped " << it->getTitle() << "\n";
enrolledCourses.erase(it);
return;
}
}
std::cout << "Course not found!\n";
}

void displayCourses() const {


std::cout << "Courses for " << name << ":\n";
for (const auto& course : enrolledCourses) {
std::cout << "- " << course.getTitle() << " (" << course.getCourseCode() << ")\n";
}
}
};

// Derived class for undergraduate students


class UndergraduateStudent : public Student {
public:
UndergraduateStudent(std::string n) : Student(n) {}
};

// Derived class for postgraduate students


class PostgraduateStudent : public Student {
public:
PostgraduateStudent(std::string n) : Student(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:

 Encapsulates course details such as title and course code.


 Provides methods to get the title and course code.

2. Student Class:

 Contains student name and a list of enrolled courses.


 Provides methods to enroll in a course, drop a course, and
display enrolled courses.

3. UndergraduateStudent and PostgraduateStudent


Classes:

 Inherit from the Student class.


 They can have specific behaviors if needed, but for
simplicity, they currently use the same behavior as the
Student class.

4. Main Function:

 Creates courses and student objects.


 Enrolls students in courses, displays their courses, and
allows them to drop courses.

Q-3: Write program using class Geometry with data members


length and width. The program
should ask the user for length and width from function
members if the length and width
both are same then it should call square function to calculate
its area and parameter
otherwise it should call rectangle function to calculate the
area and parameter of the
rectangle.

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:

1. Encapsulation: The length and width data members are


private, ensuring they are only accessible through the
class's member functions.
2. Abstraction: The user interacts with simple functions like
getDimensions and calculate, hiding the details of how
area and perimeter calculations are done.

3. Functions:

 getDimensions: Prompts the user to enter the dimensions.

 calculate: Determines whether to treat the shape as a


square or rectangle based on the dimensions and calls the
appropriate function.

 square: Calculates and prints the area and perimeter of a


square.

 rectangle: Calculates and prints the area and perimeter of


a rectangle.

4. Code Organization: The class and its methods are well-


organized, with clear naming conventions for readability
and maintainability.

Q-4: Write a program to declare a class staff having data


members name, basic salary, dearneass
Allowence (DA), House Rent Allownce (HRA) and calculate
gross salary. Accept and display data for one staff where
DA=30% of basic salary
HRA=40% of basic salary
Gross salary= HRA+DA+ Basic salary ?
Code:
#include <iostream>
#include <string>

using namespace std;

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
}

// Method to calculate the gross salary


float calculateGrossSalary() {
return basic_salary + da + hra;
}

// Method to display the staff details including gross salary


void displayStaffDetails() {
cout << "Name: " << name << endl;
cout << "Basic Salary: " << basic_salary << endl;
cout << "Dearness Allowance (DA): " << da << endl;
cout << "House Rent Allowance (HRA): " << hra << endl;
cout << "Gross Salary: " << calculateGrossSalary() << endl;
}
};

int main() {
string name;
float basic_salary;

// Input from the user


cout << "Enter the staff's name: ";
getline(cin, name);
cout << "Enter the basic salary: ";
cin >> basic_salary;

// Create a Staff object


Staff staff_member(name, basic_salary);

// Display the staff details


staff_member.displayStaffDetails();

return 0;
}
Output:

1. Namespace: using namespace std; is used to bring all the


standard library symbols into the current scope, allowing
us to use string, cin, cout, and other standard library
components without prefixing them with std::.

2. Class Definition: The Staff class remains the same, with


private data members and public methods for initializing,
calculating gross salary, and displaying details.

3. Main Function: The main function captures user input,


creates a Staff object, and calls the method to display the
staff member's details.

Q-5: Write a program to define class city having data name,


population, famous place. Accept and display data for 10
cities.

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

// Function to accept city details


void accept() {
cout << "Enter city name: ";
getline(cin, name);
cout << "Enter population: ";
cin >> population;
cin.ignore(); // To ignore the newline character after entering the population
cout << "Enter famous place: ";
getline(cin, famousPlace);
}

// Function to display city details


void display() const {
cout << "City Name: " << name << endl;
cout << "Population: " << population << endl;
cout << "Famous Place: " << famousPlace << endl;
}
};

int main() {
const int numCities = 10;
City cities[numCities];

// Accept details for 10 cities


for (int i = 0; i < numCities; ++i) {
cout << "Enter details for city " << i + 1 << ":" << endl;
cities[i].accept();
cout << endl;
}

// Display details for 10 cities


for (int i = 0; i < numCities; ++i) {
cout << "Details of city " << i + 1 << ":" << endl;
cities[i].display();
cout << endl;
}

return 0;
}

Output:
1. Encapsulation:

 The City class encapsulates the data members (name,


population, and famousPlace) and provides member
functions (accept and display) to interact with these
data members.

2. Class Hierarchy and Relationships:


 This is a simple class design where there is only one
class, City, which encapsulates the properties and
behaviors related to a city.

3. Code Organization and Structure:

 The City class uses appropriate naming conventions.

 Methods like accept and display are clearly named to


represent their functionality.

4. Functionality and Correctness:

 The program accepts and displays data for 10 cities


as required.

 Proper input handling (using getline and cin.ignore)


ensures correct data entry.

5. Documentation and Comments:

 Comments are included to explain the purpose of


each part of the code, making it easy to understand
and maintain.

Q-6: . Write a C++ program to make a car class with wheels,


doors as private data members and car_speed as public data
member while speed and break as public member functions.
Use constructor with default value of wheels=4,doors=2 and
car_speed=0 for initialization. Make two objects in the name
of ferrari and hino. Ferrari is initialized through default
values while hino has 10 wheels, 4 doors. Every time when
you call speed function car_speed is increased by 5 while
break function decrease it by 5. Display current speed?
Code:

#include <iostream>

using namespace std;

class Car {
private:
int wheels;
int doors;

public:
int car_speed;

// Constructor with default values


Car(int w = 4, int d = 2, int s = 0) : wheels(w), doors(d), car_speed(s) {}

// Function to increase speed


void speed() {
car_speed += 5;
}

// Function to decrease speed


void brake() {
if (car_speed >= 5) {
car_speed -= 5;
}
else {
car_speed = 0;
}
}

// Function to display car details


void display() const {
cout << "Wheels: " << wheels << endl;
cout << "Doors: " << doors << endl;
cout << "Current Speed: " << car_speed << " km/h" << endl;
}
};

int main() {
// Creating the ferrari object with default values
Car ferrari;
// Creating the hino object with specified values
Car hino(10, 4);

// Displaying initial values for ferrari


cout << "Ferrari:" << endl;
ferrari.display();
cout << endl;

// Displaying initial values for hino


cout << "Hino:" << endl;
hino.display();
cout << endl;

// Increasing speed for ferrari


ferrari.speed();
ferrari.speed();
ferrari.speed();
// Displaying updated speed for ferrari
cout << "Ferrari after speeding:" << endl;
ferrari.display();
cout << endl;

// Applying brake for ferrari


ferrari.brake();
// Displaying updated speed for ferrari after brake
cout << "Ferrari after braking:" << endl;
ferrari.display();
cout << endl;

// Increasing speed for hino


hino.speed();
hino.speed();
hino.speed();
hino.speed();
// Displaying updated speed for hino
cout << "Hino after speeding:" << endl;
hino.display();
cout << endl;

// Applying brake for hino


hino.brake();
// Displaying updated speed for hino after brake
cout << "Hino after braking:" << endl;
hino.display();
cout << endl;

return 0;

}
Output:
1. Encapsulation:

 The wheels and doors data members are private,


ensuring they can only be accessed and modified
through the class's public methods.

 The car_speed data member is public for direct


access as per the requirements.

2. Abstraction:

 The Car class hides the complexity of the car's


internals (number of wheels and doors) and exposes
only the functionalities to modify the speed.

3. Inheritance and Polymorphism:

 Not directly applicable in this specific problem as


there is no class hierarchy or polymorphic behavior
required.

4. Design and Implementation:


 The Car class effectively encapsulates data and
behavior with appropriate constructors and member
functions.

5. Code Organization and Structure:

 The code uses clear and appropriate naming


conventions for classes, methods, and variables.

 The display function helps in showcasing the state of


the object.

6. Functionality and Correctness:

 The program correctly initializes objects and


modifies their state according to the specified
functionality.

7. Documentation and Comments:


 The program includes comments to explain the
purpose of each section, enhancing readability and
maintainability.

Q-7: Write a C++ program where Employee is an object. Each


employee has a name, age, NIC#, Address and salary. Your
Employee class should have a constructor that initializes all
its fields including Address. Declare function prototypes to
set and get the value of the zip code of an employee. Also
declare a print() function that prints all the details of an
employee object?

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

// Setter for zip code


void setZipCode(int zip) {
zip_code = zip;
}

// Getter for zip code


int getZipCode() const {
return zip_code;
}

// Function to print employee details


void print() const {
cout << "Employee Details:" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "NIC#: " << NIC << endl;
cout << "Address: " << address << endl;
cout << "Zip Code: " << zip_code << endl;
cout << "Salary: $" << salary << endl;
}
};

int main() {
// Creating an Employee object
Employee emp("John Doe", 35, "123456789", "1234 Elm Street", 12345, 55000.00);

// Printing initial details of the employee


emp.print();
cout << endl;

// Changing the zip code using the setter


emp.setZipCode(67890);

// Printing updated details of the employee


cout << "Updated Employee Details:" << endl;
emp.print();

return 0;
}
Output:

1. Encapsulation:

 The data members name, age, NIC, address,


zip_code, and salary are private, ensuring they can
only be accessed and modified through the class's
public methods.

2. Constructor :

 The Employee class has a constructor that initializes


all its fields, including the address and zip code.

3. Getter and Setter Methods:

 The class includes a setter method setZipCode to set


the value of the zip code and a getter method
getZipCode to retrieve the value of the zip code.

4. Print Method:
 The print method is used to display all the details of
the employee object.

5. Code Organization and Structure:

 The code uses clear and appropriate naming


conventions for classes, methods, and variables.

 The program is structured to separate the main


functionality from the class definition, enhancing
readability and maintainability.

6. Functionality and Correctness:

 The program correctly initializes an employee object and


allows modification of the zip code using setter and getter
methods.
7. Documentation and Comments:

 The program includes comments to explain the purpose of


each section, making it easy to understand and maintain.

BEST OF LUCK!

You might also like