0% found this document useful (0 votes)
17 views2 pages

Final Oop

Oop

Uploaded by

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

Final Oop

Oop

Uploaded by

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

Faculty of Information Technology

Final Exam of Computer Programming 2 (CSW 263)


Academic Year: 2022/2023
Level: 2nd Level Semester: Fall 2022
Date: 14 – 01 – 2023 Time: 3 hours
Answer about the following questions (Total: 60 marks)
Question 1: [10 Marks]
(a) Show the output of the following program:
class weight2 { void display()
private: { cout << "\nkg=" << kg << "\tgms=" << grams;
int pound; float ounces; }
public: weight1 operator+ (weight1 w) {
weight2() { pound = 0; ounces = 0.0; } weight1 temp;
weight2(int p, float on) temp.kg = kg + w.kg;
{ pound = p; ounces = on; } temp.grams = grams + w.grams;
void display() return temp; }
{ cout <<"\npnd=" << pound << "\tons=" << ounces; } operator weight2() {
friend class weight1; float kgValue = kg + grams/1000;
}; float poundValue = kgValue * 2.2;
class weight1 { int pnd = (int) poundValue;
private: float oncs = (poundValue - pnd) * 16;
int kg; float grams; return weight2(pnd,oncs); }
public: };
weight1() { kg = 0; grams = 0.0; } void main() {
weight1(int k, float g) weight2 w2(15,8); weight1 w1 = w2;
{ kg = k; grams = g; } w2.display(); w1.display();
weight1(weight2 w) { weight1 wt1(10, 20); weight1 wt2(15, 80);
float poundValue = w.pound + w.ounces/16; weight1 wt3;
float kgValue = poundValue * 0.45; wt3 = wt1 + wt2; wt3.display();
kg = (int) kgValue; weight2 wt4;
grams = (kgValue - kg) * 1000; } wt4 = wt3; wt4.display(); }
Question 2: [15 Marks]
Define a class time that has 3 int data members: hr and min, sec which hold the time in hours ,
minutes, and seconds. It has a constructor that creates a time object and sets its data members to given
values, and it has the following functions:
• get_date() that reads from the keyboard the data of a time object in the form hh:mm:ss.
• Operator = that copies a given time object into another one.
• Operator + that add two objects of class time.
• friend operator < that compares two time objects and returns true if the first time is less
than the second one, and false otherwise.
• show_date() that displays a time object in the form hh:mm:ss.
Add an exception class, and throw an exception in the 3-argument constructor and in the member
function get_date(), if the given hour is not in the range 0 – 23 .

Page 1 of 2
Finally, write a main program that creates a time object and initializes it with current time, and
creates another time object and reads its data from the keyboard, then
• Copy the first object to the second and display the result.
• Add the two objects and display the result.
• Compares these two times and displays them with a message indicating the result of the
comparison.
Question 3: [20 Marks]
A) Define a class productspec, which has three data members: description (type string) , price
(type double), and itemid (type int). that represents the descriptions, price, and id of a product.
It has a constructor and 2 member functions: set_data(), which accept all data of a product and
store it in the data member ; get_price(), which return the price of product object; and an
insertion operator << that displays the description of product object.
B) Define a class SalesLineItem, which has two data members: quantity (type int), spec (object
from class productspec), it has 4 member functions: set_data(), which accept all data of item
and store it in the data member; get_subtotal(), which return the result of price × quantity,
get_quantity(), which return the quantity, and show_data(), that displays the data of
saleslineitem object.
C) Define a class Sale, which has 4 data members:sale_id (type int), date (type string), salelist (
array of object from class saleslineitem), and no_items (an int represent the no of items). it has
a constructor, and three member functions enteritem(), that accept a saleslineitem object and
enter it in the array salelist, get_total(), that calculate and return the total of all salelslineitems
record for this sale, display(), that display all items of the sale in 4 columns discerption, price,
quantity, and subtotal, then at the end display the total of the sale.
D) Write main program that reads a sales’s date and id, and creates a sale object for that sale. Then,
the program reads a series of items descriptions, prices, ids, and quantity, until the descriptions
XXX is entered. For each item, a saleslineitem object is created and entered to the array salelist
of the sale object by registering that item with the sale. After all items have been entered, the
data of the sale are displayed, then the total of sale calculated and displayed.
Question 4: [15 Marks]
A) Declare and implement the following class hierarchy:
(i) An abstract base class Shape that has one data member Perim that represents the perimeter
of the shape, and 3 pure virtual member functions: getdata(), showdata(), and getPerim().
(ii) A derived class Triangle that has 3 additional data members, representing the 3 sides of the
triangle, and implements the 3 member functions: getdata(), to read the 3 sides of the
triangle; showdata(), to display the triangle data; and getPerim(), to calculate the perimeter
of the triangle.
(iii) A derived class Rectangle that has additional 2 data members, representing the length and
width of the rectangle, and implements the 3 member functions: getdata(), to read the length
and width of the rectangle; showdata(), to display the rectangle data; and getPerim(), to
calculate the perimeter of the rectangle.
B) Write a main program that creates an array of 10 pointers to Shape. In a loop, ask the user for
data about a shape and its type (triangle or rectangle), and use new to create a suitable object
(Triangle or Rectangle) to hold the data, then put the pointer to the object in the array. When
the user finishes entering the data for all shapes, display the data of all the shapes entered.
With My Best Wishes
Dr. Alaa Zaki

Page 2 of 2

You might also like