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

Lab 11 question

Uploaded by

iloveulol428
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab 11 question

Uploaded by

iloveulol428
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DCP5201 | OBJECT ORIENTED PROGRAMMING LAB 11

Question 1

Create a complete program based on the following criteria:

A) Create class Triangle.


(i) protected data members : a (int), b (int), c (int)
(ii) public member functions
 Default constructor
To output “-------- Triangle Class -------“.
 void setData(………)
To set the data member a and b with the value of its arguments.

B) Create class PythagorasTriangle which inherits publicly from class Triangle.


(i) private data members : none
(ii) public member functions
 Default constructor
To output “-------Pythogoras Triangle -------“.
 void calcHypotenuse()
To calculate the hypotenuse which is c using the formula given below
𝑐 = √𝑎 2 + 𝑏 2
(Hint: Use the functions in the math library (#include<cmath>)
Square root function: sqrt(<a number>)
Power function: pow(<a number>, 2) )

 void displaySides()
Display values of a, b and c.

C) In main()
 Create an object of class PythagorasTriangle using the new operator.
 Get user input in the main function for 2 sides (side a and side b) of a Pythagoras Triangle and pass
it to the setData(..) function as arguments. Invoke the setData(..) function through the dynamic
object .
 Using the same object, call calcHypotenuse() and displaySides().
 Deallocate the memory for the object.
[Note: refer to sample output screen below]

Sample Output Screen


----- Triangle class -------
-----Pythagoras Triangle------
Enter a : 5
Enter b : 12

::The sides of a Pythagoras triangle::


a: 5
b : 12
c : 13 (also known as the Hypotenuse)

1/3
DCP5201 | OBJECT ORIENTED PROGRAMMING LAB 11

Question 2

Modify main function of Question 1 solution so that the program will repeat as long as user wants to. Use a
do-while loop.
[Note: refer to sample output screen below]

Sample Output Screen


----- Triangle class -------
-----Pythagoras Triangle------
Enter a : 5
Enter b : 12

::The sides of a Pythagoras triangle::


a: 5
b : 12
c : 13 (also known as the Hypotenuse)

Do you want to continue [Y/N] : Y

----- Triangle class -------


-----Pythagoras Triangle------
Enter a : 3
Enter b : 4

::The sides of a pythogoras triangle::


a: 3
b:4
c : 5 (also known as the Hypotenuse)

Do you want to continue [Y/N]: N

Question 3

Create a complete program based on the following criteria:

A) Create class Product


(i) private data member : prodID (int)
(ii) protected data member: total_price (float)
(iii) public member functions :
 constructor to initialize total_price to 0.0
 int getProd_ID()
Returns prodID
 void input_ProdID()
Get user input for prodID (representing product ID)

2/3
DCP5201 | OBJECT ORIENTED PROGRAMMING LAB 11

B) Create class CanFood which inherits protectedly from class Product.


(i) private data members : unit_price(float), order_unit(int)
(ii) public member functions
 void get_Product() Call input_prodID( ).
Get user input for unit_price and order_unit.
 void calculate_Total()
Calculate total_price
 void display_product()
Display prodID by calling accessor function. Display total_price.

C) In main():
(i) Prompt the user to enter the number of types of canned food.
(ii) Create pointer tp of class canFood
(iii) Use the pointer tp to create a dynamic array of canFood using new operator. (the size of the array
will be the number of types of canned food entered by the user earlier)
(iv) In a for-loop that loop through the array, using pointer tp:
 Call get_Product( )
 Call calculate_Total( )
(v) In another for-loop that loop through the array, using pointer tp:
 Call display_product( )
(vi) Deallocate the memory of the dynamic array.

Sample Output Screen


How many types of canned food? 2

Enter product ID : 1001


Enter price : RM30
Enter order unit : 5

Enter product ID : 1002


Enter price : RM45
Enter order unit : 3

Product ID : 1001
Total price : RM150

Product ID : 1002
Total price : RM135

3/3

You might also like