National University of Sciences & Technology (NUST)
School of Electrical Engineering and Computer Science (SEECS)
Department of Electrical Engineering
Mid Semester Exam (MSE)
CS 212 Object Oriented Programming – Fall 2024
BEE-15 2K23 ( ABC)
Name: _______________________________________ Registration #: _________________________
Class / Section: ________________________________ Signature: _____________________________
Instructions
1. Including this cover page, make sure you have 7 pages (4 sheets) of this exam.
2. Answer the questions in the space provided. If you run short of space, use the extra space at the
end.
3. Additional sheets are NOT allowed.
4. For rough work, you may use any space outside the boxes specified for answers.
5. Cheating, use of unfair means or helping others in cheating will lead to cancellation of your exam.
6. Maximum allowed time is Two Hours.
7. YOU CAN DO IT.
Best wishes!
Course Learning Outcomes
1. Understand the difference between procedural and Object-oriented Programming paradigms.
2. Demonstrate the ability to create and use OOP constructs to map real world scenarios.
3. Develop programs using object-oriented techniques.
4. Use the latest IDEs to enable quick development, testing, documentation, and packaging of
programs.
Grading
CLO1 CLO2 CLO2 CLO3
MSE
Total Points
Q1 Q2 Q3 Q4
30 5 5 5 15
_______________________ _______________________ _______________________
Instructor: Peer Reviewer: Invigilator:
Page 1 of 11
Dr.Muhammad Sadiq Amin
Q1. Infer what does the following program prints into the screen. [marks 5 points]
rect area : 12
rectb area : 25
Output : ______________________________________________________________________________
Q2. Recognize the errors and fix those errors in this code. [marks 5]
Page 2 of 11
Solution :
#include <iostream>
1-Line Yard (int, int); (Constructor
using namespace std; Declaration): The constructor Yard 1(int,
int); is declared in the class but is never
class Yard {
defined in the implementation. This will
public: cause a linker error.2-CallingY1.set_length()
and Y2.set_length() (Lines 9-10 in main
Yard(int l = 0, int w = 0) : length(l), width(w), area(0) {}
function): These functions are called in
void set_length(int l) { length = l; } main, but they are defined as void
set_length(); in the class without
void set_width(int w) { width = w; } parameters, which makes it unclear how
void display_area() { the length is being set without additional
implementation.3-Calling Y2.cal_area()
cal_area(); (Line 15 in main function): cal_area() is
cout << "Area: " << area << endl; defined as a private function in the class,
making it inaccessible in main. This will
} cause an error.4-Direct Access to Y1.area
and Y2.area (Line 16 in main function): The
private:
area variable is private, so Y1.area =
void cal_area() { Y2.area; is an invalid access and will cause a
compilation error.5-Uninitialized length
area = static_cast<double>(length * width); }
and width Variables: Without proper
int length; setters or initial values, length and width
could remain uninitialized, leading to
int width;
unpredictable behavior.
double area;
Fixtures :
};
1-Constructor Implementation: Yard(int l =
int main() { 0, int w = 0) initializes length and width to
the values provided or defaults to 0.
Yard Y1, Y2;
2-Setters (set_length and set_width): These
Y1.set_length(20); functions now accept parameters to set
Y1.set_width(22); length and width.
Y2.set_length(10); 3-Private cal_area Function: This function is
called within display_area to ensure that
Y2.set_width(10); area is calculated before it is displayed.
Y1.display_area(); 4-Removed Direct Access to Private
Y2.display_area(); Members: area is calculated and displayed
only through the display_area method,
return 0; which handles everything internally.
}
Page 3 of 11
Q 3. Specify the output of given code. [marks 5]
#include <iostream> int main() {
using namespace std; SecretBox box1(1234), box2(5678);
class Key; Key key1(box1), key2(box2);
class SecretBox {
int secretCode; cout << "Key1 unlocking Box1: " <<
(tryToUnlock(key1, box1) ? "Success" : "Failed") <<
public:
endl;
SecretBox(int code) : secretCode(code) {}
cout << "Key1 unlocking Box2: " <<
friend class Key; (tryToUnlock(key1, box2) ? "Success" : "Failed") <<
endl;
friend bool tryToUnlock(const Key& key, const
SecretBox& box); cout << "Key2 unlocking Box1: " <<
(tryToUnlock(key2, box1) ? "Success" : "Failed") <<
};
endl;
class Key {
cout << "Key2 unlocking Box2: " <<
int associatedCode; (tryToUnlock(key2, box2) ? "Success" : "Failed") <<
endl;
public:
Key(const SecretBox& box) :
associatedCode(box.secretCode) {} return 0;
}
friend bool tryToUnlock(const Key& key, const
SecretBox& box);
};
bool tryToUnlock(const Key& key, const SecretBox&
box) {
return key.associatedCode == box.secretCode;
}
Page 4 of 11
Q4. Apply object orientation to design a Ship class in C++ to manage details for cargo ships.
Implement functionality to input, validate, display, and compare ship details based on their
identification numbers. [Marks 15]
Note: Marks will only be awarded once all parts of the code fully adhere to the instructions.
For example, no marks will be given if any section of the code is incomplete or missing.
Context: Ship management system
1. Class Requirements:
o Create a class Ship with the following private data members: [marks 3]
S_Numb (int): Ship's identification number.
S_Lat (int): Ship's latitude (must be between -90 and 90).
S_Long (int): Ship's longitude (must be between -180 and 180).
S_Name (string): Ship's name.
o Implement the following public member functions: [Marks 7]
Constructor: Initializes the ship’s details and increments a static count of
total ships.
Destructor: Decrements the ship count.
Set functions: set_Numb, set_Lat, set_Long, set_Name – with input
validation for each.
get_Info: Prompts for user input for each field using set functions.
display: Outputs all ship details to the console.
compare: Compares two ships based on S_Numb and indicates which has a
higher number.
countShips: A static function returning the total number of ships created.
2. Main Function Requirements:
o In main: [Marks 5]
Create an array of three Ship objects.
Use get_Info to gather details for each ship.
Display the details of each ship using display.
Use compare to compare the first two ships by S_Numb.
Display the total number of ships using countShips.
Page 5 of 11
Solution :
#include <iostream>
#include <string>
using namespace std;
class Ship {
private:
int S_Numb;
int S_Lat;
int S_Long;
string S_Name;
static int shipCount; // Static variable to keep track of the number of ships
public:
// Constructor
Ship(int numb = 0, int lat = 0, int longi = 0, string name = "")
: S_Numb(numb), S_Lat(lat), S_Long(longi), S_Name(name) {
shipCount++; // Increment ship count whenever a Ship object is created
// Destructor
~Ship() {
shipCount--; // Decrement ship count when a Ship object is destroyed
// Set functions with validation
void set_Name(const string &name) {
S_Name = name;
Page 6 of 11
}
void set_Numb() {
while (true) {
cout << "Enter Ship Number: ";
cin >> S_Numb;
if (cin.fail()) {
cout << "Invalid input. Please enter an integer value." << endl;
cin.clear(); // Clear the fail state
cin.ignore(1000, '\n'); // Ignore any remaining characters in the input buffer
} else {
break;
void set_Lat() {
while (true) {
cout << "Enter Ship Latitude: ";
cin >> S_Lat;
if (cin.fail() || S_Lat < -90 || S_Lat > 90) {
cout << "Invalid latitude. Enter a value between -90 and 90." << endl;
cin.clear();
cin.ignore(1000, '\n');
} else {
break;
Page 7 of 11
}
void set_Long() {
while (true) {
cout << "Enter Ship Longitude: ";
cin >> S_Long;
if (cin.fail() || S_Long < -180 || S_Long > 180) {
cout << "Invalid longitude. Enter a value between -180 and 180." << endl;
cin.clear();
cin.ignore(1000, '\n');
} else {
break;
// Function to get information from user
void get_Info() {
string name;
set_Numb();
set_Lat();
set_Long();
Page 8 of 11
cout << "Enter Ship Name: ";
cin.ignore(); // Clear newline left in buffer after last integer input
getline(cin, name);
set_Name(name);
// Display function to show information
void display() const {
cout << "Ship Number: " << S_Numb << endl;
cout << "Ship Latitude: " << S_Lat << endl;
cout << "Ship Longitude: " << S_Long << endl;
cout << "Ship Name: " << S_Name << endl;
// Function to compare two ships based on S_Numb
void compare(const Ship &other) const {
if (S_Numb > other.S_Numb)
cout << S_Name << " has a greater ship number (" << S_Numb << ") than " << other.S_Name << "
(" << other.S_Numb << ")." << endl;
else if (S_Numb < other.S_Numb)
cout << other.S_Name << " has a greater ship number (" << other.S_Numb << ") than " <<
S_Name << " (" << S_Numb << ")." << endl;
else
cout << S_Name << " and " << other.S_Name << " have the same ship number (" << S_Numb <<
")." << endl;
// Static function to get the total count of ships
Page 9 of 11
static int countShips() {
return shipCount;
};
// Initialize static member
int Ship::shipCount = 0;
int main() {
// Array of Ship objects
Ship fleet[3];
// Get information for each ship
for (int i = 0; i < 3; i++) {
cout << "\nEnter details for Ship " << (i + 1) << ":" << endl;
fleet[i].get_Info();
// Display information for each ship
cout << "\nDisplaying details of all ships:" << endl;
for (int i = 0; i < 3; i++) {
cout << "\nDetails of Ship " << (i + 1) << ":" << endl;
fleet[i].display();
// Compare the first two ships in terms of S_Numb
cout << "\nComparing the first two ships based on their Ship Number:" << endl;
fleet[0].compare(fleet[1]);
Page 10 of 11
// Display the total number of ships created
cout << "\nTotal number of ships created: " << Ship::countShips() << endl;
return 0;
Page 11 of 11