CSC10003 – Object Oriented Programming
Week 04
Inheritance
Cảm ơn thầy Trần Duy Quang đã cung cấp template cho môn học
Department of Software Engineering-FIT-VNU-HCMUS
OOP W04 – Inheritance
1
Notes
Create a single solution/folder to store your source code in a week.
Then, create a project/sub-folder to store your source code of each assignment.
The source code in an assignment should have at least 3 files:
• A header file (.h): struct definition, function prototypes/definition.
• A source file (.cpp): function implementation.
• Another source file (.cpp): named YourID_Ex01.cpp, main function. Replace 01 by id of an
assignment.
Make sure your source code was built correctly. Use many test cases to check your code before
submitting to Moodle.
Name of your submission: StudentID_W04_YY.zip. YY: number of assignments you have done. YY:
00 – 99.
2
OOP W04 – Inheritance
2
Content
In this lab, we will review the following topics:
1. Apply inheritance to build class B based on existing class A.
3
OOP W04 – Inheritance
3
Assignments
A: YY: 01
H: YY: 03
Draw a class diagram and implement in C++ for each of the following assignments.
Class diagram: [Link]
Use class string and class vector.
Use stereotypes to annotate the meaning of classes and methods.
<<interface>>, <<abstract>>, <<concrete>>, <<constructor>>, <<destructor>>, <<friend>>, <<static>>,
<<virtual>>, <<pure virtual>>, <<override>>, <<operator>>.
Ex: +<<pure virtual>> getSalary(): double
3.1. Assignment 1 – Company
Company ABC asks you to build an employee management system.
The system should store the following info for each employee: employ id, full name, hire date, address.
There are 2 types of employees in the company: Office employee and worker.
• Salary of an office employee in a month = number of his/her working days * pay rate per day
(300.000 VND/day)
• Salary of a worker in a month = number of items he/she produced in the month * rate pay per
item (20.000VND/item)
In this week exercise, you are asked to create 3 classes: Employee, OfficeEmployee and Worker.
OfficeEmployee and Worker extends/inherits from Employee.
Then you must define and implement the following methods for each Employee classes.
1. Input the information of an employee
2. Print the information of an employee to console.
3. Compute the salary in a month of an employee
4. Define at least 5 constructors for each of 3 classes.
3.2. Assignment 2 – Bank Account
Define a class, named BankAccount with the following attributes and methods: Attributes:
4
OOP W04 – Inheritance
1. Account number
2. Name of owner
3. Social ID of owner
4. Balance (money in the account)
Methods:
1. Input from keyboard.
2. Print out to console.
3. Deposit an amount of money.
4. Withdraw an amount of money. After withdrawing, the balance should be equal or greater than
50.000 VND.
5. Check the current balance.
Then, define another class, named, SavingBankAccount,which extends / inherits from class
BankAccount.
The attributes and methods of new classes are below. Attributes:
1. Account number
2. Name of owner
3. Social ID of owner
4. Balance (money in the account)
5. Annual interest rates, for example: 6% in a year (360 days)
6. Period, for example, 1, 2, 3, 6 or 12 months.
7. Number of saving months until now, for examples, 3 months.
Methods:
1. Input from keyboard.
2. Print out to console.
3. Deposite an amount of money. Do not allow the customer to deposit if the number of saving
months < period. In fact, the bank teller will ask the customer to create a new saving account to
store new amount of money.
4. Withdraw an amount of money. Do not allow the customer to withdraw if the number of saving
months < period.
5. Withdraw immediately. Then the saving account is considered as free-period one, and the interest
rate is only 2%.
6. Check the current balance. Remember to add the current amount of interest to the balance. Use
balance, interest rate, period and number of saving months to compute the interest.
7. Check the interest at this time.
3.3. Assignment 3 – Console
What is printed to the console? Give a brief explanation.
3.4. Assignment 4
Draw a class diagram and write a simple console application in C++ to simulate the following screens.
5
OOP W04 – Inheritance
Write a report to explain your solution.
New requirements:
• Product information will be loaded from a text file.
• Orders will be saved to a text file.
• Support one type of promotion.