Created
July 15, 2021 06:16
-
-
Save jiankaiwang/4dcb50d01b5ce1c92eb2ff45c16086de to your computer and use it in GitHub Desktop.
the compare between prototype and object oriented programming language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <string> | |
#include <vector> | |
class Employee { | |
protected: | |
std::string name = ""; | |
std::string dept = ""; | |
public: | |
Employee() { | |
name = ""; | |
dept = "general"; | |
} | |
void setName(std::string getName) { | |
name = getName; | |
} | |
void setDept(std::string getDept) { | |
dept = getDept; | |
} | |
std::string getName() { | |
return name; | |
} | |
std::string getDept() { | |
return dept; | |
} | |
~Employee() { | |
std::cout << "Deleted the instance." << std::endl; | |
} | |
}; | |
class Manager : public Employee { | |
protected: | |
std::vector<std::string> reports; | |
public: | |
Manager() {} | |
void addReport(std::string getReport) { | |
reports.push_back(getReport); | |
} | |
void showReports() { | |
for(int i = 0 ; i < reports.size(); i++) { | |
printf("%d: %s\n", i, reports[i].c_str()); | |
} | |
} | |
~Manager() { | |
reports.clear(); | |
printf("Clear the reports.\n"); | |
} | |
}; | |
class WorkerBee : public Employee { | |
protected: | |
std::vector<std::string> projects; | |
public: | |
WorkerBee() {} | |
~WorkerBee() { | |
projects.clear(); | |
printf("Clear the project.\n"); | |
} | |
void addProject(std::string getProject) { | |
projects.push_back(getProject); | |
} | |
void showProject() { | |
for(int i = 0 ; i < projects.size(); i++) { | |
printf("%d: %s\n", i, projects[i].c_str()); | |
} | |
} | |
}; | |
class Engineer : public WorkerBee { | |
protected: | |
std::string machine = ""; | |
public: | |
Engineer() { | |
machine = ""; | |
dept = "engineering"; | |
} | |
void setMachine(std::string getMachine) { | |
machine = getMachine; | |
} | |
std::string getMachine() { | |
return machine; | |
} | |
}; | |
int main(int argv, char* args[]) { | |
Engineer eg = Engineer(); | |
printf("Dept name: %s\n", eg.getDept().c_str()); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head></head> | |
<body> | |
<script type="text/javascript"> | |
function Employee(name, dept) { | |
this.name = name || ""; | |
this.dept = dept || "general"; | |
this.format = function(getName) { | |
this.name = getName; | |
}; | |
} | |
function Manager() { | |
this.reports = []; | |
} | |
Manager.prototype = new Employee; | |
function WorkerBee(name, dept, projects) { | |
this.base = Employee; | |
this.base(name, dept); | |
this.projects = projects || []; | |
} | |
WorkerBee.prototype = new Employee; | |
function SalesPerson() { | |
this.dept = "sales"; | |
this.quota = 100; | |
} | |
SalesPerson.prototype = new WorkerBee; | |
function Hobby(name) { | |
this.hobby = name || "HB"; | |
} | |
function Engineer(name, projects, machine, hobby) { | |
WorkerBee.call(this, name, "engineering", projects); | |
Hobby.call(this, hobby); | |
this.machine = machine || "MOBILE"; | |
} | |
Engineer.prototype = new WorkerBee; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment