Ood Concepts in C++
Ood Concepts in C++
GROUP MEMBERS
#include <iostream>
// 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;
}
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;
}
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;
}
};
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.raiseSalary(500);
supervisor.raiseSalary(500);
cout<<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
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.