Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- using namespace std;
- class Machinery {
- private:
- char* name;
- int weight;
- public:
- char* get_name();
- int get_weight();
- virtual void operate(int hours) = 0; // helps us make sure that children utilize the operate member function
- Machinery(char* nam, int weight) : weight(weight) {
- name = new char[strlen(nam) + 1];
- strcpy(name, nam);
- };
- virtual ~Machinery() {
- delete[] name;
- };
- };
- class Vehicle : public Machinery {
- public:
- Vehicle(char* nam, int weight) : Machinery(nam, weight) {}
- virtual void operate(int hours) {
- cout << this->get_name() << " worked for " << hours << " hours." << endl;
- };
- };
- class Computer : public Machinery {
- public:
- Computer(char* nam, int weight) : Machinery(nam, weight) {}
- virtual void operate(int hours) {
- cout << this->get_name() << " worked for " << hours << " hours." << endl;
- };
- };
- class Person {
- public:
- void workOn(Machinery* m) {
- m->operate(10);
- };
- void workOn(Vehicle* v) {
- v->operate(10);
- };
- };
- class Technician : public Person {
- public:
- void workOn(Machinery* m) {
- m->operate(10);
- };
- void workOn(Computer* pc) {
- pc->operate(10);
- };
- };
- char* Machinery::get_name() { return name; }
- int Machinery::get_weight() { return weight; }
- int main(void) {
- Machinery* m = new Vehicle("Toyota", 155);
- Person * per = new Technician();
- per->workOn(m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement