Sie sind auf Seite 1von 2

Object oriented programming language using C++

Final examination. Code: 2


May 31th 2016
Student name
Time 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 particular
values of its 2 attributes.
A virtual methodpnamed length to calculate the length of a vector.
Hint: Length = x2 + y 2
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 integer
attribute z. Methods of 3D vector include:
A constructor with 3 integer parameters: initialize a 3D vector-object with particular values of its 3 attributes. It calls the constructor of the class vector.
A redefined method
p length to calculate the length of a vector.
Hint: Length = x2 + y 2 + 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 integer
attributes z and t. Methods of 4D vector include:
1

Display the values of all vector elements

Final examination. Code: 2 - Page 2 of 2

May 31th 2016

A constructor with 4 integer parameters: initialize a 4D vector-object with particular values of its 4 attributes. It calls the constructor of the class vector.
2
A redefined method
p length to calculate the length of a vector .
Hint: Length = x2 + y 2 + 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.

To calculate the square root sqr of a real number r:


- Use math.h: #include < math.h >
- Calculate the square root: sqr = sqrt(r)

Das könnte Ihnen auch gefallen