CSC241 Object Oriented Programming
Assignment # 2
Name M. Umar Iqbal
Registration Number FA22 – BEE – 253
Class BEE 3A
Instructor’s Name Dr. Syed Riaz Hussain
Date Monday, 23 October 2023
Question No. 1:
a) Write a class definition that creates a class called leverage with one private
data member, crowbar, of type int and one public function whose
declaration is void pry ().
b) Write a statement that defines an object called lever1 of the leverage class
c) Write a statement that executes the pry () function in the lever1 object
d) Write a member function called getcrow ( ) for the leverage class. This function should return the
value of the crowbar data. Assume the function is defined within the class definition.
e) Write a constructor that initializes to 0 the crowbar data. Assume that the constructor is defined
within the class definition.
F) Assume that the member function getcrow( ) described is defined outside the class definition. Write the
declaration that goes inside the class definition. Write a revised version of the getcrow( ) member function
that is defined outside the class definition.
Question No. 2:
a) Member functions defined inside a class definition are _______ by default.
Answer: inline (also private)
b) In a class definition, data or functions designated private are accessible
i. to any function in the program.
ii. only if you know the password.
Answer: iii.to member functions of that
class.
iv. only to public members of the class.
c) The only technical difference between structures and classes in C++ is that _________.
Answer: member functions and data are, by default, public in structures but private
in classes
d) Sending a message to an object is the same as _________.
Answer: calling one of its member functions
e) If three objects of a class are defined, how many copies of that class’s data items are stored in
memory? How many copies of its member functions?
Answer: three, one
f) Write a declaration (not a definition) for a const void function called aFunc( ) that takes one const
argument called jerry of type float.
Answer: void aFunc(const float jerry) const;
Question No. 3:
Create a class called Ship that incorporates a ship’s number and location. Number each ship object
as it is created. Use two variables of the Angle class from Question-7 of exercise to represent the
ship’s latitude and longitude. A member function of the Ship class should get a position from the
user and store it in the object; another should report the serial number and position. Write a main(
) program that creates three Ships, asks the user to input the position of each, and then displays
each ship’s number and position.
Construct the UML diagram of the class Ship.
CODE LINK: https://onlinegdb.com/d3JPF7vuC
UML diagram of the class Ship:
Angle Ship Notes
- degrees: int - count: int - Private
- minutes: float - shipNumber: unsigned int +Public
- direction: char - latitude: angle
- longitude: angle Member: type
+ getAngle(): void + ship()
+ dispAngle(): void + getPosition(): void
+ display(): void
PROGRAM OUTPUT:
Conclusion:
The program specifies the classes angle and ship to represent ship locations. It
shows how to create objects, get user input, and display them. The UML diagram depicts the class
hierarchy, demonstrating data and behaviour encapsulation. The program manages ship data
efficiently, keeping its structure simple and orderly. It ensures simplicity and clarity in managing
ship data, making it easy to understand and modify.