THE UNIVERSITY OF DODOMA
COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION
Course code: CP 223
Course Name: Object-oriented Systems design
GROUP MEMBERS
NAME REG NUMBER PROGRAM
HENRY TARSIAN T/UDOM/2020/00342 SE
HAPPINESS MUHOCHI T/UDOM/2020/00390 SE
SAID MAPANDE T/UDOM/2020/09488 SE
VICENT LAIZER T/UDOM/2020/00388 SE
SIMEO MANYERERE T/UDOM/2020/00364 SE
REGINA LUDOVIC T/UDOM/2020/07104 SE
ALFRED MWASHALA T/UDOM/2020/00351 SE
NESTORY KATO T/UDOM/2020/00349 SE
KELVIN ANDERSON T/UDOM/2020/00355 SE
ALEX JONATHAN T/UDOM/2020/00359 SE
INSTRUCTOR: Mr. WOISO
Question one source code:
// one code to rule them all: one program to display all the needed
object-oriented features
// inheritance
// encapsulation
// abstraction
// polymorphism
// based on our previous assumed scenario
#include <iostream>
using namespace std;
// Abstract class
class Employee{
// encapsulation
protected:
string firstName, middleName, surName, ssn, phone, email;
float salary;
bool isSuspended;
public:
Employee(string fn, string mn, string sn, string ssn, string
p, string e, float s, bool isSuspended=false){
this->firstName = fn;
this->middleName = mn;
this->surName = sn;
this->ssn = ssn;
this->phone = p;
this->email = e;
this->salary = s;
this->isSuspended = isSuspended;
}
// public getters, implementing encapsulation once again
string getFirstName(){
return this->firstName;
}
string getMiddleName(){
return this->middleName;
}
string getLastName(){
return this->surName;
}
string getSsn(){
return this->ssn;
}
string getPhone(){
return this->phone;
}
string getEmail(){
return this->email;
}
float getSalary(){
return this->salary;
}
bool getState(){
return this->isSuspended;
}
void suspend(){
this->isSuspended = true;
}
// setters, only one is neccessary,
// its also a pure virtual function
virtual void raiseSalary(float newSalary)=0;
};
class Teacher:public Employee{
public:
int experienceLevel;
Teacher(string fn, string mn, string sn, string ssn, string p,
string e, float s, bool isSuspended=false, int exlvl=1) :
Employee(fn, mn, sn, ssn, p, e, s, isSuspended){
this->experienceLevel = exlvl;
}
void teach(){
cout<<"Subjects Tought!"<<endl;
}
void prepareLesson(){
cout<<"Preparing Lessons..."<<endl;
cout<<"Lesson Prepared!"<<endl;
}
// polymorphism
// implementing the virtual method for raising salary
void raiseSalary(float amount){
this->salary += amount * this->experienceLevel;
}
};
class Supervisor:public Teacher{
public:
int supervisionLevel;
Supervisor(string fn, string mn, string sn, string ssn, string
p, string e, float s, bool isSuspended=false, int exlvl=1, int
splvl=1):
Teacher(fn, mn, sn, ssn, p, e, s, isSuspended, exlvl){
this->supervisionLevel = splvl;
}
void supervise(){
cout<<this->getFirstName()<<" is currently
supervising"<<endl;
}
// polymorphism
// implementing the virtual method for raising salary
void raiseSalary(float amount){
this->salary += (this->experienceLevel + this-
>supervisionLevel)*amount;
}
};
int main(){
Teacher teacher("vincent", "gabriel", "laizer", "1234", "07564",
"
[email protected]", 1200.0);
Supervisor supervisor("Simeo", "Masatu", "Manyerere", "1235",
"07567", "
[email protected]", 1200.0);
// show the current salary
cout<<teacher.getFirstName()<<"'s salary is
"<<teacher.getSalary()<<"tsh"<<endl;
cout<<supervisor.getFirstName()<<"'s salary is
"<<supervisor.getSalary()<<"tsh"<<endl;
teacher.raiseSalary(500);
supervisor.raiseSalary(500);
cout<<endl;
// show new salary
cout<<teacher.getFirstName()<<"'s salary is
"<<teacher.getSalary()<<"tsh"<<endl;
cout<<supervisor.getFirstName()<<"'s salary is
"<<supervisor.getSalary()<<"tsh"<<endl;
// although the above objects have the same raise salary methods,
polymorphism enables them to have different implementation
// and so return different salary raise
return 0;
}
OUTPUT
From the above program Object-oriented concepts have been implemented as described below;
Inheritance: Teacher class inherits from Employee Class and Supervisor class inherits
from Teacher class
Encapsulation: The use of protected access modifier in Employee class employs a
certain level of encapsulation
Abstraction: Employee class is an abstract class since it contains a virtual function
raiseSalary()
Polymorphism: The raiseSalary() function has different implementations in both child
classes of Employee class
Question two:
The difference in implementation of Object Oriented Design Concept between Java and C++
1. Java uses a ‘super’ keyword to access the constructor of the base class while,
C++ uses ‘:’ constructorName(params) to access the constructor of the base class in inheritance
2. Java uses the '.' operator when using 'this' keyword, C++ uses the '->' operator as 'this' is
a pointer to the current instance of a class
3. Java implements polymorphism using methods with ‘abstract’ keyword, C++ implements
polymorphism with the use of virtual functions, ‘virtual’ keyword.
4. Java uses the ‘extends’ keyword to implement inheritance while C++ uses ‘:’ to implement
inheritance.