Zuuh's Group Work
Zuuh's Group Work
Encapsulation
#include<iostream>
using namespace std;
class Employee //create class Employee
{
//everything in class in c++ is private by default unless specified
//members of class (attributes)
private:
string Name;
int EmployeeID;
int Age;
public:
void setName(string name) //setter method
{
Name=name;
}
string getName() //getter method
{
return Name;
}
void setEmployeeID(int ID)
{
EmployeeID=ID;
}
int getEmployeeID()
{
return EmployeeID;
}
void setAge(int age)
{
Age=age;
}
int getAge()
{
return Age;
}
void EmployeeAvailable() //method/function
{
cout<<"Employee Name: "<<Name<<endl;
cout<<"Employee ID: "<<EmployeeID<<endl;
cout<<"Employee Age: "<<Age<<endl;
}
int main()
{
//create variable of type Employee
//invoke constructor that is ready to receive argument
Employee e1 = Employee("Anna",2345,23);
e1.EmployeeAvailable(); //invoke function
Employee e2 = Employee("John",2567,25);
e2.EmployeeAvailable();
e1.setAge(40);
cout<<e1.getName()<<" is "<<e1.getAge()<<" years old"<<endl;
}
Abstraction
#include<iostream>
using namespace std;
class AbstractEmployee
{
//obligatory that is force any class that signs this contract (abstract class) to
implement this method
virtual void Promotion()=0;
};
class Employee:AbstractEmployee
{
private:
string Name;
int EmployeeID;
int Age;
public:
void setName(string name) //setter method
{
Name=name;
}
string getName() //getter method
{
return Name;
}
void setEmployeeID(int ID)
{
EmployeeID=ID;
}
int getEmployeeID()
{
return EmployeeID;
}
void setAge(int age)
{
Age=age;
}
int getAge()
{
return Age;
}
void EmployeeAvailable() //method/function
{
cout<<"Employee Name: "<<Name<<endl;
cout<<"Employee ID: "<<EmployeeID<<endl;
cout<<"Employee Age: "<<Age<<endl;
}
void Promotion()
{
if(Age>30)
cout<<Name<<" Got Promoted!"<<endl;
else
cout<<Name<<" Sorry No Promotion!"<<endl;
}
int main()
{
Employee e1 = Employee("Anna",2345,23);
Employee e2 = Employee("John",2567,35);
e1.Promotion();
e2.Promotion();
}
Inheritance
#include<iostream>
using namespace std;
class AbstractEmployee
{
virtual void Promotion()=0;
};
class Employee:AbstractEmployee
{
//everything in class in c++ is private by default unless specified
//members of class (attributes)
private:
string Name;
int EmployeeID;
int Age;
public:
void setName(string name) //setter method
{
Name=name;
}
string getName() //getter method
{
return Name;
}
void setEmployeeID(int ID)
{
EmployeeID=ID;
}
int getEmployeeID()
{
return EmployeeID;
}
void setAge(int age)
{
Age=age;
}
int getAge()
{
return Age;
}
void EmployeeAvailable() //method/function
{
cout<<"Employee Name: "<<Name<<endl;
cout<<"Employee ID: "<<EmployeeID<<endl;
cout<<"Employee Age: "<<Age<<endl;
}
void Promotion()
{
if(Age>30)
cout<<Name<<" Got Promoted!"<<endl;
else
cout<<Name<<" Sorry No Promotion!"<<endl;
}
int main()
{
Supervisor s1 = Supervisor("Happy",23456,24,300000);
s1.Supervise();
s1.Promotion();
}
Polymorphism
#include<iostream>
using namespace std;
class AbstractEmployee
{
virtual void Promotion()=0;
};
class Employee:AbstractEmployee
{
//everything in class in c++ is private by default unless specified
//members of class (attributes)
private:
string Name;
int EmployeeID;
int Age;
public:
void setName(string name) //setter method
{
Name=name;
}
string getName() //getter method
{
return Name;
}
void setEmployeeID(int ID)
{
EmployeeID=ID;
}
int getEmployeeID()
{
return EmployeeID;
}
void setAge(int age)
{
Age=age;
}
int getAge()
{
return Age;
}
void Promotion()
{
if(Age>30)
cout<<Name<<" Got Promoted!"<<endl;
else
cout<<Name<<" Sorry No Promotion!"<<endl;
}
void work()
{
cout<<Name<<" is checking emails"<<endl;
}
Employee(string name, int ID, int age) //constructor
{
Name = name;
EmployeeID = ID;
Age = age;
}
};
class Supervisor:public Employee //inheritance is private by default
{
public:
int salary;
Supervisor(string name, int ID, int age, int Salary)
:Employee(name,ID,age)
{
salary=Salary;
}
void Supervise()
{
cout<<getName()<<" has a salary of "<<salary<<endl;
}
void work()
{
cout<<getName()<<" is collecting reports from all employees"<<endl;
}
};
int main()
{
Employee e1 = Employee("Anna",4567,23);
Supervisor s1 = Supervisor("Happy",23456,24,300000);
e1.work();
s1.work();
}
2. DIFFERENTIATE IMPLEMENTATION OF OOD CONCEPTS BETWEEN
JAVA AND C++
i. In terms of encapsulation both JAVA and C++ use the set and get method which
are public to access the private properties (attributes).
ii. In abstraction, for C++ the abstract class doesn’t use the abstract keyword (and it
is known as contract) as well as its method, instead the method inside the abstract
class is made obligatory that means to force any class that signs this contract
(abstract class) to implement the method by using the virtual keyword and that
class can implement the contract by class classname: contractname
(abstractclassname) WHILE in JAVA it uses the abstract keyword for both the
class and method and uses the extend keyword to access the abstract class.
iii. In Inheritance, JAVA uses the extend keyword for the child class to inherit from
the parent class WHILE in C++ it uses to the colon (:) for the child class to
inherit from the parent class as well as the attributes have to be set to public since
in C++ the attributes are private by default otherwise use the set and get method.
iv. In polymorphism, JAVA uses the extend keyword as well as the method can be
accessed by the object created in the main class WHILE for C++ it uses the colon
(:) or the parent class reference is used to refer to a child class object that is by
creating a pointer of the parent class and assign to it a reference of the child class
(Example classname* pointername = &objectname of the child class) and to
access it, instead of using a dot as applied in JAVA, the pointers created can
access the method by using -> (Example pointername->methodname(); )