2016-03-31 20 views
1

私は自分の問題の記述方法がわかりません。私は、派生クラスでは同じだが、別のパラメータを渡すベースクラスの関数を書くことが可能かどうかを知りたい。おそらく、私のコードでは、それを記述するよりもわかりやすいでしょう:派生クラスでもパラメータは同じで同じメソッド

class Employee 
{ 
public: 
    std::string name, profession; 
    std::string current_task = "NONE"; 
    int id, age; 

    // checks if the input by user a new task belongs to duties list 
    // for the class; if it does then it changes the current task 
    std::string AssignNewTask(std::vector<std::string> v, std::string input_string) 
    { 
     for (unsigned int i = 0; i < v.size(); i++) 
     { 
      if (input_string == v[i]) 
      { 
       return input_string; 
      } 
     } 

     return "NONE"; 
    } 
}; 

class HR : public Employee 
{ 
private: 
    static std::vector<std::string> tasks; //list of duties for HR employees 

public: 
    HR::HR() 
    { 
     Employee::profession = "HR Specialist"; 
    } 
    //the same function as above but passing tasks (std::vector with duties) 
    std::string AssignNewTask(tasks, std::string input_string) 
}; 

std::vector<std::string> HR::tasks = { "something" }; 

int main() 
{ 
    HR obj; 
    // 'something1' does not belong to duties list so still "NONE" 
    obj.AssignNewTask("something1"); 
    // 'something' belongs so current_task has been changed 
    obj.AssignNewTask("something"); 
} 

私はコードが動作しないことを知っています。私はちょうど私が正確に意味するものを示したいと思った。

+0

? – Samer

+0

1つの引数を取る 'AssignNewTask'のオーバーロードがないので、コードをコンパイルできません。 – SergeyA

+0

それは私が実際に書く方法を知らないので、それは私のコードの一部です。 'obj.AssignNewTask(" something1 ")'が2つの引数を必要とするので、上記のコードは機能しません – mathsicist

答えて

1

私は目的を正しく理解しています。

  • 我々は、それが派生クラスのタスクリストに含まれている場合のみタスクを割り当てますAssignNewTask機能
  • を提供するベースクラスを持っていると思います。

次のようにプログラムを修正しました。 AssignNewTaskはベースにありますが、構築中に派生クラスのタスクリストへの参照が取得されます。

#include <vector> 
#include <string> 

class Employee { 
public: 
    Employee(std::vector<std::string>& tasks) : tasks{tasks} {}; // we need a constructor here to pass the reference to the tasks 
    std::string name,profession; 
    std::string current_task = "NONE"; 
    int id,age; 

    std::vector<std::string>& tasks; // tasks now refers to the derived list of tasks 

    // checks if the input by user a new task belongs to duties list for the class; if it does then it changes the current task 
    virtual void AssignNewTask(std::string input_string) 
    { 
     for (unsigned int i = 0; i < tasks.size(); i++) { 
      if (input_string == tasks[i]) { 
       current_task = input_string; 
      } 
     } 
    } 
}; 

class HR : public Employee { 
public: 
    HR::HR() 
     : Employee(tasks) // We now pass the list of tasks by reference 
    { 
     Employee::profession = "HR Specialist"; 
    } 

    //AssignNewTask not required here as it has already been inherited 

private: 
    static std::vector<std::string> tasks; //list of duties for HR employees 

}; 


std::vector<std::string> HR::tasks ={"something"}; 

int main() 
{ 
    HR obj; 

    obj.AssignNewTask("something1"); // 'something1' does not belong to duties list so still "NONE" 
    obj.AssignNewTask("something"); // 'something' belongs so current_task has been changed 
} 

また、むしろ我々が現在持っているものよりrange based for loopを好むかもしれない:このコードでは動作しません、まさに

for (const auto& task : tasks) { 
    if (input_string == task) { 
     current_task = input_string; 
    } 
} 
+0

はい、それはまさに私が意味していたものです。私は正しく理解しているかどうか確認したいと思います:1.空の 'std :: vector tasks'を作成してコンパイラがエラーを返さないようにしましたか? 2。なぜ 'std :: vector タスク'の代わりに 'std :: vector &tasks'を書いたのですか? 3. 'Employee(std :: vector &tasks):タスク{tasks} {};という行は何を意味しますか? 4.なぜ '人事'コンストラクタが 'タスク'を渡すのですか? – mathsicist

+0

アップ、あなたはそれを編集しました。ごめんなさい。 – mathsicist

1

私は派生クラスで同じになる基本 クラスに関数を記述することが可能であるかどうかを確認したいが、それは可能である 異なるパラメータ

を渡し、それだろう派生クラスオブジェクトで割り当てられた基本ポインタを使用して多態性を達成しようとすると、コンテキスト内で基本関数を隠すことになります。

+0

あなたはそれについてもっと教えてください。私はメソッドを 'protected'に置くべきであることを知っています。しかし、次に何?私はその点を理解していない。 – mathsicist

+0

@mathsicistこのリンクは、http://www.gotw.ca/gotw/005.htm – Steephen

1

あなたのコードには多くの問題があるようです。まず、親クラスの関数を基底クラスに置き換えたい場合、親は関数をvirtualとして宣言しなければなりません。次に、メソッドが実際に同じパラメータを取るように見えますが、HRのベクトルのインスタンスを実際のベクトルの型と混同しているだけです。どのようにあなたは両方のクラスの文字列のベクトルをtypedefするのですか:

// new type now named StringVector 
typedef std::vector<std::string> StringVector; 

class Employee 
{ 
public: 
    std::string name, profession; 
    std::string current_task = "NONE"; 
    int id, age; 

    // checks if the input by user a new task belongs to duties list for the class; if it does then it changes the current task 
    virtual std::string AssignNewTask(StringVector v, std::string input_string) 
    { 
     for (unsigned int i = 0; i < v.size(); i++) 
     { 
      if (input_string == v[i]) 
      { 
       return input_string; 
      } 
     } 

     return "NONE"; 
    } 
}; 

class HR : public Employee 
{ 
private: 
    static StringVector tasks; //list of duties for HR employees 

public: 
    HR::HR() 
    { 
     Employee::profession = "HR Specialist"; 
    } 

    std::string AssignNewTask(StringVector tasks, std::string input_string) 
    { 
     // do something 
    } 

}; 

std::vector<std::string> HR::tasks = { "something" }; 

int main() 
{ 
    HR obj; 

    obj.AssignNewTask("something1"); // 'something1' does not belong to duties list so still "NONE" 
    obj.AssignNewTask("something"); // 'something' belongs so current_task has been changed 
} 

これはあなたの質問に答えてくれることを願っています。

+0

を完全には理解できません。しかし、あなたが気づいたように、多くの問題があります。私は 'virtual'としての機能を宣言することを忘れてしまった。 @フラットマウスは私が何を意味したかを推測した。 – mathsicist

+0

@mathsicistああ、私は実際の質問が不足していたと感じました。とにかくあなたの答えが得られたらうれしいです。 – Cmoraski

関連する問題