Upload
minh-quang-nguyen
View
212
Download
0
Embed Size (px)
Citation preview
8/17/2019 [finalVersion]DeCuoiKi2.pdf
http://slidepdf.com/reader/full/finalversiondecuoiki2pdf 1/2
Object oriented programming language using C++
Final examination. Code: 2May 31th 2016 Student nameTime limit: 120 minutes Student ID
Note: You are not allowed to use textbooks or notes or anything else besides your brain.
1. (5 points) Write a program with a menu:
1. Find a digit in a positive integer: press 1.
2. Exit: press 2.
Operation of the program:
• If a user enters ’1’:
– Ask the user to input a positive integer N and a digit d.
– Print out on the screen: whether d appears in N or not?
– Repeat until the user presses ’2’.
• If the user enters ’2’: close the program.
• If the user enters other strings: ask him/her to re-input.
2. (5 points) Create a class named vector with 2 attributes: x and y (both are integers).The class vector includes the following methods:
• A constructor with 2 integer parameters: initialize a vector-object with particularvalues of its 2 attributes.
• A virtual method named length to calculate the length of a vector.Hint: Length =
x2 + y2
• A virtual method named display to display a vector1 and its length on the screen.
Create a class named 3D vector inherited from the class vector with additional integerattribute z . Methods of 3D vector include:
• A constructor with 3 integer parameters: initialize a 3D vector-object with par-ticular values of its 3 attributes. It calls the constructor of the class vector.
• A redefined method length to calculate the length of a vector.Hint: Length =
x2 + y2 + z 2
• A redefined method display to display a vector and its length on the screen.
Create a class named 4D vector inherited from the class vector with additional integerattributes z and t. Methods of 4D vector include:
1Display the values of all vector elements
8/17/2019 [finalVersion]DeCuoiKi2.pdf
http://slidepdf.com/reader/full/finalversiondecuoiki2pdf 2/2
Final examination. Code: 2 - Page 2 of 2 May 31th 2016
• A constructor with 4 integer parameters: initialize a 4D vector-object with par-ticular values of its 4 attributes. It calls the constructor of the class vector.
• A redefined method length to calculate the length of a vector2.Hint: Length =
x2 + y2 + z 2 + t2
•
A redefined method display to display a vector and its length on the screen.Operation of the function main:
• Create an array of 10 vector pointers.
• For each pointer:
– Ask users to enter a number 0 or 1, corresponds to 3D vector or 4D vector.
– Ask users to enter values for the attributes of a 3D vector object (or 4D vector
object) to temporary variables.
– Based on the information entered, initialize an appropriate object.
• Use a f or loop to:– Display on the screen the information of 10 vectors and their lengths.
2To calculate the square root sqr of a real number r:- Use math.h: #include < math.h >
- Calculate the square root: sqr = sqrt(r)