Lab Report 4_Ver2025
Lab Report 4_Ver2025
SEMESTER 2 2024/2025
Objectives:
The purpose of this lab assignment is to confirm your progress in achieving range of learning
objectives in particular;
a) Understand the basic concept of inheritance in object-oriented programming.
b) Understand to write a simple C++ program by applying inheritance concept.
Exercise 1:
Suppose that you want to define a class to group the attributes of persons in University. There are
both Person class which is Staff and Student. Student group can set their matric number and
CGPA. While staff group, they can set their staff number and their office number. Because of
staff and student shares the common properties such as name, ic number, phone number and
address which grouped in class Person, class Staff and Student can be derived from class Person.
Every class can print their information, and the Staff and Student class can use print function of
Person’s class before print its own information in order to display complete information of the
classes.
1. Base on statements above, state the base class and the derived class.
Person
-Name
-Ic number
-Phone number
-Address
+setName()
+setICno()
+setPhoneNo()
+setAddr()
+printDetails()
Staff Student
-staffNo -matrikNo
-officeNo -CGPA
+setStaffNo() +setMatrikNo()
+setOfficeNo() +setCGPA()
+printDetails() +setPhoneNo()
+printDetails()
#include<iostream>
#ifndef PERSON_H
#define PERSON_H
using namespace std;
class Person{
string name;
int icNo;
int phoneNo;
string address;
public:
void setName(string);
void setICno(int);
void setPhoneNo(int);
void setAddr(string);
void printDetails();
};
#endif
#include<iostream>
#include"Person.h"
using namespace std;
public:
void setStaffNo(int);
void setOfficeNo(int);
void printDetails();
};
Class Staff (header file)
#include<iostream>
#include"personalDetail.h"
using namespace std;
public:
void setMatrikNo(int);
void setCGPA(int);
void printDetails();
};
Class Student (header file)
3. Write a complete C++ implementation file to justify the given header file. (Data provided by
pass parameter, not by read from user).
(30 Marks)
4. In main function, create one object of Staff’s class and one object of Student’s class. Call the
appropriate function within the objects created to store name, ic number, phone number and
address number for both class. Also call the appropriate function to store matric number and
CGPA for student object and staff number and office number for staff object. Lastly call
function to display the information of both classes. Write the code with in sequence objects
(complete the Staff object and then Student object).
With all information given above, write the main function program.
(20 Marks)
Exercise 2:
#include<iostream>
class rectangle{
private:
double width;
double height;
double Area;
public:
void setInput(); //get input from user, with no initialization on variable
void displayVariable();
void area();
rectangle(); //default constructor
rectangle(int,int); //constructor with parameter;
~rectangle();
};
1. Write the default constructor to the class rectangle so that it initializes member variables
to zero.
Rectangle :: rectangle() {
Width = 0;
Length = 0;
Area = 0;
(10 Marks)
2. Write a constructor that can initialize the member variables by using pass parameter.
width = w;
height = h;
(20 Marks)