我有一个 MasterProcess ,它是一般流程概念的专业化,MasterDocument 类应该以可能的通用方式考虑 MasterProcess 的几种不同专业化。我正在使用我正在使用 virtual void Init() 来实现此功能。代码如下:
#include <iostream>
#include <utility>
#include <string>
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <fstream>
#include <vector>
#include <functional>
#include <map>
#include <stdexcept>
using namespace std;
class BaseMasterProcess {
protected:
int processID;
int num_of_Questions;
double min_Threshold_Score_for_Process;
double total_Process_Score;
double overall_Audit_Value;
int question;
pair <int,double> answer;
//define all the variable used in any sub-class
int question1;
int question2;
int question3;
int question4;
int question5;
double question1_Weightage;
double question2_Weightage;
double question3_Weightage;
double question4_Weightage;
double question5_Weightage;
int passing_Score;
pair <int,double> answer1_Option1;
pair <int,double> answer1_Option2;
pair <int,double> answer1_Option3;
pair <int,double> answer2_Option1;
pair <int,double> answer2_Option2;
pair <int,double> answer2_Option3;
pair <int,double> answer3_Option1;
pair <int,double> answer3_Option2;
pair <int,double> answer3_Option3;
pair <int,double> answer4_Option1;
pair <int,double> answer4_Option2;
pair <int,double> answer4_Option3;
pair <int,double> answer5_Option1;
pair <int,double> answer5_Option2;
pair <int,double> answer5_Option3;
public:
virtual void Init();
virtual double getQuestionWeightage(int ques) = 0;
virtual double getAnswerScore(int ques, int ans) = 0;
int getNumQuestions()
{
return num_of_Questions;
}
int getProcesssID()
{
return processID;
}
double getMinThresholdScore()
{
return min_Threshold_Score_for_Process;
}
double overallAuditValue()
{
return overall_Audit_Value;
}
};
class ConcreteMasterProcess1 : public BaseMasterProcess
{
public:
Init()
{
processID = 10;
num_of_Questions = 3;
passing_Score = 70;
min_Threshold_Score_for_Process = 0.7;
overall_Audit_Value = 0.1;
question1 = 1;
question1_Weightage = 0.3;
answer1_Option1 = make_pair (1,0.3);
answer1_Option2 = make_pair (2,0.0);
question2 = 2;
question2_Weightage = 0.3;
answer2_Option1 = make_pair (1,0.3);
answer2_Option2 = make_pair (2,0.0);
question3 = 3;
question3_Weightage = 0.4;
answer3_Option1 = make_pair (1,0.4);
answer3_Option2 = make_pair (2,0.0);
}
double getQuestionWeightage(int ques)
{
switch (ques)
{
case 1:
return question1_Weightage;
case 2:
return question2_Weightage;
case 3:
return question3_Weightage;
}
double getAnswerScore(int ques, int ans)
{
if (ques == question1 && ans == answer1_Option1.first)
return answer1_Option1.second;
else if (ques == question1 && ans == answer1_Option2.first)
return answer1_Option2.second;
else if (ques == question2 && ans == answer2_Option1.first)
return answer2_Option1.second;
else if (ques == question2 && ans == answer2_Option2.first)
return answer2_Option2.second;
else if (ques == question3 && ans == answer3_Option1.first)
return answer3_Option1.second;
else
return answer3_Option2.second;
}
};
class ConcreteMasterProcess2 : public BaseMasterProcess
{
Init()
{
processID = 11;
num_of_Questions = 4;
passing_Score = 70;
min_Threshold_Score_for_Process = 0.75;
overall_Audit_Value = 0.1;
question1 = 1;
question1_Weightage = 0.25;
answer1_Option1 = make_pair (1,0.25);
answer1_Option2 = make_pair (2,0.0);
question2 = 2;
question2_Weightage = 0.25;
answer2_Option1 = make_pair (1,0.25);
answer2_Option2 = make_pair (2,0.0);
answer2_Option3 = make_pair (3,0.15);
question3 = 3;
question3_Weightage = 0.25;
answer3_Option1 = make_pair (1,0.25);
answer3_Option2 = make_pair (2,0.0);
question4 = 4;
question4_Weightage = 0.2;
answer4_Option1 = make_pair (1,0.2);
answer4_Option2 = make_pair (2,0.0);
question5 = 5;
question5_Weightage = 0.2;
answer5_Option1 = make_pair (1,0.2);
answer5_Option2 = make_pair (2,0.0);
}
double getQuestionWeightage(int ques)
{
switch (ques)
{
case 1:
return question1_Weightage;
break;
case 2:
return question2_Weightage;
break;
case 3:
return question3_Weightage;
break;
case 4:
return question4_Weightage;
break;
}
double getAnswerScore(int ques, int ans)
{
if (ques == question1 && ans == answer1_Option1.first)
return answer1_Option1.second;
else if (ques == question1 && ans == answer1_Option2.first)
return answer1_Option2.second;
else if (ques == question2 && ans == answer2_Option1.first)
return answer2_Option1.second;
else if (ques == question2 && ans == answer2_Option2.first)
return answer2_Option2.second;
else if (ques == question2 && ans == answer2_Option3.first)
return answer2_Option3.second;
else if (ques == question3 && ans == answer3_Option1.first)
return answer3_Option1.second;
else if (ques == question3 && ans == answer3_Option2.first)
return answer3_Option2.second;
else if (ques == question4 && ans == answer4_Option1.first)
return answer4_Option1.second;
else
return answer4_Option2.second;
}
};
class MasterDocument
{
std::vector<BaseMasterProcess*> myProcessList;
void AddProcess(BaseMasterProcess* iProcess)
{
myProcessList.push_back(iProcess);
}
void foo()
{
//myProcessList[...]->Method1(); //do something without knowing which specific concrete class the process belongs to..
}
};
int main ()
{
MasterDocument master_doc;
cout << master_doc.myProcessList.size();
}
}
编译时出现以下错误。
**MD.cxx:81:11: error: ISO C++ forbids declaration of ‘Init’ with no type [-fpermissive]
MD.cxx:138:11: error: ISO C++ forbids declaration of ‘Init’ with no type [-fpermissive]
MD.cxx:138:6: error: conflicting return type specified for ‘virtual int ConcreteMasterProcess1::ConcreteMasterProcess2::Init()’
MD.cxx:57:15: error: overriding ‘virtual void BaseMasterProcess::Init()’
MD.cxx:582:1: error: expected ‘;’ after class definition
MD.cxx:582:1: error: expected ‘}’ at end of input
MD.cxx:81:6: error: conflicting return type specified for ‘virtual int ConcreteMasterProcess1::Init()’
MD.cxx:57:15: error: overriding ‘virtual void BaseMasterProcess::Init()’
MD.cxx: In member function ‘virtual double ConcreteMasterProcess1::getQuestionWeightage(int)’:
MD.cxx:119:2: error: a function-definition is not allowed here before ‘{’ token
MD.cxx: In member function ‘virtual double ConcreteMasterProcess1::ConcreteMasterProcess2::getQuestionWeightage(int)’:
MD.cxx:192:2: error: a function-definition is not allowed here before ‘{’ token
MD.cxx: At global scope:
MD.cxx:580:1: error: expected unqualified-id at end of input**