Advertisement
Guest User

aski1and2

a guest
Dec 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Machinery {
  6.    private:
  7.     char* name;
  8.     int weight;
  9.  
  10.    public:
  11.     char* get_name();
  12.     int get_weight();
  13.     virtual void operate(int hours) = 0;  // helps us make sure that children utilize the operate member function
  14.     Machinery(char* nam, int weight) : weight(weight) {
  15.         name = new char[strlen(nam) + 1];
  16.         strcpy(name, nam);
  17.     };
  18.     virtual ~Machinery() {
  19.         delete[] name;
  20.     };
  21. };
  22.  
  23. class Vehicle : public Machinery {
  24.    public:
  25.     Vehicle(char* nam, int weight) : Machinery(nam, weight) {}
  26.     virtual void operate(int hours) {
  27.         cout << this->get_name() << " worked for " << hours << " hours." << endl;
  28.     };
  29. };
  30.  
  31. class Computer : public Machinery {
  32.    public:
  33.     Computer(char* nam, int weight) : Machinery(nam, weight) {}
  34.     virtual void operate(int hours) {
  35.         cout << this->get_name() << " worked for " << hours << " hours." << endl;
  36.     };
  37. };
  38.  
  39. class Person {
  40.    public:
  41.     void workOn(Machinery* m) {
  42.         m->operate(10);
  43.     };
  44.     void workOn(Vehicle* v) {
  45.         v->operate(10);
  46.     };
  47. };
  48. class Technician : public Person {
  49.    public:
  50.     void workOn(Machinery* m) {
  51.         m->operate(10);
  52.     };
  53.     void workOn(Computer* pc) {
  54.         pc->operate(10);
  55.     };
  56. };
  57.  
  58. char* Machinery::get_name() { return name; }
  59. int Machinery::get_weight() { return weight; }
  60.  
  61. int main(void) {
  62.     Machinery* m = new Vehicle("Toyota", 155);
  63.     Person * per = new Technician();
  64.     per->workOn(m);
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement