Anda di halaman 1dari 6

KT24103 CONFIDENTIAL Version No. 1.

1
LAB 2 EXERCISES: Basic Object-Oriented Programming with C++

Exercise 2.1

Create a class called Nationality with the following class members:
A private string variable called identityNo to store the IC or passport number;
A private boolean variable called identityType to store the type of identity, 0 passport,
1 Malaysian IC;
A private character variable called iStatus to store the status of the person, a active, d
deceased, x dangerous;
A public function called setNationality() to set the identityNo,
identityType, and iStatus;
A public function called updateStatus() to update the iStatus;
A public function called getIdentityNo() to return the IC or passport number;
A public function called getIdentityType() to return the Identity type.
A public function called getSidentityType(). If the identifyType is 0, return the
string Foreigner; if the identityType is 1, return the string Malaysian;
A public function called getiStatus() to return the status of the person.
A public function called getSstatus(). If iStatus is a, return the string active; if
iStatus is d, return the string deceased; if iStatus is x, return the string dangerous;
A default constructor to initialize identityType to 1 and iStatus to a;
A parameterized constructor that accept the identityNo, identityType, and
iStatus;
A parameterized constructor that accept the identityNo and identityType. Initialise
the iStatus to a.

Save this class as a file called nationality.h

Exercise 2.2

Create a class called Person with the following class members:
A protected string variable called firstName to store the first name;
A protected string variable called lastName to store the last name (surname);
A protected boolean variable called gender to store the gender of the person, 1 male, 0
female;
A protected user defined datatype called national to store nationality of the person
(using the class Nationality defined in Exercise 2.1) ;
A public function called print to print the name and nationality of the person in the format
Name: fistName lastName
Identity type: identityNo, Status: identity status;

For example,
Name:JasonDerulo
Foreigner:AK47,Status:dangerous

A default constructor;
A parameterized constructor that accept the firstName, lastName, gender, and nationality
details;
A parameterized constructor that accept the firstName, lastName, and gender.

KT24103 CONFIDENTIAL Version No. 1.1
Save this class as a file called person.h


Exercise 2.3

1. Create a derived class called Academic with a public memberAccessSpecifier from the
class Person (as defined in Exercise 2.2) with class members as shown in the left UML class
diagram. The position is the academic position of the staff as either as a lecturer, senior
lecturer, associate professor, or professor. The JPFIU is the name of the unit or school such as
FK, FKI, or FSP. The status can be either as a permanent, temporary, or contract staff. Save
it as academic.h.

2. Create a derived class called Student with a public memberAccessSpecifier from the
class Person (as defined in Exercise 2.2) with the following class members as shown in the
right UML class diagram. The level of a student can be either postgraduate or undergraduate.
The programme is the programme name such as HK08, HC00, HC05, and so on. The
status can be L lulus, LB lulus bersyarat, GB gagal berhenti. Save it as student.h.
























Exercise 2.4

Overload the function print() of the base class Person in the derived classes of Academic and
Student in Exercise 2.3 to print the details of academics and students. Remember, the details of an
academic or a student includes all class members of the sub-classes as well as the super-classes.





Academic
-staffID: string
-position: string
-grade: string
-JPFIU: string
-status: string
+setStaffID (string): void
+setPosition(string):void
+setGrade (string): void
+setJPFIU(string):void
+setStatus(string):void
+getStaffID ( ): string
+getPosition( ):string
+getGrade ( ): string
+getJPFIU( ):string
+getStatus( ):string
+Academic()
+Academic( string, string, bool,
Nationality , string, string, string, string,
string)

Student
-matricNo: string
-level: string
-JPFIU: string
-programme: string
-status: string
+setMatricNo(string): void
+setLevel(string):void
+setProgramme (string): void
+setJPFIU(string):void
+setStatus(char):void
+getMatricNo( ): string
+getLevel( ):string
+getProgramme ( ): string
+getJPFIU( ):string
+getStatus( ):string
+Student(string, string, bool,
Nationality, string, string, string,
string, string)

KT24103 CONFIDENTIAL Version No. 1.1
Exercise 2.5

Write a basic program to create instances of Academic and Student, as defined in Exercise 2.3
and Exercise 2.4, with details as shown in Table 2.1. Then, print all the details of the instances.

Table 2.1: Details of an academic and two students
Details Academic 1 Student 1 Student 2
Instance Academic Student Student
First name Saddam Pei Ling Jason
Last name Hussein Lim Derulo
Gender 1 0 1
Identity type 1 1 0
Identity number 770717-12-5566 850610-13-6688 A12589633
Identity Status a a a
staffID/ matric no 040615-001177 BK131100118 BK131155668
Position/level Senior lecturer Undergraduate Undergraduate
Grade/programme DS52 HC00 HC05
JPFIU FKI FKI FKI
status Permanent L LB

Then, update the details in the program with new data as in Table 2.2, and print out all the details of
each individual that you have created.

Table 2.2: Details to be updated, empty cells indicate no changes.
Details Academic 1 Student 1 Student 2
Identity Status x
Position/level Professor Postgraduate
Grade/programme VK7


Exercise 2.6

Create a class called Publication with the following class members:
A private integer variable called publicationNo as a unique number to identify the
publication;
A private string variable called staffNo to store the identity of the staff who produced the
publication;
A boolean variable called status to store the status of the publication as either 1 - published
or 0 - unpublished;
A private integer variable called type to store the type of publication as either
o 1 Journal International
o 2 Journal National
o 3 Book
o 4 Chapter in Book
o 5 Proceedings International
o 6 Proceedings National
o 7 Others
A private string variable called bookTitle to store the title of the type of publication;
A private boolean variable called authorType that store the type of authoring as either 0
collaborative author, 1 first author;
KT24103 CONFIDENTIAL Version No. 1.1
A private string variable called authors to store the authors of the publication;
A private integer variable called year to store the year of the publication;
A private string variable called title to store the title of the publication;
A private integer called seriesNo to store the series number of the publication if applicable;
Two private integers called pageFrom and pageTo to store the starting page number and the
ending page number of the publication. Common sense dictates that the value for pageTo is
smaller than the value for pageFrom;
A private string called publisher to store the name of the publisher for the named
publication;
A parameterized constructor that accept the publicationNo, staffNo, status, type, booktitle,
title, authors, year, pageFrom, pageTo, and publisher. Also, initialize the value of
authorType to 1 and seriesNo to 1. Set authorType to first author;
Accessor ( e.g., getStatus(), getAutor(), getTitle(), getBooktitle(), etc ) and mutator ( e.g.,
setStatus(bool s), setAuthor(string s), setTitle(string s), setBookTitle(string s), etc ) functions
for all the variables.
A print() method to print the details.


Save it as publication.h.

Exercise 2.7

Create a class called ResearchWarrant with the following class members:
A private integer variable called grantNo as a unique number to identify the research
warrant;
A private string variable called staffNo to store the identity of the staff who succeed in
securing the research warrant;
A private integer variable called level to store the source of funding as either from
o 1 International body
o 2 National body
o 3 University
A private boolean variable called researcherType to store the participation of a researcher as
either 1 head researcher, or 0 co-researcher;
A private string variable called headResearcher to store the name of the head researcher;
A private string variable called coResearchers to store the name of co-researchers;
A private string variable called title to store the title of the research.
Two private string variables called startDate and endDate to store the start date and end
date to conduct the research;
A private integer variable called status to store the status of the funded research as either
o 0 failed
o 1 ongoing
o 2 postponed
o 3 completed
A private string variable called fundSource to store the name of the body that sponsor the
research;
A private double variable called amount to store the amount of fund sponsored for the
research.
A parameterized constructor that accept the grantNo, staffNo, level, headResearcher,
coResearcher, title, startDate, endDate, fundSource, and amount. Also, initialize the value
KT24103 CONFIDENTIAL Version No. 1.1
of researcherType, grantNo, and status. Set researcherType to head researcher and status
to ongoing;
Accessor methods ( e.g., getLevel(), getHeadResearcher(), getCoResearhers(), etc ) and
mutator methods ( e.g., setLevel(int l), setHeadResearcher(string s), setCoresearchers(string
s), etc ) functions for all the variables.
A print() method to print the details.

Save it as research.h.

Exercise 2.8
Write a basic program to create the instances of Academic, Publication, and
ResearchWarrant with the details as shown in Table 2.2, Table 2.3, and Table 2.4. Then, print all
the details arranged according the academic. For example, print the details for Academic 1, followed
by all his publication, and then all his research grants.

Table 2.2: Details of two academic staff
Details Academic 1 Academic2
Instance Academic Academic
First name John Jenny
Last name Loudmouth Brain
Gender 1 0
Identity type 0 1
Identity number 040424-001122 020222-001100
Identity Status a a
staffID/ staffNo 040615-001177 030324-01177
Position/level Senior lecturer Senior lecturer
Grade/programme DS51 DS52
JPFIU FKI FKI
status contract permanent

Table 2.3: Details of academic staffs publication
Details Publication 1 Publication 2 Publication 3
staffID/staffNo 040615-001177 030324-01177 030324-01177
status 0 1 1
type 6 1 1
bookTitle UMS: How to Con? Current trends in
Swarm Robotics
Bio-inspired
Robotics
authorType 0 1 1
authors Chakime, A.,
Loudmouth, J.,
Darghum, J.
Brain, J., Chung,
C.L., Hinad, H.
Brain, J., Lau, E.T.,
Hinad, H.
year 2013 2014 2014
title How to fool everyone
in UMS A
foreigners
perspective
Immune-inspired
Autonomous Error
Detection
Ants-inspired path-
finding algorithm
seriesNo 1
pageFrom 1 55 121
pageTo 10 75 130
publisher Some Publishing IEEE Springer-Verlag
KT24103 CONFIDENTIAL Version No. 1.1
Table 2.4: Details of an academic staffs research warrant
Details Grant 1 Grant 2 Grant 3
staffNo 030324-01177 040615-001177 030324-01177
level 1 3 2
Title Bio-inspired
Robotics
Conning in UMS Ant-inspired path
finding
algorithms
researchType 1 0 1
headReseacher Jenny Brain Jamel Darghum Jenny Brain
coResearcher Hanad Hanid
Chung Ching
Long
John Loudmouth,
Ali Chakime
Ray Alfredo,
Awang Awang
startDate 1-Aug-2012 1-April-2013 1-Sept-2010
endDate 31-July-2015 31-March-2015 31-Aug-2012
status 1 2 3
fundSource IEEE UMS KPM
amount 800,000 50,000 200,000



Exercise 2.9

Update the class Academic to include a function to overload the assignment operator (=). Write a
main program to assign Academic2 in Table 2.2 to an instance of Academic initialized with default
constructor and print out the details of both instances.

Exercise 2.10

Update the class Academic, Publication, and ReseachWarrant to include a function to
overload the stream insertion operator (<<). Then, write a main program to print out the details in
Exercise 2.8 using the overloaded stream insertion operator.

Anda mungkin juga menyukai