c++ - Inheritance , how can i make two classes share the same base class contents? -
c++ - Inheritance , how can i make two classes share the same base class contents? -
i have abstract class operations inherits var class , operations derived class(out,sleep,add) inherit operations class. fsm class inherits var also, want 1 instance of var class within program.
i trying create vector < pair< string, int>> var shared info between fsm class , operations class , deviates . initialized var in main through fsm class .
each time phone call exist function in var through class operation , returns doesn't exits cause empty ! how can overcome this?
#include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; class var { public: vector<pair<string, int>> var; var() { cout << "created var" << endl; } ~var(){ cout << "destrioed var" << endl; } void createvar(string x,int y) { pair<string, int>t; t.first = x; t.second = y; var.push_back(t); } int getvarvalue(string x) { (int = 0; i<var.size(); i++) { if (var[i].first == x) { homecoming var[i].second; } } } void setvarvalue(string& x, int y) { (int = 0; i<var.size(); i++) { if (var[i].first == x) { var[i].second = y; = var.size(); } } } bool exits(string& name) { (int = 0; i<var.size(); i++) { if (var[i].first == name) homecoming true; } homecoming false; } }; class operations : virtual public var { public: operations() { cout << "operations created" << endl; } ~operations() { cout << "operations destroied" << endl; } void virtual excute() = 0; }; class out :public virtual operations { public: string s; out(string xx = "") :s(xx) { cout << "out created" << endl; } ~out() { cout << "out destroied" << endl; } void virtual excute() { cout << "out class" << endl; if (exits(s)) cout<<"it never reach here, why !"<<endl; } }; class add together :public virtual operations { public: string s; add(string ss = "") :s(ss) { cout << "add created" << endl; } ~add() { cout << "add destroied" << endl; } void virtual excute() { string ex1 = s.substr(s.find('=') + 1, s.find('+')), ex2 = s.substr(s.find('+') + 1); if (exits(ex1)) cout<<"it never reach here, why !"<<endl; else result = atoi(ex1.c_str()); if (exits(ex2)) cout<<"it never reach here, why !"<<endl; } }; class state { public: vector<operations*> instructionlist; string name; void exec_all() { (int x = 0; x < instructionlist.size(); x++) instructionlist[x]->excute(); } }; class transition { public: vector < pair<state, vector<pair<state, int>>>> trans; static int currentstate; }; class fsm :public virtual var, public virtual transition { public: fsm() { cout << "fsm" << endl; } void intialize() { createvar("x", 1); createvar("y", 5); } }; void main() { fsm x; pair<state, vector<pair<state, int>>> p1; pair<state, int>p2; x.intialize(); p2.first.name = "b"; p2.second = 3; p1.first.name = "a"; p1.second.push_back(p2); x.trans.push_back(p1); x.trans[0].first.instructionlist.push_back(new add("x=x+y")); x.trans[0].first.instructionlist.push_back(new out("x")); x.trans[0].first.exec_all();//wrong output cause exist() returns false }
a minimal finish illustration looks this:
#include <iostream> using namespace std; class var { public: int var; virtual ~var() {} void setvar(int n) {var=n;} }; class out :public var {}; class fsm :public var {}; int main() { fsm x; x.setvar(5); out op; if (x.var==op.var) cout<<"it reaches here now" << endl; else cout << "it fails" << endl; return(0); }
and 1 way prepare this:
class var { public: static int var; int var; virtual ~var() {} void setvar(int n) {var=n;} }; int var::var=0;
c++ class inheritance
Comments
Post a Comment