Вы находитесь на странице: 1из 8

THE OPEN UNIVERSITY OF SRI LANKA

DIPLOMA IN TECHNOLOGY - LEVEL 3


ACADEMIC YEAR 2007/2008
MEK3170 C PROGRAMMING
ASSIGNMENT #3

1. Answer all 4 Questions. Mark allocations are given under each question.
2. Marks will be given for proper comments. Therefore, use appropriate comments
in all programmes.
3. Write your registration number and name as a C++ comment at the end of each
programme you submit.
4. Programmes should be named as Question1.cpp, Question2.cpp etc. The text file,
object file and the executable file of each programme should be submitted. You
should copy all the programmes (programming codes) that you have written into a
word document and this also needs to be submitted along with the programmes.
5. Put all above mentioned files in to a folder and name the folder as
‘MEK3170_A3_2007_your student number’ and zip the folder and upload it to
the OUSL Virtual Class Room.
6. Last date of submission is 25th January 2008
7. Penalties will apply for late submissions
8. If you need further clarification, you may contact the academic coordinator via
email
Email: wsnan@ou.ac.lk

Question 1

This question is based on the concept of Inheritance. You should write a programme,
which produces an output, which is similar to a balance sheet provided by a bank at the
end of each month. Your output should be organized in the following manner.

---Nimal’s Account---

Account Type : Savings


Rs
Opening Amount: 5000
Deposits: 300
Balance: 5300
Withdrawal: 200
Balance: 5100
Withdrawal: 100
Balance: 5000
Interest: 100
Account Balance: 5100

Page 1 of 8
Create a class called ‘Account’ and two derived classes called Current account
(Current_acc) and Savings Account (Savings_acc)

Account

Current Account Savings Account

(a) Class Account

• This class has a private data member called ‘amount’ and two protected data
members called ‘balance’ and ‘ini_amount’.

o Balance - Store the balance of any account


o ini_amount - Store the initial amount that the customer has deposited at the
opening of any bank account.
o amount - In a cash deposit this variable carries the cash amount which will be
added to the bank balance, and in a cash withdrawal this variable carries the
cash amount, which will be deducted from the bank balance.

• Class Account has three public methods called ‘status’, ‘deposit’ and ‘withdraw’.

o status()-This method should display the initial balance in an account. Initially


the bank balance is the initial amount that a customer deposits at the opening
of the account.
o deposit()-This method should be called every time a money deposit takes
place in any type of account. The money that the customer deposits (stored in
the variable ‘amount’) should be added to the balance and display the answer
using this method.
o withdraw()- This method should be called every time a money withdrawal
takes place in any type of account. The money that the customer withdraws
(stored in the variable ‘amount’) should be deducted from the balance and
display the answer using this method.

• You should define a constructor for the class account which initializes the default
initial bank balance to zero 0.

• You should also define a destructor.

Page 2 of 8
(b) Class Current Account

• Every customer should deposit Rs.5000 in order to open a current account.


Hint: Use a constructor

• Every month a service charge of Rs.100 will be charged from the customer. Hence
at the end of each month Rs.100 will be deducted from the customers bank
balance. Create a public method to calculate the service charges and debit
(deduct) the service charge from the customer’s current account balance at the end
of the month and display the answer.

(c) Class Savings Account

• Every customer should deposit Rs.1000 or more in order to open a savings


account. The customer can deposit Rs.1000 or any amount above Rs.1000, at the
opening of the savings account.
Hint: Use constructors.

• 2% interest on the present balance will be added to the savings account at the end
of each month according to the bank balance.

Example: At the end of the month if the bank balances is Rs.5000


Interest = 5000*0.02 = Rs.100 and the bank balance will be Rs.5100

Create a public method to calculate the interest and credit (add) it to the
customer’s savings account balance at the end of the month and display the
answer.

The tables 1, 2 and 3 show the transactions that Kamal, Saman and Mihiri has performed
during the month of November 2007. Create the objects and perform the following
transactions for the month of November and display the transactions according to the
format given at the beginning of the question.

Table 1:
Kamal’s Account
Type - Current Account
Date Transaction Amount (Rs)
01/11/2007 Opening the account 5000 (default value)
10/11/2007 Deposit 2300
15/11/2007 Deposit 7000
17/11/2007 Withdrawal 1750
20/11/2007 Withdrawal 1900

Page 3 of 8
Table 2:
Saman’s Account
Type: Savings Account
Date Transaction Amount(Rs)
02/11/2007 Opening the account 1000 (default value)
05/11/2007 Deposit 2500
15/11/2007 Deposit 3500
19/11/2007 Withdrawal 750
30/11/2007 Deposit 7500

Table 3:
Mihiri’s Account
Type: Savings Account
Date Transaction Amount(Rs)
06/11/2007 Opening the account 8000
12/11/2007 Deposit 2500
17/11/2007 Deposit 3500
22/11/2007 Deposit 4300
25/11/2007 Withdrawal 1200

Note: You do not need to include the date of the transactions in your programme.

Hint: Inside the main method, you should call the different types of methods you have
created (ex. deposit(), withdraw(), status(), etc ) for every object, in order to display the
transactions properly.

(30 marks)

Question 2

This question is based on the concept of ‘Polymorphism’

Create a base class called ‘Student’ and two derived classes called Regular Student
(Regular_stu) and Scholarship Student (Schol_stu).

Student

Regular Student Scholarship Student

Page 4 of 8
• In the base class, define two protected members called ‘course’ and ‘material’ and
a public method called set_values in order to set the values for the protected
members ‘course’ and ‘material’.

• Students have to pay a course fee (‘course’) as well as a course material fee
(‘material’) depending on the courses that they have enrolled for. Total course
charges include the course chargers as well as the course material charges.

• Class Regular_stu has a public method called charges() which calculates the total
course charges.

• Class Schol_stu also has the public method called charges() which calculates the
total course charges but the scholarship students will be charged half of the course
charges and they also get a 10% discount for the course material charges.

• Create two objects called ‘regular’ and ‘scholar’, which are of classes regular_stu
and schol_stu respectively.

• Create two pointers of type Student and assign references to ‘regular’ and
‘scholar’ to these pointers.

• Using the pointers and set the values of the objects according to the values given
below.
o The regular student’s course charge is Rs.5000.50 and the material charge
is Rs.2000.75.
o The scholarship student’s course charge is 2000 and the material charge is
Rs.100.90.

• Using the pointers, call the charges() methods of the Regular_stu class and the
Schol_stu class and display the answers.

Your output should be organized in the following manner.

--- Regular Student---


Charges Rs.7001.25

---Scholarship student---
Charges Rs. 1090.81

Hint: Use virtual member functions.

Page 5 of 8
(20 marks)

Question 3

You should write a programme which similar to an information system which stores
information on books.

a) The user should be given the opportunity to enter the information given below
into the system separately.

• Name of the book: Maximum 50 characters long.


• The author’s first name or names and the last name: The user should be
able to enter the author’s first name and the last name separately. (first
name- maximum 50 characters, last name-maximum 40 characters)
• Information regarding the book: (maximum 100 characters)
• Book ISBN: Book ISBN is an integer value.

(b) The information that the user has entered should be stored in a text file called
‘books’ in the following manner.
Book Name - C++ Programming for Dummies
Author’s Name - Stephen Randy Davis
Book Details - C++ for Dummies is an introduction for C++ programmig
Book ISBN - 8126501200

Note:
o Every time a user enters a new book into the system, all the details should
be appended (added) to the text file.
o Even though the user enters the author’s first name and last name
separately, the author’s full name should be stored in the text file as given
above.

Hint: Use the file input/output flags and string functions.

(c) After the user enters the details of the book into the system, your programme
should display the following message to the user.

Do you want to view the available books?


Press 'y' if you want to view available books
Press 'n' if you want to exit the programme

Page 6 of 8
• If the user enters ‘y’ your programme should display all the information in
your text file into the output screen after clearing the screen. Your output
should be organized in the following manner.

---Sarasi Bookshop---

Available Books:

Book Name - C++ Programming for Dummies


Author’s Name - Stephen Randy Davis
Book Details - C++ for Dummies is an introduction for C++ programmig
Book ISBN - 8126501200
--------------------------------------------------------------------------------------

Book Name - Research Methods for Business


Author Name - Uma Sekaran
Book Details - A book which illustrates different research approaches
Book ISBN - 8126509287
--------------------------------------------------------------------------------------

• If the user enters ‘n’, your programme should quit.

• If the user enters any other character, your programme should display an
error message and ask the user to enter the correct option until the user
enters a valid character. (‘y’ or ‘n’)

(30 marks)

Question 4

You should write a programme using a C++ data structure, which calculates the total
number of seconds when the user enters the number of hours, minutes and seconds.

(a) Define a structure called ‘Time’ and three integer members called ‘hours’,
‘minutes’ and ‘seconds’ and a object called ‘t’

(b) The user should be given the opportunity to enter the number of hours, minutes
and seconds, separately. Store the values that the user has entered in the data
members called ‘hours’, ‘minutes’ and ‘seconds’ respectively.

(c) Your programme should create an integer function and pass the data structure into
the function as a parameter. Using the above function calculate the total number
of seconds and return the answer into the main function.

(d) Using the function, display the answer.

Page 7 of 8
Your programme output should be organized in the following manner.

---Calculating the time in seconds---

Please enter the number of hours: 3


Please enter the number of minutes: 10
Please enter the number of seconds: 20

Total Seconds: 11420

Note: 1 hour = 3600 seconds


1 minute = 60 seconds

(20 marks)

Page 8 of 8

Вам также может понравиться